• 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");
            }
    

      

  • 相关阅读:
    docker镜像文件导入与导出,支持批量
    配置和启动Kubernetes服务
    在CentOS 7 上安装docker
    安装CentOS7精简版后的配置工作
    Docker镜像加速
    docker命令不需要敲sudo的方法
    建立时间和保持时间(setup time 和 hold time)
    时序收敛:基本概念
    GitHub: Windows 下的简单使用
    K-means算法和矢量量化
  • 原文地址:https://www.cnblogs.com/malianyong/p/2595278.html
Copyright © 2020-2023  润新知