public void ExportXML(DataSet ds, string[] sheetName,bool autoAddTitle, string fileName)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(" <?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.Append(" <?mso-application progid=\"Excel.Sheet\"?>");
sb.Append(" <Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sb.Append(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
sb.Append(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
sb.Append(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sb.Append(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
sb.Append(" <DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">");
sb.Append(" <Author>Dean</Author>");
sb.Append(" <Created>" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "</Created>");
sb.Append(" <Company>SAE</Company>");
sb.Append(" <Version>1.0</Version>");
sb.Append(" </DocumentProperties>");
sb.Append(" <ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">");
sb.Append(" <ProtectStructure>False</ProtectStructure>");
sb.Append(" <ProtectWindows>False</ProtectWindows>");
sb.Append(" </ExcelWorkbook>");
sb.Append(" <Styles>");
sb.Append(" <Style ss:ID=\"s01\">");
sb.Append(" <Interior ss:Color=\"#FFFF00\" ss:Pattern=\"Solid\"/>");
sb.Append(" </Style>");
sb.Append(" </Styles>");
string cellType = "";
for (int num = 0; num < ds.Tables.Count; num++)
{
sb.Append(" <Worksheet ss:Name='" + sheetName[num] + "'>");
sb.Append(" <Table ss:ExpandedColumnCount='" + ds.Tables[num].Columns.Count + "' ss:ExpandedRowCount='" + ds.Tables[num].Rows.Count + 1 + "' x:FullColumns='1' x:FullRows='1'>");//ExpandedRowCount一定不能小于实际总行数,否则报错:This file is corrupt and cannot be opened.
if (autoAddTitle)
{
sb.Append(" <Row>");
foreach (DataColumn dc in ds.Tables[num].Columns)
{
sb.Append(" <Cell><Data ss:Type='String'>" + dc.ColumnName + "</Data></Cell>");
}
sb.Append(" </Row>");
}
for (int i = 0; i < ds.Tables[num].Rows.Count; i++)
{
sb.Append(" <Row>");
for (int j = 0; j < ds.Tables[num].Columns.Count; j++)
{
cellType = ds.Tables[num].Rows[i][j].GetType().ToString();
if (cellType == "System.Decimal" || cellType == "System.Double" || cellType == "System.Int16" || cellType == "System.Int32" || cellType == "System.Int64" || cellType == "System.Byte")
//sb.Append(" <Cell><Data ss:Type='Number'>" + ds.Tables[num].Rows[i][j].ToString() + "</Data></Cell>");
sb.Append(" <Cell ss:StyleID=\"s01\"><Data ss:Type='Number'>" + ds.Tables[num].Rows[i][j].ToString() + "</Data></Cell>");//添加颜色等style
else
sb.Append(" <Cell><Data ss:Type='String'>" + ds.Tables[num].Rows[i][j].ToString() + "</Data></Cell>");
}
sb.Append(" </Row>");
}
//sb.Append(" <Row><Cell><Data ss:Type='Number'>23</Data></Cell><Cell><Data ss:Type='Number'>11</Data></Cell><Cell ss:Formula=\"=RC[-2]+RC[-1]\"><Data ss:Type='Number'>34</Data></Cell></Row>");//添加公式
sb.Append(" </Table>");
sb.Append(" </Worksheet>");
}
sb.Append(" </Workbook>");
saveFile(sb, "application/vnd.ms-excel", fileName);
}
public void saveFile(System.Text.StringBuilder sb,string fileType,string fileName)
{
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(fileName + ".xls")));
Response.ContentType = fileType;
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Write(sb.ToString());
Response.Flush();
Response.End();
}
注:
可以添加颜色,公式,合并单元格等;
具体可以创建一个EXCEL文件,里面输入数据,保存为XML文件,然后用记事本打开此XML文件,即可看到里面的内容
浙公网安备 33010602011771号