Web項目中,不少時候需要實現將查詢的數據集導出爲Excel文檔的功能,不少時候不但願在工程中添加對Office組件相關的DLL的引用,甚至有時候受到Office不一樣版本的影響,致使在不一樣的服務器上部署後功能受限,或和其它項目衝突,那麼,使用這種簡單粗暴的方式,可能會解決部分猿類靈長動物的煩惱憂愁。html
/// <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 < 1) return false; if (dataSet.Tables[0] == 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 = 0; i < dataSet.Tables.Count; i++) { System.Data.DataTable dt = dataSet.Tables[i]; //新建Sheet string sheetName = string.IsNullOrWhiteSpace(dt.TableName) ? "Sheet" + (i + 1) : 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 = 0; 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 = 0; k < dt.Rows.Count; k++) { sb.Append("<Row>"); for (int l = 0; 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(); }
有須要的拿去本身進行改進和優化吧。服務器