伪Excel导出新版代码

时间:2024-10-07 13:07:26
    /// <summary>
/// 导出Excel CreateExcel(ds, "统计表.xls",new int[]{6,8});
/// </summary>
/// <param name="ds">DataSet ds</param>
/// <param name="FileName">文件名称</param>
/// <param name="iarry">new int[] {6,8};</param>
public void CreateExcel(DataSet ds, string FileName,int[] iarry)
{
FileName = HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8); HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName); StringBuilder colHeaders = new StringBuilder();
StringBuilder ls_item = new StringBuilder(); DataTable dt = ds.Tables[0];
DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
int i = 0;
int cl = dt.Columns.Count; //取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))//最后一列,加n
{
colHeaders.Append(dt.Columns[i].Caption.ToString() + "\n");
}
else
{
colHeaders.Append(dt.Columns[i].Caption.ToString() + "\t");
} }
resp.Write(colHeaders.ToString());
//向HTTP输出流中写入取得的数据信息 //逐行处理数据
foreach (DataRow row in myRow)
{
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
for (i = 0; i < cl; i++)
{
if (iarry.Contains(i))
{
ls_item.Append("=\"" + Convert.ToString(row[i]) + "\"\t");
}
else
{
if (i == (cl - 1))//最后一列,加n
{
ls_item.Append(row[i].ToString() + "\n");
}
else
{
ls_item.Append(Convert.ToString(row[i]) + "\t");
}
}
} resp.Write(ls_item.ToString());
ls_item.Clear();
}
resp.End();
}

第二种

    /// <summary>
/// DataSetToExcel(this, ds, "人员信息表.xls", new string[] { "身份证", "账户" });
/// </summary>
/// <param name="page">this</param>
/// <param name="ds">DateSet</param>
/// <param name="fileName">xxx.xxx</param>
/// <param name="sarry">数字文本</param>
public void DataSetToExcel(Page page,DataSet ds,string fileName,string[] sarry )
{
page.Response.Clear();
page.Response.Buffer = true;
page.Response.Charset = "GB2312";
//page.Response.Charset = "UTF-8";
fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
page.Response.ContentType = "application/vnd.ms-excel";//设置输出文件类型为excel文件。
page.Response.ContentType = "";
page.EnableViewState = false; /// 1) 文本:vnd.ms-excel.numberformat:@
/// 2) 日期:vnd.ms-excel.numberformat:yyyy/mm/dd
/// 3) 数字:vnd.ms-excel.numberformat:#,##0.00
/// 4) 货币:vnd.ms-excel.numberformat:¥#,##0.00
/// 5) 百分比:vnd.ms-excel.numberformat: #0.00% StringBuilder sb = new StringBuilder();
int count = 0;
foreach (DataTable tb in ds.Tables)
{
sb.AppendLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">");
sb.AppendLine("<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">"); sb.AppendLine("<tr style=\"font-weight: bold; white-space: nowrap;\">");
foreach (DataColumn column in tb.Columns)
{
sb.AppendLine("<td>" + column.ColumnName + "</td>");
}
sb.AppendLine("</tr>"); foreach (DataRow row in tb.Rows)
{
sb.Append("<tr>");
foreach (DataColumn column in tb.Columns)
{
if (sarry.Contains(column.ColumnName))
sb.Append("<td style=\"vnd.ms-excel.numberformat:@\">" + row[column].ToString() + "</td>");
else
sb.Append("<td>" + row[column].ToString() + "</td>");
}
sb.AppendLine("</tr>");
count++;
}
sb.AppendLine("</table>");
} page.Response.Write(sb.ToString());
page.Response.End();
}