ASP.NET將DataSet匯出Excel

來源:互聯網
上載者:User
  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");        }

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.