• DataSet导出到Excel文件


      1         public static void ExportToExcel(DataSet source, string fileName)
      2         {
      3             System.IO.StreamWriter excelDoc = new System.IO.StreamWriter(fileName);
      4             ExportToExcel(source, excelDoc);
      5         }
      6 
      7         public static void ExportToExcel(DataTable dtSource, string fileName)
      8         {
      9             DataSet dsSource = new DataSet();
     10             dsSource.Tables.Add(dtSource);
     11 
     12             ExportToExcel(dsSource, fileName);
     13         }
     14 
     15         public static void ExportToExcel(DataTable dtSource, System.IO.StreamWriter excelDoc)
     16         {
     17             DataSet dsSource = new DataSet();
     18             dsSource.Tables.Add(dtSource);
     19 
     20             ExportToExcel(dsSource, excelDoc);
     21         }
     22 
     23         public static void ExportToExcel(DataSet source, System.IO.StreamWriter excelDoc)
     24         {
     25             const string startExcelXML = "<xml version>
    <Workbook " +
     26                   "xmlns="urn:schemas-microsoft-com:office:spreadsheet"
    " +
     27                   " xmlns:o="urn:schemas-microsoft-com:office:office"
     " +
     28                   "xmlns:x="urn:schemas-    microsoft-com:office:" +
     29                   "excel"
     xmlns:ss="urn:schemas-microsoft-com:" +
     30                   "office:spreadsheet">
     <Styles>
     " +
     31                   "<Style ss:ID="Default" ss:Name="Normal">
     " +
     32                   "<Alignment ss:Vertical="Bottom"/>
     <Borders/>" +
     33                   "
     <Font/>
     <Interior/>
     <NumberFormat/>" +
     34                   "
     <Protection/>
     </Style>
     " +
     35                   "<Style ss:ID="BoldColumn">
     <Font " +
     36                   "x:Family="Swiss" ss:Bold="1"/>
     </Style>
     " +
     37                   "<Style     ss:ID="StringLiteral">
     <NumberFormat" +
     38                   " ss:Format="@"/>
     </Style>
     <Style " +
     39                   "ss:ID="Decimal">
     <NumberFormat " +
     40                   "ss:Format="0"/>
     </Style>
     " +
     41                   "<Style ss:ID="Integer">
     <NumberFormat " +
     42                   "ss:Format="0"/>
     </Style>
     <Style " +
     43                   "ss:ID="DateLiteral">
     <NumberFormat " +
     44                   "ss:Format="mm/dd/yyyy;@"/>
     </Style>
     " +
     45                   "</Styles>
     ";
     46             const string endExcelXML = "</Workbook>";
     47 
     48             int rowCount = 0;
     49             int sheetCount = 1;
     50             excelDoc.Write(startExcelXML);
     51             excelDoc.Write("<Worksheet ss:Name="Sheet" + sheetCount + "">");
     52             excelDoc.Write("<Table>");
     53             excelDoc.Write("<Row>");
     54             for (int x = 0; x < source.Tables[0].Columns.Count; x++)
     55             {
     56                 excelDoc.Write("<Cell ss:StyleID="BoldColumn"><Data ss:Type="String">");
     57                 excelDoc.Write(source.Tables[0].Columns[x].ColumnName);
     58                 excelDoc.Write("</Data></Cell>");
     59             }
     60             excelDoc.Write("</Row>");
     61             foreach (DataRow x in source.Tables[0].Rows)
     62             {
     63                 rowCount++;
     64                 if (rowCount == 64000)
     65                 {
     66                     rowCount = 0;
     67                     sheetCount++;
     68                     excelDoc.Write("</Table>");
     69                     excelDoc.Write(" </Worksheet>");
     70                     excelDoc.Write("<Worksheet ss:Name="Sheet" + sheetCount + "">");
     71                     excelDoc.Write("<Table>");
     72                 }
     73                 excelDoc.Write("<Row>");
     74                 for (int y = 0; y < source.Tables[0].Columns.Count; y++)
     75                 {
     76                     System.Type rowType;
     77                     rowType = x[y].GetType();
     78                     switch (rowType.ToString())
     79                     {
     80                         case "System.String":
     81                             string XMLstring = x[y].ToString();
     82                             XMLstring = XMLstring.Trim();
     83                             XMLstring = XMLstring.Replace("&", "&");
     84                             XMLstring = XMLstring.Replace(">", ">");
     85                             XMLstring = XMLstring.Replace("<", "<");
     86                             excelDoc.Write("<Cell ss:StyleID="StringLiteral">" +
     87                                            "<Data ss:Type="String">");
     88                             excelDoc.Write(XMLstring);
     89                             excelDoc.Write("</Data></Cell>");
     90                             break;
     91                         case "System.DateTime":
     92                             DateTime XMLDate = (DateTime)x[y];
     93                             string XMLDatetoString = "";
     94                             XMLDatetoString = XMLDate.Year.ToString() +
     95                                  "-" +
     96                                  (XMLDate.Month < 10 ? "0" +
     97                                  XMLDate.Month.ToString() : XMLDate.Month.ToString()) +
     98                                  "-" +
     99                                  (XMLDate.Day < 10 ? "0" +
    100                                  XMLDate.Day.ToString() : XMLDate.Day.ToString()) +
    101                                  "T" +
    102                                  (XMLDate.Hour < 10 ? "0" +
    103                                  XMLDate.Hour.ToString() : XMLDate.Hour.ToString()) +
    104                                  ":" +
    105                                  (XMLDate.Minute < 10 ? "0" +
    106                                  XMLDate.Minute.ToString() : XMLDate.Minute.ToString()) +
    107                                  ":" +
    108                                  (XMLDate.Second < 10 ? "0" +
    109                                  XMLDate.Second.ToString() : XMLDate.Second.ToString()) +
    110                                  ".000";
    111                             excelDoc.Write("<Cell ss:StyleID="DateLiteral">" +
    112                                          "<Data ss:Type="DateTime">");
    113                             excelDoc.Write(XMLDatetoString);
    114                             excelDoc.Write("</Data></Cell>");
    115                             break;
    116                         case "System.Boolean":
    117                             excelDoc.Write("<Cell ss:StyleID="StringLiteral">" +
    118                                         "<Data ss:Type="String">");
    119                             excelDoc.Write(x[y].ToString());
    120                             excelDoc.Write("</Data></Cell>");
    121                             break;
    122                         case "System.Int16":
    123                         case "System.Int32":
    124                         case "System.Int64":
    125                         case "System.Byte":
    126                             excelDoc.Write("<Cell ss:StyleID="Integer">" +
    127                                     "<Data ss:Type="Number">");
    128                             excelDoc.Write(x[y].ToString());
    129                             excelDoc.Write("</Data></Cell>");
    130                             break;
    131                         case "System.Decimal":
    132                         case "System.Double":
    133                             excelDoc.Write("<Cell ss:StyleID="Decimal">" +
    134                                   "<Data ss:Type="Number">");
    135                             excelDoc.Write(x[y].ToString());
    136                             excelDoc.Write("</Data></Cell>");
    137                             break;
    138                         case "System.DBNull":
    139                             excelDoc.Write("<Cell ss:StyleID="StringLiteral">" +
    140                                   "<Data ss:Type="String">");
    141                             excelDoc.Write("");
    142                             excelDoc.Write("</Data></Cell>");
    143                             break;
    144                         default:
    145                             throw (new Exception(rowType.ToString() + " not handled."));
    146                     }
    147                 }
    148                 excelDoc.Write("</Row>");
    149             }
    150             excelDoc.Write("</Table>");
    151             excelDoc.Write(" </Worksheet>");
    152             excelDoc.Write(endExcelXML);
    153             excelDoc.Close();
    154         }
  • 相关阅读:
    表单重复提交问题
    Win8.1卸载64位Oracle Database 11g的详细图文步骤记录
    A1084. Broken Keyboard (20)
    A1088. Rational Arithmetic (20)
    A1089. Insert or Merge (25)
    A1034. Head of a Gang (30)
    A1013. Battle Over Cities (25)
    A1030. Travel Plan (30)
    A1003. Emergency (25)
    A1076. Forwards on Weibo (30)
  • 原文地址:https://www.cnblogs.com/ziranquliu/p/4765677.html
Copyright © 2020-2023  润新知