zoukankan      html  css  js  c++  java
  • 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;
                }
            }
      protected void btnDC_Click(object sender, EventArgs e)
            {
                DataSet ds = BLL.ExamResult.SelectExamResult();
                new ExcelExporter(ds).Export(@"C:\Users\Administrator\Desktop\mly.xls");
            }
    

      

  • 相关阅读:
    函数中this指向问题及函数不同方式的调用
    拷贝继承
    组合继承
    借用构造函数
    继承
    UVA-11054(扫描法)
    hihocoder-1347 小h的树上的朋友(lca+线段树)
    UVA-10391(字符串检索)
    UVA-10125(中途相遇法)
    UVA-10827(前缀和降维)
  • 原文地址:https://www.cnblogs.com/malianyong/p/2595278.html
Copyright © 2011-2022 走看看