將 DataTable 轉化為 Excel Xml 格式供下載

來源:互聯網
上載者:User

做網站項目時,常需要將表格轉化為 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\">");
            sb.AppendLine(" <Worksheet ss:Name=\"Sheet1\">");
            sb.AppendLine("  <Table>");
//
// 輸出標題
//
            sb.AppendLine("   <Row>");
if (displayColumnCaptions != null)
            {
// 輸出指定欄位標題
for (int i = 0; i < displayColumnCaptions.Length; i++)
                    sb.AppendLine("    <Cell><Data ss:Type=\"String\">" + displayColumnCaptions[i] + "</Data></Cell>");
            }
else if (displayColumnNames != null)
            {
// 輸出展示欄位標題
for (int i = 0; i < displayColumnNames.Length; i++)
                    sb.AppendLine("    <Cell><Data ss:Type=\"String\">" + displayColumnNames[i] + "</Data></Cell>");
            }
else
            {
// 輸出所有欄位標題
for (int i = 0; i < dt.Columns.Count; i++)
                    sb.AppendLine("    <Cell><Data ss:Type=\"String\">" + dt.Columns[i].Caption + "</Data></Cell>");
            }
            sb.AppendLine("   </Row>");
//
// 輸出資料
//
if (displayColumnNames != null)
            {
// 輸出指定列資料
foreach (DataRow dr in dt.Rows)
                {
                    sb.AppendLine("   <Row>");
foreach (string colName in displayColumnNames)
                        sb.AppendLine("    <Cell><Data ss:Type=\"String\">" + dr[colName].ToString() + "</Data></Cell>");
                    sb.AppendLine("   </Row>");
                }
            }
else
            {
//輸出所有列資料
foreach (DataRow dr in dt.Rows)
                {
                    sb.AppendLine("   <Row>");
                    Object[] ary = dr.ItemArray;
for (int i = 0; i <= ary.GetUpperBound(0); i++)
                        sb.AppendLine("    <Cell><Data ss:Type=\"String\">" + ary[i].ToString() + "</Data></Cell>");
                    sb.AppendLine("   </Row>");
                }
            }
// 表結束
            sb.AppendLine("  </Table>");
            sb.AppendLine(" </Worksheet>");
            sb.AppendLine("</Workbook>");
return sb.ToString();
        }

此外需注意,必須匯出為 UTF-8格式的xml,否則excel 無法開啟該檔案,會報錯滴~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.