`
yangyou230
  • 浏览: 1639344 次
文章分类
社区版块
存档分类

C#导出Excel cvs数据

 
阅读更多
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Windows.Forms; using System.IO; namespace TopAPI { class ExcelCreate { public void CreateCsv(System.Data.DataTable dt, string strName) { //先打印标头 StringBuilder strColu=new StringBuilder(); StringBuilder strValue=new StringBuilder(); int i=0; try { StreamWriter sw = new StreamWriter(new FileStream(strName+".csv", FileMode.Create), Encoding.GetEncoding("GB2312")); for( i=0;i <workbook xmlns:o='/"urn:schemas-microsoft-com:office:office/"' microsoft-com:office: xmlns:ss='/"urn:schemas-microsoft-com:"'><styles> " + "<style ss:id='/"Default/"' ss:name='/"Normal/"'> " + "<Alignment ss:Vertical=/"Bottom/"/> <Borders/>" + " <Font/> <Interior/> <NumberFormat/>" + " <Protection/> </style> " + "<style ss:id='/"BoldColumn/"'> <Font " + "x:Family=/"Swiss/" ss:Bold=/"1/"/> </style> " + "<style ss:id='/"StringLiteral/"'> <NumberFormat" + " ss:Format=/"@/"/> </style> <style> <NumberFormat " + "ss:Format=/"0.0000/"/> </style> " + "<style ss:id='/"Integer/"'> <NumberFormat " + "ss:Format=/"0/"/> </style> <style> <NumberFormat " + "ss:Format=/"mm/dd/yyyy;@/"/> </style> " + "</styles> "; const string endExcelXML = "</workbook>"; int rowCount = 0; int sheetCount = 1; excelDoc.Write(startExcelXML); excelDoc.Write("<worksheet ss:name='/"Sheet"' sheetcount>"); excelDoc.Write("<table>"); excelDoc.Write("<row>"); for (int x = 0; x <data ss:type='/"String/"'>"); excelDoc.Write(source.Columns[x].ColumnName); excelDoc.Write("</data>"); } excelDoc.Write("</row>"); foreach (DataRow x in source.Rows) { rowCount++; //if the number of rows is &gt; 64000 create a new page to continue output if (rowCount == 64000) { rowCount = 0; sheetCount++; excelDoc.Write("</table>"); excelDoc.Write(" </worksheet>"); excelDoc.Write("<worksheet ss:name='/"Sheet"' sheetcount>"); excelDoc.Write("<table>"); } excelDoc.Write("<row>"); //ID=" + rowCount + " for (int y = 0; y ", "&gt;"); XMLstring = XMLstring.Replace("" + "<data ss:type='/"String/"'>"); excelDoc.Write(XMLstring); excelDoc.Write("</data>"); 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 " + "<data ss:type='/"DateTime/"'>"); excelDoc.Write(XMLDatetoString); excelDoc.Write("</data>"); break; case "System.Boolean": excelDoc.Write("<cell ss:styleid='/"StringLiteral/"'>" + "<data ss:type='/"String/"'>"); excelDoc.Write(x[y].ToString()); excelDoc.Write("</data></cell>"); break; case "System.Int16": case "System.Int32": case "System.Int64": case "System.Byte": excelDoc.Write("<cell ss:styleid='/"Integer/"'>" + "<data ss:type='/"Number/"'>"); excelDoc.Write(x[y].ToString()); excelDoc.Write("</data></cell>"); break; case "System.Decimal": case "System.Double": excelDoc.Write("<cell ss:styleid='/"Decimal/"'>" + "<data ss:type='/"Number/"'>"); excelDoc.Write(x[y].ToString()); excelDoc.Write("</data></cell>"); break; case "System.DBNull": excelDoc.Write("<cell ss:styleid='/"StringLiteral/"'>" + "<data ss:type='/"String/"'>"); excelDoc.Write(""); excelDoc.Write("</data></cell>"); break; default: throw (new Exception(rowType.ToString() + " not handled.")); } } excelDoc.Write("</row>"); } excelDoc.Write("</table>"); excelDoc.Write(" </worksheet>"); excelDoc.Write(endExcelXML); excelDoc.Close(); } } }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics