WEB开发网
开发学院数据库MSSQL Server DataSet导出到Excel比较完整的解决方案(一)--客... 阅读

DataSet导出到Excel比较完整的解决方案(一)--客户端生成文件(downmoon)

 2009-01-16 10:19:16 来源:WEB开发网   
核心提示: 原来,就是纯粹的html格式,DataSet导出到Excel比较完整的解决方案(一)--客户端生成文件(downmoon)(3),披了件Excel的外衣,这样用户传上来的文件当然不是标准的Excel格式了! 于是

原来,就是纯粹的html格式,披了件Excel的外衣。这样用户传上来的文件当然不是标准的Excel格式了!

于是, 想到直接生成xml格式的Excel文档,方法如下 :

ExportToExcel
/**//// <summary>
    /// 执行导出
    /// </summary>
    /// <param name="ds">要导出的DataSet</param>
    /// <param name="strExcelFileName">要导出的文件名</param>
    public static void ExportToExcel(DataSet source, string fileName)
    {
  
      System.IO.StreamWriter excelDoc;
  
      excelDoc = new System.IO.StreamWriter(fileName);
      const string startExcelXML = "<xml version>rn<Workbook " +
         "xmlns="urn:schemas-microsoft-com:office:spreadsheet"rn" +
         " 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 <Styles>rn " +
         "<Style ss:ID="Default" ss:Name="Normal">rn " +
         "<Alignment ss:Vertical="Bottom"/>rn <Borders/>" +
         "rn <Font/>rn <Interior/>rn <NumberFormat/>" +
         "rn <Protection/>rn </Style>rn " +
         "<Style ss:ID="BoldColumn">rn <Font " +
         "x:Family="Swiss" ss:Bold="1"/>rn </Style>rn " +
         "<Style   ss:ID="StringLiteral">rn <NumberFormat" +
         " ss:Format="@"/>rn </Style>rn <Style " +
         "ss:ID="Decimal">rn <NumberFormat " +
         "ss:Format="0.0000"/>rn </Style>rn " +
         "<Style ss:ID="Integer">rn <NumberFormat " +
         "ss:Format="0"/>rn </Style>rn <Style " +
         "ss:ID="DateLiteral">rn <NumberFormat " +
         "ss:Format="mm/dd/yyyy;@"/>rn </Style>rn " +
         "</Styles>rn ";
      const string endExcelXML = "</Workbook>";
  
      int rowCount = 0;
      int sheetCount = 1;
      /**//*
      <xml version>
      <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">
      <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 ss:ID="Decimal">
       <NumberFormat ss:Format="0.0000"/>
      </Style>
      <Style ss:ID="Integer">
       <NumberFormat ss:Format="0"/>
      </Style>
      <Style ss:ID="DateLiteral">
       <NumberFormat ss:Format="mm/dd/yyyy;@"/>
      </Style>
      </Styles>
      <Worksheet ss:Name="Sheet1">
      </Worksheet>
      </Workbook>
      */
      excelDoc.Write(startExcelXML);
      excelDoc.Write("<Worksheet ss:Name="Sheet" + sheetCount + "">");
      excelDoc.Write("<Table>");
      excelDoc.Write("<Row>");
      for (int x = 0; x < source.Tables[0].Columns.Count; x++)
      {
        excelDoc.Write("<Cell ss:StyleID="BoldColumn"><Data ss:Type="String">");
        excelDoc.Write(source.Tables[0].Columns[x].ColumnName);
        excelDoc.Write("</Data></Cell>");
      }
      excelDoc.Write("</Row>");
      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("</Table>");
          excelDoc.Write(" </Worksheet>");
          excelDoc.Write("<Worksheet ss:Name="Sheet" + sheetCount + "">");
          excelDoc.Write("<Table>");
        }
        excelDoc.Write("<Row>"); //ID=" + rowCount + "
  
        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("<Cell ss:StyleID="StringLiteral">" +
                      "<Data ss:Type="String">");
              excelDoc.Write(XMLstring);
              excelDoc.Write("</Data></Cell>");
              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("<Cell ss:StyleID="DateLiteral">" +
                     "<Data ss:Type="DateTime">");
              excelDoc.Write(XMLDatetoString);
              excelDoc.Write("</Data></Cell>");
              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();
    }

上一页  1 2 3 4 5 6  下一页

Tags:DataSet 导出 Excel

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接