c#后台如何导出excel到本地_C#实现导出Excel
這段時(shí)間用到了導(dǎo)出Excel的功能,這個(gè)功能還是比較常用的,我常用的有兩個(gè)方法,現(xiàn)在整理一下,方便以后查看。
一、實(shí)現(xiàn)DataTable數(shù)據(jù)導(dǎo)出到本地,需要自己傳進(jìn)去導(dǎo)出的路徑。
///
/// DataTable導(dǎo)出到Excel
///
/// DataTable類型的數(shù)據(jù)源
/// 需要導(dǎo)出的文件路徑
public void dataTableToCsv(DataTable table, string file)
{
string title = "";
FileStream fs = new FileStream(file, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(new BufferedStream(fs), System.Text.Encoding.Default);
for (int i = 0; i < table.Columns.Count; i++)
{
title += table.Columns[i].ColumnName + "\t"; //欄位:自動(dòng)跳到下一單元格
}
title = title.Substring(0, title.Length - 1) + "\n";
sw.Write(title);
foreach (DataRow row in table.Rows)
{
string line = "";
for (int i = 0; i < table.Columns.Count; i++)
{
line += row[i].ToString().Trim() + "\t"; //內(nèi)容:自動(dòng)跳到下一單元格
}
line = line.Substring(0, line.Length - 1) + "\n";
sw.Write(line);
}
sw.Close();
fs.Close();
}
二、實(shí)現(xiàn)DataTable數(shù)據(jù)導(dǎo)出到本地,路徑可以由用戶自主選擇。
///
/// DataTable導(dǎo)出到Excel
///
/// DataTable類型的數(shù)據(jù)源
/// 文件類型
/// 文件名
public void CreateExcel(DataTable dt, string FileType, string FileName)
{
Response.Clear();
Response.Charset = "UTF-8";
Response.Buffer = true;
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls\"");
Response.ContentType = FileType;
string colHeaders = string.Empty;
string ls_item = string.Empty;
DataRow[] myRow = dt.Select();
int i = 0;
int cl = dt.Columns.Count;
for (int j = 0; j < dt.Columns.Count; j++)
{
ls_item += dt.Columns[j].ColumnName + "\t"; //欄位:自動(dòng)跳到下一單元格
}
ls_item = ls_item.Substring(0, ls_item.Length - 1) + "\n";
foreach (DataRow row in myRow)
{
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))
{
ls_item += row[i].ToString() + "\n";
}
else
{
ls_item += row[i].ToString() + "\t";
}
}
Response.Output.Write(ls_item);
ls_item = string.Empty;
}
Response.Output.Flush();
Response.End();
}
三、方法的調(diào)用。
第一種方法的調(diào)用:
this.dataTableToCsv(dt, @"C:\Users\Admin\Desktop\收聽率" + DateTime.Now.ToString("yyyy-MM-dd HHmmss") + ".xls"); //調(diào)用函數(shù)
第二種方法的調(diào)用:
CreateExcel(dt, "application/ms-excel", "Excel" + DateTime.Now.ToString("yyyy-MM-dd HHmmss") + ".xls");//調(diào)用函數(shù)
這兩種方法都是可以直接調(diào)用的,只需要傳入需要的參數(shù)即可。
如果是通過前臺(tái)js方式實(shí)現(xiàn),跳轉(zhuǎn)href為實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出的頁面地址,我是通過前臺(tái)跳轉(zhuǎn)到ashx頁面實(shí)現(xiàn)的
ashx頁面內(nèi)容
public voidProcessRequest(HttpContext context)
{string message = "";string Action = context.Request["Action"];switch(Action)
{case "Excel":
DataTable Radio=GetExcleData(context); //獲取導(dǎo)出數(shù)據(jù)源
RadioCommon helper= newRadioCommon();
helper.ExportExcel(Radio,"application/ms-excel", "Excel" + DateTime.Now.ToString("yyyy-MM-dd HHmmss"));break;}
context.Response.Write(message);
}
js部分內(nèi)容
$("#Excel").bind('click', function () {var url = "AjaxHandler/RadioFamilyDayNumber.ashx?Action=Excel";
window.location.href= url;//導(dǎo)出Excel
})
總結(jié)
以上是生活随笔為你收集整理的c#后台如何导出excel到本地_C#实现导出Excel的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NAT基本原理与私有IP
- 下一篇: iReport —— A4打印,只占纸张