DataTable转XML输出
C# code
DataTable dt = new DataTable();
das.Fill(dt);
int rows = 1;
XElement rtnXml = new XElement("Response");
for (int i = 0; i < rows; i++)
{
DataRow row = dt.Rows[i];
XElement tmpjl = new XElement("Data",
new XElement("Name", row["UserName"].ToString().Trim()),
new XElement("Phone", row["Phone"].ToString().Trim())
);
rtnXml.Add(tmpjl);
}
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
rtnXml.Save(sw, SaveOptions.DisableFormatting);
string xml = sb.ToString();
Response.Write(xml);
想得到<Response><Data><Name>张三</Name><Phone>13300000000</Phone></Data></Response>
但是得到的是:张三13300000000
要怎么改才能转换过来?加上Response.ContentType = "text/xml";也不可以
------解决方案--------------------
参考:
做网站项目时,常需要将表格转化为 Excel 文件供用户下载,实现方法有很多种。如:
(1)将DataTable 直接进行 xml 序列化成文本
(2)将DataTable 转化为 Html table 格式
(3)将DataTable 转化为 Excel Xml 格式
(4)使用 Microsoft.Office.Interop.Excel.dll 创建Excel文件,再依次填写单元格数据
测试了这些方法,发现第3种简单且稳定,其它几种方法都有这样那样的小麻烦(如乱码和文件权限问题),我不在此一一提供这些方法的实现代码。以下为第3种方法的代码,本文参考了该篇文章:http://www.cnblogs.com/tsoukw/archive/2008/05/30/1210485.html,在此,我用静态函数实现了该功能。希望对大家有用。
excel xml 格式如:
Code
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Worksheet ss:Name="Sheet1">
<Table>
<Row>
<Cell ss:MergeAcross="1"><Data ss:Type="String">Excel xml</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">A2</Data></Cell>
<Cell><Data ss:Type="Number">0.112</Data></Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
以下是生成 excel xml 的源代码:
Code
// datatable -> excel
public static void ExportExcel(DataTable dt, string fileName, string[] displayColumnNames, string[] displayColumnCaptions)
{
Response.ClearContent();
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/vnd.ms-excel; charset=utf-8";
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.Write(DataTableToExcelTable(dt, displayColumnNames, displayColumnCaptions));
Response.End();
}
// 将 DataTable 转化为 ExcelXml
public static string DataTableToExcelTable(DataTable dt, string[] displayColumnNames, string[] displayColumnCaptions)
{
// 表开始
StringBuilder sb = new StringBuilder();
sb.AppendLine("<?xml version=\"1.0\"?>");
sb.AppendLine("<?mso-application progid=\"Excel.Sheet\"?>");
sb.AppendLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">");