日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

一种比较兼容的Excel报表导出方法

發布時間:2025/6/17 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一种比较兼容的Excel报表导出方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
///?<summary>
????????
///?從DataSet導出Excel
????????
///?</summary>
????????
///?<param?name="ds"></param>
????????
///?<param?name="headTexts"></param>
????????
///?<param?name="fields"></param>
????????
///?<param?name="styles">樣式,如果要顯示為字符串,可設置為vnd.ms-excel.numberformat:@</param>
????????
///?<param?name="cssNames"></param>
????????public?void?ExportDataSetToExcel(DataSet?ds,?string[]?headTexts,?string[]?fields,?string[]?formats,?string[]?styles,?string?fileName)
????????{
????????????
//?檢測參數
????????????if?(ds?==?null?||?ds.Tables.Count?==?0)
????????????{
????????????????Alert(
"數據源為空!");
????????????????
return;
????????????}
????????????
if?(headTexts?==?null?||?fields?==?null?||?headTexts.Length?!=?fields.Length?||?headTexts.Length?>?ds.Tables[0].Columns.Count)
????????????{
????????????????Alert(
"參數與數據源不匹配!");
????????????????
return;
????????????}
????????????
if?(formats?!=?null)
????????????{
????????????????
if?(formats.Length?<?headTexts.Length)
????????????????{
????????????????????
string[]?tmp?=?formats;
????????????????????formats?
=?new?string[headTexts.Length];
????????????????????tmp.CopyTo(formats,?
0);
????????????????????tmp?
=?null;
????????????????}
????????????}
????????????
if?(styles?!=?null)
????????????{
????????????????
if?(styles.Length?<?headTexts.Length)
????????????????{
????????????????????
string[]?tmp?=?styles;
????????????????????styles?
=?new?string[headTexts.Length];
????????????????????tmp.CopyTo(styles,?
0);
????????????????????tmp?
=?null;
????????????????}
????????????}

????????????
if?(Common.StringUtils.IsNullOrEmpty(fileName))
????????????{
????????????????fileName?
=?string.Format("excel{0:yyyyMMdd}.xls",?DateTime.Now);
????????????}

????????????Response.Clear();?
????????????Response.Buffer?
=?true;?
????????????Response.Charset?
=?"UTF-8";??
????????????Response.AppendHeader(
"Content-Disposition",?"attachment;filename="?+?fileName);??
????????????Response.ContentEncoding?
=?System.Text.Encoding.Default;??
????????????Response.ContentType?
=?"application/ms-excel";
????????????
this.EnableViewState?=?false;
????????????Response.Write(
"<table?cellspacing='0'?rules='all'?border='1'?style='border-collapse:collapse;'>");

????????????
//?表頭
????????????Response.Write("<thead>");
????????????
foreach?(string?headText?in?headTexts)
????????????{
????????????????Response.Write(
string.Format("<th>{0}</th>",?headText));
????????????}
????????????Response.Write(
"</thead>");

????????????
foreach?(DataRow?row?in?ds.Tables[0].Rows)
????????????{
????????????????Response.Write(
"<tr>");
????????????????
for?(int?i?=?0;?i?<?headTexts.Length;?++i)
????????????????{
????????????????????Response.Write(
"<td");
????????????????????
if?(styles?!=?null?&&?Common.StringUtils.IsNullOrEmpty(styles[i])?==?false)
????????????????????{
????????????????????????Response.Write(
string.Format("?style=\"{0}\"",?styles[i]));
????????????????????}
????????????????????Response.Write(
">");
????????????????????
if?(formats?!=?null?&&?Common.StringUtils.IsNullOrEmpty(formats[i])?==?false)
????????????????????{
????????????????????????Response.Write(
string.Format(formats[i],?row[fields[i]]));
????????????????????}
????????????????????
else
????????????????????{
????????????????????????Response.Write(row[fields[i]]);
????????????????????}
????????????????????Response.Write(
"</td>");
????????????????}
????????????????Response.Write(
"</tr>");
????????????}
????????????Response.Write(
"</table>");
????????????Response.End();
????????}

轉載于:https://www.cnblogs.com/ahjxxy/archive/2009/04/01/1427206.html

總結

以上是生活随笔為你收集整理的一种比较兼容的Excel报表导出方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。