DataSet导出Excel,比以往的方法导出的Excel外观更加好看
生活随笔
收集整理的這篇文章主要介紹了
DataSet导出Excel,比以往的方法导出的Excel外观更加好看
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
原文發(fā)布時(shí)間為:2010-06-21 —— 來源于本人的百度文章 [由搬家工具導(dǎo)入]
======目前方法=========== #region 生成Excel/// <summary>
/// DataSet導(dǎo)出Excel
/// </summary>
/// <param name="arrTitle">列標(biāo)題,若為null,則直接取dataset列標(biāo)題</param>
/// <param name="ds">要導(dǎo)出的DataSet</param>
/// <param name="fileName">Excel文件名,不需要傳入擴(kuò)展名</param>
protected void CreateExcel(string[] arrTitle, DataSet ds, string fileName)
{
StringBuilder strb = new StringBuilder();
strb.Append(" <html xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
strb.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
strb.Append("xmlns=\"http://www.w3.org/TR/REC-html40\"");
strb.Append(" <head> <meta http-equiv='Content-Type' content='text/html; charset=gb2312'>");
strb.Append(" <style>");
strb.Append(".xl26");
strb.Append(" {mso-style-parent:style0;");
strb.Append(" font-family:\"Times New Roman\", serif;");
strb.Append(" mso-font-charset:0;");
strb.Append(" mso-number-format:\"@\";}");
strb.Append(" </style>");
strb.Append(" <xml>");
strb.Append(" <x:ExcelWorkbook>");
strb.Append("? <x:ExcelWorksheets>");
strb.Append("? <x:ExcelWorksheet>");
strb.Append("??? <x:Name>Sheet1 </x:Name>");
strb.Append("??? <x:WorksheetOptions>");
strb.Append("??? <x:DefaultRowHeight>285 </x:DefaultRowHeight>");
strb.Append("??? <x:Selected/>");
strb.Append("??? <x:Panes>");
strb.Append("????? <x:Pane>");
strb.Append("????? <x:Number>3 </x:Number>");
strb.Append("????? <x:ActiveCol>1 </x:ActiveCol>");
strb.Append("????? </x:Pane>");
strb.Append("??? </x:Panes>");
strb.Append("??? <x:ProtectContents>False </x:ProtectContents>");
strb.Append("??? <x:ProtectObjects>False </x:ProtectObjects>");
strb.Append("??? <x:ProtectScenarios>False </x:ProtectScenarios>");
strb.Append("??? </x:WorksheetOptions>");
strb.Append("? </x:ExcelWorksheet>");
strb.Append("? <x:WindowHeight>6750 </x:WindowHeight>");
strb.Append("? <x:WindowWidth>10620 </x:WindowWidth>");
strb.Append("? <x:WindowTopX>480 </x:WindowTopX>");
strb.Append("? <x:WindowTopY>75 </x:WindowTopY>");
strb.Append("? <x:ProtectStructure>False </x:ProtectStructure>");
strb.Append("? <x:ProtectWindows>False </x:ProtectWindows>");
strb.Append(" </x:ExcelWorkbook>");
strb.Append(" </xml>");
strb.Append("");
strb.Append(" </head> <body> <table align=\"center\" style='border-collapse:collapse;table-layout:fixed'> <tr>");
if (ds.Tables.Count > 0)
{
//寫列標(biāo)題?
if (arrTitle != null && arrTitle.Length > 0)
{
foreach (string strCol in arrTitle)
{
strb.Append(" <td> <b>" + strCol + " </b> </td>");
}
strb.Append(" </tr>");
}
else
{
int columncount = ds.Tables[0].Columns.Count;
for (int columi = 0; columi < columncount; columi++)
{
strb.Append(" <td> <b>" + ds.Tables[0].Columns[columi] + " </b> </td>");
}
strb.Append(" </tr>");
}
//寫數(shù)據(jù)????
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
strb.Append(" <tr>");
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
strb.Append(" <td class='xl26'>" + ds.Tables[0].Rows[i][j].ToString() + " </td>");
}
strb.Append(" </tr>");
}
}
strb.Append(" </body> </html>");
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//設(shè)置輸出流為簡體中文??
Response.ContentType = "application/ms-excel";//設(shè)置輸出文件類型為excel文件。??
this.EnableViewState = false;
Response.Write(strb);
Response.End();
}
#endregion
//不過為什么導(dǎo)出Excel,單元格是受保護(hù)呢!
//導(dǎo)出word,只要把擴(kuò)展名 .xls 改成 .doc即可。
//這個(gè)人也是用這個(gè)方法:http://wyf.javaeye.com/blog/531171
===============以往方法===============
#region 生成Excel
/// <summary>
/// DataSet導(dǎo)出Excel
/// </summary>
/// <param name="arrTitle">列標(biāo)題,若為null,則直接取dataset列標(biāo)題</param>
/// <param name="ds">要導(dǎo)出的DataSet</param>
/// <param name="fileName">Excel文件名,不需要傳入擴(kuò)展名</param>
protected void CreateExcel(string[] arrTitle, DataSet ds, string fileName)
{
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
string colHeaders = "", ls_item = "";
//定義表對象與行對象,同時(shí)用DataSet對其值進(jìn)行初始化
System.Data.DataTable dt = ds.Tables[0];
DataRow[] myRow = dt.Select();//可以類似dt.Select("id>10")之形式達(dá)到數(shù)據(jù)篩選目的
int i = 0;
int cl = dt.Columns.Count;
//取得數(shù)據(jù)表各列標(biāo)題,各標(biāo)題之間以t分割,最后一個(gè)列標(biāo)題后加回車符
if (arrTitle != null && arrTitle.Length > 0)
{
for (i = 0; i < arrTitle.Length; i++)
{
if (i == (arrTitle.Length - 1))//最后一列,加n
{
colHeaders += arrTitle[i] + "\n";
}
else
{
colHeaders +=arrTitle[i] + "\t";
}
}
}
else
{
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))//最后一列,加n
{
colHeaders += dt.Columns[i].Caption.ToString() + "\n";
}
else
{
colHeaders += dt.Columns[i].Caption.ToString() + "\t";
}
}
}
resp.Write(colHeaders);
//向HTTP輸出流中寫入取得的數(shù)據(jù)信息
//逐行處理數(shù)據(jù)?
foreach (DataRow row in myRow)
{
//當(dāng)前行數(shù)據(jù)寫入HTTP輸出流,并且置空ls_item以便下行數(shù)據(jù)???
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))//最后一列,加n
{
ls_item += row[i].ToString() + "\n";
}
else
{
ls_item += row[i].ToString() + "\t";
}
}
resp.Write(ls_item);
ls_item = "";
}
resp.End();
}
#endregion
轉(zhuǎn)載于:https://www.cnblogs.com/handboy/p/7158409.html
總結(jié)
以上是生活随笔為你收集整理的DataSet导出Excel,比以往的方法导出的Excel外观更加好看的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 带有权重的服务器SLB的实现
- 下一篇: ES6_入门(2)_const命令