Export a DataSet to Microsoft Excel without using the COM objects
It is a routine job to export data from MS SQL Server database to MS Excel, or import data from MS Excel to MS SQL Server database. There are lot of solutions based on MS Excel COM object, which make youe application depends on MS Excel, and you must install MS Excel application on you web application server. This kind of solutions are discouraged for the following reasons.
- For security reason, your customer maybe would not like to install Excel application on the web application server.
- The Excel process will expense large amount of memory of the web server. When there exists lots of instances of Excel process, it will use out the memory of the web server. In addition, you need manage the lifecycle of Excel process in your code to release resources shared by Excel.
So I prefer to not use Excel COM objects.
1. Sinlge DataTable in DataSet
Public Class DataExporter
Public Shared Function ExportToExcel(ByVal source As DataTable) As String
Dim sb As StringBuilder = New StringBuilder()
Const startExcelXML As String = "
"
"xmlns:x=""urn:schemas- microsoft-com:office:" & "excel""" & vbCr & vbLf & _
" xmlns:ss=""urn:schemas-microsoft-com:" & "office:spreadsheet"">" & vbCr & vbLf & _
"
"" & vbCr & vbLf & " " & vbCr & vbLf & " " & _
"" & vbCr & vbLf & " " & vbCr & vbLf & " " & _
"
Const endExcelXML As String = ""
Dim rowCount As Integer = 0
Dim sheetCount As Integer = 1
sb.Append(startExcelXML)
sb.Append(" For x As Integer = 0 To source.Columns.Count - 1 For Each x As DataRow In source.Rows sb.Append(" sb.Append(" Next ' iterate rows end sb.Append("
sb.Append("")
")
sb.Append("
sb.Append("
sb.Append(source.Columns(x).ColumnName)
sb.Append("
Next
sb.Append("
rowCount += 1
'if the number of rows is > 64000 create a new page to continue output
If rowCount = 64000 Then
rowCount = 0
sheetCount += 1
sb.Append("
sb.Append("
sb.Append("
sb.Append("")
")
End If
'ID=" + rowCount + "
For y As Integer = 0 To source.Columns.Count - 1
Dim rowType As System.Type
rowType = x(y).[GetType]()
Select Case rowType.ToString()
Case "System.String"
Dim XMLstring As String = x(y).ToString()
Const quote As Char = """"
XMLstring = XMLstring.Trim()
XMLstring = XMLstring.Replace("&", "&")
XMLstring = XMLstring.Replace(">", ">")
XMLstring = XMLstring.Replace("XMLstring = XMLstring.Replace("'", "'")
XMLstring = XMLstring.Replace(quote, """)
sb.Append(XMLstring)
sb.Append("
Exit Select
Case "System.DateTime"
'Excel has a specific Date Format of YYYY-MM-DD followed by
'the letter 'T' then hh:mm:sss.lll Example 2005-01-31T24:01:21.000
'The Following Code puts the date stored in XMLDate
'to the format above
Dim XMLDate As DateTime = DirectCast(x(y), DateTime)
Dim XMLDatetoString As String = ""
'Excel Converted Date
XMLDatetoString = (((((XMLDate.Year.ToString() & "-") + _
(If(XMLDate.Month < 10, "0" & XMLDate.Month.ToString(), XMLDate.Month.ToString())) & "-") + _
(If(XMLDate.Day < 10, "0" & XMLDate.Day.ToString(), XMLDate.Day.ToString())) & "T") + _
(If(XMLDate.Hour < 10, "0" & XMLDate.Hour.ToString(), XMLDate.Hour.ToString())) & ":") + _
(If(XMLDate.Minute < 10, "0" & XMLDate.Minute.ToString(), XMLDate.Minute.ToString())) & ":") + _
(If(XMLDate.Second < 10, "0" & XMLDate.Second.ToString(), XMLDate.Second.ToString())) & ".000"
sb.Append("
sb.Append(XMLDatetoString)
sb.Append("
Exit Select
Case "System.Boolean"
sb.Append("
sb.Append(x(y).ToString())
sb.Append("
Exit Select
Case "System.Int16", "System.Int32", "System.Int64", "System.Byte"
sb.Append("
sb.Append(x(y).ToString())
sb.Append("
Exit Select
Case "System.Decimal", "System.Double"
sb.Append("
sb.Append(x(y).ToString())
sb.Append("
Exit Select
Case "System.DBNull"
sb.Append("
sb.Append("")
sb.Append("
Exit Select
Case Else
Throw (New Exception(rowType.ToString() & " not handled."))
End Select
Next ' iterate columns end
sb.Append("")
sb.Append("
sb.Append(endExcelXML)
Return sb.ToString()
End Function
End Class
2. Multiple DataTables in a DataSet
public static void exportToExcel(DataSet source, string fileName)
{
System.IO.StreamWriter excelDoc;
excelDoc = new System.IO.StreamWriter(fileName);
const string startExcelXML = "
" xmlns:o="urn:schemas-microsoft-com:office:office"rn " +
"xmlns:x="urn:schemas- microsoft-com:office:" +
"excel"rn xmlns:ss="urn:schemas-microsoft-com:" +
"office:spreadsheet">rn
"rn " +
"rn " +
"rn "ss:ID="Decimal">rn
"rn "ss:ID="DateLiteral">rn
"
const string endExcelXML = "";
int rowCount = 0;
int sheetCount = 1;
/*
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
*/
excelDoc.Write(startExcelXML);
excelDoc.Write("
excelDoc.Write("");
");
excelDoc.Write("
for(int x = 0; x < source.Tables[0].Columns.Count; x++)
{
excelDoc.Write("
excelDoc.Write(source.Tables[0].Columns[x].ColumnName);
excelDoc.Write("
}
excelDoc.Write("
foreach(DataRow x in source.Tables[0].Rows)
{
rowCount++;
//if the number of rows is > 64000 create a new page to continue output
if(rowCount==64000)
{
rowCount = 0;
sheetCount++;
excelDoc.Write("
excelDoc.Write("
excelDoc.Write("
excelDoc.Write("");
");
}
excelDoc.Write("
for(int y = 0; y < source.Tables[0].Columns.Count; y++)
{
System.Type rowType;
rowType = x[y].GetType();
switch(rowType.ToString())
{
case "System.String":
string XMLstring = x[y].ToString();
XMLstring = XMLstring.Trim();
XMLstring = XMLstring.Replace("&","&");
XMLstring = XMLstring.Replace(">",">");
XMLstring = XMLstring.Replace("excelDoc.Write("
"");
excelDoc.Write(XMLstring);
excelDoc.Write("
break;
case "System.DateTime":
//Excel has a specific Date Format of YYYY-MM-DD followed by
//the letter 'T' then hh:mm:sss.lll Example 2005-01-31T24:01:21.000
//The Following Code puts the date stored in XMLDate
//to the format above
DateTime XMLDate = (DateTime)x[y];
string XMLDatetoString = ""; //Excel Converted Date
XMLDatetoString = XMLDate.Year.ToString() +
"-" +
(XMLDate.Month < 10 ? "0" +
XMLDate.Month.ToString() : XMLDate.Month.ToString()) +
"-" +
(XMLDate.Day < 10 ? "0" +
XMLDate.Day.ToString() : XMLDate.Day.ToString()) +
"T" +
(XMLDate.Hour < 10 ? "0" +
XMLDate.Hour.ToString() : XMLDate.Hour.ToString()) +
":" +
(XMLDate.Minute < 10 ? "0" +
XMLDate.Minute.ToString() : XMLDate.Minute.ToString()) +
":" +
(XMLDate.Second < 10 ? "0" +
XMLDate.Second.ToString() : XMLDate.Second.ToString()) +
".000";
excelDoc.Write("
"");
excelDoc.Write(XMLDatetoString);
excelDoc.Write("
break;
case "System.Boolean":
excelDoc.Write("
"");
excelDoc.Write(x[y].ToString());
excelDoc.Write("
break;
case "System.Int16":
case "System.Int32":
case "System.Int64":
case "System.Byte":
excelDoc.Write("
"");
excelDoc.Write(x[y].ToString());
excelDoc.Write("
break;
case "System.Decimal":
case "System.Double":
excelDoc.Write("
"");
excelDoc.Write(x[y].ToString());
excelDoc.Write("
break;
case "System.DBNull":
excelDoc.Write("
"");
excelDoc.Write("");
excelDoc.Write("
break;
default:
throw(new Exception(rowType.ToString() + " not handled."));
}
}
excelDoc.Write("
}
excelDoc.Write("
excelDoc.Write("
excelDoc.Write(endExcelXML);
excelDoc.Close();
}
Actually this approach exports data to an xml-formatting string, and the file you exported is not really an Excel file. The end users must open the exported .xls file with MS Excel application and use "Save As" to reconstruct it as a "real" Excel file. Otherwise end users can not use it to import data to MS SQL Server database.
References
[@more@]來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/13651903/viewspace-1043346/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 翻譯 | Learning React Without Using React Part 2React
- 翻譯 | Learning React Without Using React Part 1React
- MGTSC 212 using ExcelExcel
- Microsoft Excel 2021 for MacROSExcelMac
- Microsoft Excel 2019 for Mac(office excel 2019)ROSExcelMac
- 在 Microsoft Dynamics NAV 2018 Setting Up and Using a Purchase Approval WorkflowROSAPP
- Excel Export 踩坑注意點+匯出方案設計ExcelExport
- ObjectsObject
- SAP UI5 表格資料如何匯出成 Excel 檔案(Table Export As Excel)UIExcelExport
- Mac版如何在Microsoft Excel中使用Snip?MacROSExcel
- Microsoft Excel 教程「2」,如何在 Excel 中建立資料透檢視?ROSExcel
- Microsoft Excel 教程「1」,如何在 Excel 圖表中新增趨勢線?ROSExcel
- TableTools Export Excel前Table內容格式的轉換應用ExportExcel
- Microsoft Excel 2019 for Mac(excel電子表格) v16.45 Beta中文啟用版ROSExcelMac
- export 和 export default 區別Export
- JavaScript datasetJavaScript
- JavaScript中的export、export default、exports和module.exports(export、export default、exports使用詳細)JavaScriptExport
- Microsoft Excel 教程「3」,如何在 Excel 中保護檔案、轉換資料型別?ROSExcel資料型別
- export和export default的區別Export
- 【Dataset】Maple-IDS - Network Security Malicious Traffic Detection Dataset
- SCSS without和withCSS
- 2.3.2 Application Common ObjectsAPPObject
- import、require 、export、export default、exports、module exportsImportUIExport
- Mmdetection dataset pipline
- tensorflow dataset APIAPI
- export/importExportImport
- 利用wps的com口用python實現excel轉pdfPythonExcel
- 6.exports、module.exports、export、export defalutExport
- JavaScript ES6中,export與export defaultJavaScriptExport
- ES6:export 與 export default 區別Export
- Microsoft Excel LTSC 2021 for Mac v16.80beta中文啟用版ROSExcelMac
- Performance Without the Event LoopORMOOP
- scp without interative password
- zend_objects_store_putObject
- 2.3.2.1 Creation of Application Common ObjectsAPPObject
- 2.1.3.3 Container Data Objects in a CDBAIObject
- autohotkey透過com物件控制excel的許可權問題物件Excel
- Pytorch Dataset入門PyTorch
- image-classification-dataset