DataSet数据导出为Excel文档(每个DataTable为一个Sheet)

时间:2023-03-08 20:27:22
DataSet数据导出为Excel文档(每个DataTable为一个Sheet)

Web项目中,很多时候须要实现将查询的数据集导出为Excel文档的功能,很多时候不希望在工程中添加对Office组件相关的DLL的引用,甚至有时候受到Office不同版本的影响,导致在不同的服务器上部署后功能受限,或和其它项目冲突,那么,使用这种简单粗暴的方式,可能会解决部分猿类灵长动物的烦恼忧愁。

/// <summary>
/// DataSet导出到Excel文件(多个Sheet)
/// </summary>
/// <param name="dataSet">DataSet</param>
/// <param name="fileName">文件名称</param>
/// <returns>导出是否成功</returns>
public static bool DataSetToExcel(DataSet dataSet, string fileName)
{
if (dataSet == null) return false;
if (dataSet.Tables.Count < ) return false;
if (dataSet.Tables[] == null) return false;
if (string.IsNullOrWhiteSpace(fileName)) return false;
try
{
ExecDataSetToExcel(dataSet, fileName);
return true;
}
catch (Exception ex)
{
//DO LOG
return false;
}
}
//执行 DataSet 导出
private static void ExecDataSetToExcel(DataSet dataSet, string fileName)
{
HttpResponse httpResponse = HttpContext.Current.Response;
httpResponse.Clear();
httpResponse.Charset = "utf-8";
httpResponse.ContentType = "text/xml";
httpResponse.ContentEncoding = System.Text.Encoding.UTF8;
httpResponse.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8) + ".xls"); StringBuilder sb = new StringBuilder();
for (int i = ; i < dataSet.Tables.Count; i++)
{
System.Data.DataTable dt = dataSet.Tables[i];
//新建Sheet
string sheetName = string.IsNullOrWhiteSpace(dt.TableName) ? "Sheet" + (i + ) : dt.TableName;
sb.Append("<Worksheet ss:Name='" + sheetName + "'>");
sb.Append("<Table x:FullColumns='1' x:FullRows='1' ss:DefaultColumnWidth='118'>");
//输出标题行
sb.Append("<Row ss:AutoFitHeight='1'>");
for (int j = ; j < dt.Columns.Count; j++) sb.Append("<Cell ss:StyleID='header'><Data ss:Type='String'>" + dt.Columns[j].ColumnName + "</Data></Cell>");
sb.Append("</Row>");
//输入内容行
for (int k = ; k < dt.Rows.Count; k++)
{
sb.Append("<Row>");
for (int l = ; l < dt.Columns.Count; l++)
{
//Type ct = dt.Columns[l].DataType;
//sb.Append("<Cell ss:StyleID='body'><Data ss:Type='Number'>" + dt.Rows[k][l] + "</Data></Cell>"); //数字类型
sb.Append("<Cell ss:StyleID='body'><Data ss:Type='String'>" + dt.Rows[k][l] + "</Data></Cell>");
}
sb.Append("</Row>");
}
sb.Append("</Table>");
sb.Append("</Worksheet>");
} string excelXmlDoc = CreateExcelXmlDoc(sb.ToString());
httpResponse.Write(excelXmlDoc);
httpResponse.Flush();
httpResponse.End();
}
//创建Excel文档对应的XML字符串
private static string CreateExcelXmlDoc(string worksheetXml)
{
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version='1.0'?><?mso-application progid='Excel.Sheet'?>");
sb.Append("<Workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet' xmlns:o='urn:schemas-microsoft-com:office:office'"
+"xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet'"
+"xmlns:html='http://www.w3.org/TR/REC-html40'>");
//文档属性
sb.Append("<DocumentProperties xmlns='urn:schemas-microsoft-com:office:office'>"
+ "<Author>Unitech</Author><LastAuthor>Unitech</LastAuthor><Created></Created><LastSaved></LastSaved><Version>12.00</Version>"
+ "</DocumentProperties>");
//样式
sb.Append("<Styles>");
sb.Append("<Style ss:ID='header'>"
+ "<Font ss:FontName='宋体' x:CharSet='134' ss:Size='10' ss:Color='#000000' ss:Bold='0'/>"
+ "<Alignment ss:Horizontal='Center' ss:Vertical='Center'/>"
+ "<Interior ss:Color='#d8d8d8' ss:Pattern='Solid'/>"
+ "<Borders>"
+ "<Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1' ss:Color='#bfbfbf'/>"
+ "<Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1' ss:Color='#bfbfbf'/>"
+ "<Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1' ss:Color='#bfbfbf'/>"
+ "<Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1' ss:Color='#bfbfbf'/>"
+ "</Borders></Style>");
sb.Append("<Style ss:ID='body'>"
+ "<Font ss:FontName='宋体' x:CharSet='134' ss:Size='10' ss:Color='#000000'/>"
+ "<Alignment ss:Vertical='Center'/>"
+ "<NumberFormat ss:Format='@'/>"
+ "<Borders>"
+ "<Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1' ss:Color='#bfbfbf'/>"
+ "<Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1' ss:Color='#bfbfbf'/>"
+ "<Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1' ss:Color='#bfbfbf'/>"
+ "<Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1' ss:Color='#bfbfbf'/>"
+ "</Borders></Style>");
sb.Append("</Styles>");
//Work Sheet
sb.Append(worksheetXml);
sb.Append("</Workbook>");
return sb.ToString();
}

有需要的拿去自己进行改进和优化吧。