.net客戶端導(dǎo)出Excel實(shí)現(xiàn)代碼及注意事項(xiàng)
更新時(shí)間:2013年02月18日 11:57:42 作者:
將DataGrid導(dǎo)出為Excel文件及導(dǎo)出dgData中0-3列的數(shù)據(jù)到excel文件中的優(yōu)缺點(diǎn)介紹,感興趣的朋友可以了解下,希望本文對(duì)你有所幫助
客戶端導(dǎo)出excel
/*
* 將DataGrid導(dǎo)出為Excel文件
*
* @param strTitle 文件標(biāo)題
* @param dgData 待導(dǎo)出的DataGrid
* @param iStartCol 起始列序號(hào)
* @param iEndCol 結(jié)束列序號(hào)
*
* 創(chuàng)建人: calvin
* 創(chuàng)建日期: 2005-10-08
* 修改人:
* 修改日期:
**/
function DataGrid2Excel(strTitle, dgData, iStartCol, iEndCol)
{
// 定義Excel Applicaiton Object
var appExcel = null;
// 當(dāng)前激活的工作簿
var currentWork = null;
var currentSheet = null;
try
{
// 初始化application
appExcel = new ActiveXObject("Excel.Application");
appExcel.Visible = true;
}
catch(e)
{
window.alert("Please Install Excel First");
return;
}
// 獲取當(dāng)前激活的工作部
currentWork = appExcel.Workbooks.Add();
currentSheet = currentWork.ActiveSheet;
// 填充excel內(nèi)容
// 設(shè)置標(biāo)題
currentSheet.Cells(1,1).Value = strTitle;
currentSheet.Cells(1,1).Value = dgData.innerText;
window.alert(dgData.innerHTML);
// 填充內(nèi)容
for (var iRow = 0; iRow < dgData.rows.length - 1; iRow++)
{
// 顯示指定列的內(nèi)容
for (var iCol = iStartCol; iCol <= iEndCol; iCol++)
{
currentSheet.Cells(iRow + 2, iCol + 1).Value =
dgData.rows[iRow].cells[iCol].innerText;
}
}
}
/**************************************************************************/
/**
* 導(dǎo)出dgData中0-3列的數(shù)據(jù)到excel文件中
**/
function ToExcel()
{
DataGrid2Excel("使用javascript導(dǎo)出excel的例子", document.getElementsById("dgData"), 0, 3);
} 這種方法的缺點(diǎn)是:
?。?)了能夠在客戶端調(diào)用Excel.Application,需要把IE的安全級(jí)別設(shè)為“低”。
?。?)與方法一相同,還是只能導(dǎo)出當(dāng)前顯示在datagrid里面的數(shù)據(jù),無(wú)法導(dǎo)出分頁(yè)的數(shù)據(jù)。
--------------------------------------------------------------------------------
終極解決方案:將DataTable導(dǎo)出為excel
好,讓我們快點(diǎn)結(jié)束這篇無(wú)聊的post。一般來(lái)說(shuō),頁(yè)面上的datagrid是以查詢得到的一個(gè)DataTable為數(shù)據(jù)源的。那么為了把全部數(shù)據(jù)導(dǎo)入excel中,我們只要把DataTable數(shù)據(jù)源輸出為excel就可以了。
/**//// <summary>
/// 把DataTable內(nèi)容導(dǎo)出偉excel并返回客戶端
/// </summary>
/// <param name="dgData">待導(dǎo)出的DataTable</param>
/// 創(chuàng) 建 人:陳文凱
/// 創(chuàng)建日期:2005年10月08日
/// 修 改 人:
/// 修改日期:
public static void DataTable2Excel(System.Data.DataTable dtData)
{
System.Web.UI.WebControls.DataGrid dgExport = null;
// 當(dāng)前對(duì)話
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
// IO用于導(dǎo)出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
if (dtData != null)
{
// 設(shè)置編碼和附件格式
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.ContentEncoding =System.Text.Encoding.UTF8;
curContext.Response.Charset = "";
// 導(dǎo)出excel文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
// 為了解決dgData中可能進(jìn)行了分頁(yè)的情況,需要重新定義一個(gè)無(wú)分頁(yè)的DataGrid
dgExport = new System.Web.UI.WebControls.DataGrid();
dgExport.DataSource = dtData.DefaultView;
dgExport.AllowPaging = false;
dgExport.DataBind();
// 返回客戶端
dgExport.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
}
}
需要注意的是,導(dǎo)出excel之前要把datatable的列名更改為客戶要求的文字,就ok了。因?yàn)槭菑腄ataTable導(dǎo)出的,所以這種方法解決了分頁(yè)數(shù)據(jù)的問(wèn)題,堪稱(chēng)終極解決方案。
復(fù)制代碼 代碼如下:
/*
* 將DataGrid導(dǎo)出為Excel文件
*
* @param strTitle 文件標(biāo)題
* @param dgData 待導(dǎo)出的DataGrid
* @param iStartCol 起始列序號(hào)
* @param iEndCol 結(jié)束列序號(hào)
*
* 創(chuàng)建人: calvin
* 創(chuàng)建日期: 2005-10-08
* 修改人:
* 修改日期:
**/
function DataGrid2Excel(strTitle, dgData, iStartCol, iEndCol)
{
// 定義Excel Applicaiton Object
var appExcel = null;
// 當(dāng)前激活的工作簿
var currentWork = null;
var currentSheet = null;
try
{
// 初始化application
appExcel = new ActiveXObject("Excel.Application");
appExcel.Visible = true;
}
catch(e)
{
window.alert("Please Install Excel First");
return;
}
// 獲取當(dāng)前激活的工作部
currentWork = appExcel.Workbooks.Add();
currentSheet = currentWork.ActiveSheet;
// 填充excel內(nèi)容
// 設(shè)置標(biāo)題
currentSheet.Cells(1,1).Value = strTitle;
currentSheet.Cells(1,1).Value = dgData.innerText;
window.alert(dgData.innerHTML);
// 填充內(nèi)容
for (var iRow = 0; iRow < dgData.rows.length - 1; iRow++)
{
// 顯示指定列的內(nèi)容
for (var iCol = iStartCol; iCol <= iEndCol; iCol++)
{
currentSheet.Cells(iRow + 2, iCol + 1).Value =
dgData.rows[iRow].cells[iCol].innerText;
}
}
}
/**************************************************************************/
/**
* 導(dǎo)出dgData中0-3列的數(shù)據(jù)到excel文件中
**/
function ToExcel()
{
DataGrid2Excel("使用javascript導(dǎo)出excel的例子", document.getElementsById("dgData"), 0, 3);
} 這種方法的缺點(diǎn)是:
?。?)了能夠在客戶端調(diào)用Excel.Application,需要把IE的安全級(jí)別設(shè)為“低”。
?。?)與方法一相同,還是只能導(dǎo)出當(dāng)前顯示在datagrid里面的數(shù)據(jù),無(wú)法導(dǎo)出分頁(yè)的數(shù)據(jù)。
--------------------------------------------------------------------------------
終極解決方案:將DataTable導(dǎo)出為excel
好,讓我們快點(diǎn)結(jié)束這篇無(wú)聊的post。一般來(lái)說(shuō),頁(yè)面上的datagrid是以查詢得到的一個(gè)DataTable為數(shù)據(jù)源的。那么為了把全部數(shù)據(jù)導(dǎo)入excel中,我們只要把DataTable數(shù)據(jù)源輸出為excel就可以了。
復(fù)制代碼 代碼如下:
/**//// <summary>
/// 把DataTable內(nèi)容導(dǎo)出偉excel并返回客戶端
/// </summary>
/// <param name="dgData">待導(dǎo)出的DataTable</param>
/// 創(chuàng) 建 人:陳文凱
/// 創(chuàng)建日期:2005年10月08日
/// 修 改 人:
/// 修改日期:
public static void DataTable2Excel(System.Data.DataTable dtData)
{
System.Web.UI.WebControls.DataGrid dgExport = null;
// 當(dāng)前對(duì)話
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
// IO用于導(dǎo)出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
if (dtData != null)
{
// 設(shè)置編碼和附件格式
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.ContentEncoding =System.Text.Encoding.UTF8;
curContext.Response.Charset = "";
// 導(dǎo)出excel文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
// 為了解決dgData中可能進(jìn)行了分頁(yè)的情況,需要重新定義一個(gè)無(wú)分頁(yè)的DataGrid
dgExport = new System.Web.UI.WebControls.DataGrid();
dgExport.DataSource = dtData.DefaultView;
dgExport.AllowPaging = false;
dgExport.DataBind();
// 返回客戶端
dgExport.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
}
}
需要注意的是,導(dǎo)出excel之前要把datatable的列名更改為客戶要求的文字,就ok了。因?yàn)槭菑腄ataTable導(dǎo)出的,所以這種方法解決了分頁(yè)數(shù)據(jù)的問(wèn)題,堪稱(chēng)終極解決方案。
相關(guān)文章
Ubuntu16.04系統(tǒng)搭建.Net Core開(kāi)發(fā)環(huán)境
本文詳細(xì)講解了Ubuntu系統(tǒng)搭建.Net Core開(kāi)發(fā)環(huán)境的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02ASP.NET實(shí)現(xiàn)上傳圖片并生成縮略圖的方法
這篇文章主要介紹了ASP.NET實(shí)現(xiàn)上傳圖片并生成縮略圖的方法,提供了一個(gè)asp.net上傳圖片與生成縮略圖的方法,并給出了改進(jìn)程序供大家對(duì)比分析,需要的朋友可以參考下2015-12-12ASP.NET框架中的數(shù)據(jù)綁定概要與數(shù)據(jù)綁定表達(dá)式的使用
數(shù)據(jù)綁定是ASP.NET中操作數(shù)據(jù)的基礎(chǔ)方式,這里我們暫時(shí)拋開(kāi).NET提供的控件,來(lái)從基礎(chǔ)上講解ASP.NET框架中的數(shù)據(jù)綁定概要與數(shù)據(jù)綁定表達(dá)式的使用:2016-06-06Asp.net在頁(yè)面間傳遞大量數(shù)據(jù)(數(shù)據(jù)表)建議采用的方法
能讓數(shù)據(jù)在 兩個(gè)不同站點(diǎn)之間傳遞嗎,針對(duì)這個(gè)問(wèn)題將會(huì)展開(kāi)本文的探討有想在A站點(diǎn)的數(shù)據(jù)傳遞到B站點(diǎn)的朋友們可以適當(dāng)參考下,或許本文對(duì)你有所幫助2013-02-02