ASP.NET将DataSet导出Excel


public class ExcelExporter 
      { 
          private DataSet _dataSet = null; 
          /// <summary> 
          /// 要导出的DataSet 
          /// </summary> 
          public DataSet DataSet 
          { 
              get { return _dataSet; } 
          } 
          /// <summary> 
          /// 构造一个ExcelExporter 
          /// </summary> 
          public ExcelExporter() 
          { 
              _dataSet = new DataSet(); 
          } 
          /// <summary> 
          /// 构造一个ExcelExporter 
          /// </summary> 
          /// <param name="dataSet">要导出的DataSet</param> 
          public ExcelExporter(DataSet dataSet) 
          { 
              _dataSet = dataSet; 
          } 
          /// <summary> 
          /// 生成Excel文件 
          /// </summary> 
          /// <param name="fileName">文件名</param> 
          public void Export(string fileName) 
          { 
              if (fileName == null) 
              { 
                  throw new NullReferenceException("fileName"); 
              } 
              string excelXML = CreateExcelXML(_dataSet); 
              StreamWriter streamWriter = new StreamWriter(fileName, false, Encoding.Unicode); 
              streamWriter.Write(excelXML.ToString()); 
              streamWriter.Close(); 
          } 
          /// <summary> 
          /// 生成Excel文件 
          /// </summary> 
          /// <param name="stream">流</param> 
          public void Export(Stream stream) 
          { 
              if (stream == null) 
              { 
                  throw new NullReferenceException("stream"); 
              } 
              string excelXML = CreateExcelXML(_dataSet); 
              StreamWriter streamWriter = new StreamWriter(stream, Encoding.Unicode); 
              streamWriter.Write(excelXML.ToString()); 
              streamWriter.Close(); 
          } 
          private string CreateExcelXML(DataSet dataSet) 
          { 
              if (dataSet == null) 
              { 
                  throw new NullReferenceException("dataSet"); 
              } 
              if (dataSet.Tables.Count == 0) 
              { 
                  throw new InvalidOperationException("DataSet中至少要包含一个DataTable。"); 
              } 
              StringBuilder excelXML = new StringBuilder(); 
              // XML头 
              excelXML.AppendLine("<?xml version=\"1.0\"?>"); 
              // 工作簿 
              excelXML.AppendLine("<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\">"); 
              // 工作表 
              foreach (DataTable dataTable in dataSet.Tables) 
              { 
                  excelXML.Append("\t"); 
                  excelXML.AppendLine("<Worksheet ss:Name=\"" + TextToXML(dataTable.TableName) + "\">"); 
                  excelXML.Append("\t\t"); 
                  excelXML.AppendLine("<Table>"); 
                  // 表头 
                  excelXML.Append("\t\t\t"); 
                  excelXML.AppendLine("<Row>"); 
                  foreach (DataColumn dataColumn in dataTable.Columns) 
                  { 
                      excelXML.Append("\t\t\t\t"); 
                      excelXML.AppendLine("<Cell>"); 
                      excelXML.Append("\t\t\t\t\t"); 
                      excelXML.AppendLine("<Data ss:Type=\"String\">" + TextToXML(dataColumn.ColumnName) + "</Data>"); 
                      excelXML.Append("\t\t\t\t"); 
                      excelXML.AppendLine("</Cell>"); 
                  } 
                  excelXML.Append("\t\t\t"); 
                  excelXML.AppendLine("</Row>"); 
                  // 表体 
                  foreach (DataRow dataRow in dataTable.Rows) 
                  { 
                      excelXML.Append("\t\t\t"); 
                      excelXML.AppendLine("<Row>"); 
                      // 单元格 
                      foreach (DataColumn dataColumn in dataTable.Columns) 
                      { 
                          excelXML.Append("\t\t\t\t"); 
                          excelXML.AppendLine("<Cell>"); 
                          excelXML.Append("\t\t\t\t\t"); 
                          excelXML.AppendLine("<Data ss:Type=\"String\">" + TextToXML(dataRow[dataColumn].ToString()) + "</Data>"); 
                          excelXML.Append("\t\t\t\t"); 
                          excelXML.AppendLine("</Cell>"); 
                      } 
                      excelXML.Append("\t\t\t"); 
                      excelXML.AppendLine("</Row>"); 
                  } 
                  excelXML.Append("\t\t"); 
                  excelXML.AppendLine("</Table>"); 
                  excelXML.Append("\t"); 
                  excelXML.AppendLine("</Worksheet>"); 
              } 
              excelXML.Append("</Workbook>"); 
              return excelXML.ToString(); 
          } 
          private string TextToXML(string value) 
          { 
              value = value.Replace("&", "&"); 
              value = value.Replace("\"", """); 
              value = value.Replace("<", "<"); 
              value = value.Replace(">", ">"); 
              return value; 
          } 
      } 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值