/// <summary>
/// 导出EXCEL,传入DataTable //
/// </summary>
/// <param name="page">要导出数据的页面</param>
/// <param name="dtReport">数据表</param>
/// <param name="strReportName">报表名</param>
/// <param name="isShowHead">是否导出抬头</param>
public void WriteDataSetExcel(Page page, DataTable dtReport, string strReportName)
{
try
{
// 下载时保存文档的类型
string strFileName = "attachment;filename=" + DateTime.Now.Year + "_" + DateTime.Now.Month + "_" + DateTime.Now.Day + Server.UrlPathEncode(strReportName) + ".xls";
page.Response.Clear();
page.Response.Buffer = true;
page.Response.Charset = "GB2312";
page.Response.AppendHeader("Content-Disposition", strFileName);
page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); // 设置输出流为简体中文
page.Response.ContentType = "text/plain"; // 输入文本类型TXT文件
page.EnableViewState = false;
System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
// 是否写抬头的标记。默认写
bool flag = true;
// 写入抬头
if (flag)
{
for (int m_ColumnsCount = 0; m_ColumnsCount < dtReport.Columns.Count; m_ColumnsCount++)
{
stringBuilder.Append(dtReport.Columns[m_ColumnsCount].ColumnName);
stringBuilder.Append('/t');
}
}
stringBuilder.Append('/n');
// 写入文档
for (int m_RowCount = 0; m_RowCount < dtReport.Rows.Count; m_RowCount++)
{
for (int m_RowColumnsCount = 0; m_RowColumnsCount < dtReport.Columns.Count; m_RowColumnsCount++)
{
stringBuilder.Append(dtReport.Rows[m_RowCount][dtReport.Columns[m_RowColumnsCount].ColumnName]);
stringBuilder.Append('/t');
}
stringBuilder.Append('/n');
}
//保存文档
page.Response.Write(stringBuilder.ToString());
// page.Session[PublicTestCountValue.TSDataSet] = null;
// page.Session[PublicTestCountValue.TSErrorLogDataSetName] = null;
page.Response.End();
}
catch (Exception error)
{
string strError = error.Message;
//throw new Exception(error.Message);
}
}