public static class Excel
{
/// <summary>
/// 将dataset里的数据导出为Excel.
/// </summary>
/// <param name="fileName">导出时保存的文件名.</param>
/// <param name="page">页对象.</param>
/// <param name="dataset">数据集.</param>
public static void ExportExcel(string fileName, Page page, DataSet dataset)
{
try
{
page.Response.Clear();
page.Response.Buffer = false;
page.Response.Charset = "GB2312";
page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
page.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
// 指定返回的是一个不能被客户端读取的流,必须被下载
page.Response.ContentType = "application/ms-excel";
page.EnableViewState = false;
page.Response.BufferOutput = false;
page.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");
page.Response.Write(ExportTable(dataset));
page.Response.Flush();
page.Response.End();
}
catch (System.Exception)
{
}
}
/// <summary>
/// 将DataSet信息输出为string
/// </summary>
/// <param name="dataset">DataSet信息</param>
/// <returns>string格式的信息</returns>
public static string ExportTable(DataSet dataset)
{
if (dataset == null)
return string.Empty;
StringBuilder strBuilder = new StringBuilder();
foreach (DataTable table in dataset.Tables)
{
strBuilder.AppendLine("<table border=1 cellspacing=0 cellpadding=5 rules=all >");
//输出表头.
strBuilder.AppendLine("<tr style=\"font-weight: bold; white-space: nowrap;\">");
foreach (DataColumn headColumn in table.Columns)
{
strBuilder.AppendLine("<td>" + headColumn.ColumnName + "</td>");
}
strBuilder.AppendLine("</tr>");
//输出数据.
foreach (DataRow row in table.Rows)
{
strBuilder.AppendLine("<tr>");
foreach (DataColumn dataColumn in table.Columns)
{
strBuilder.AppendLine("<td style=\"mso-number-format:'\\@';\">");
strBuilder.AppendLine(row[dataColumn].ToString());
strBuilder.AppendLine("</td>");
}
strBuilder.AppendLine("</tr>");
}
strBuilder.AppendLine("</table>");
}
return strBuilder.ToString();
}
/// <summary>
/// 将dataset里的数据导出为Excel.
/// </summary>
/// <param name="fileName">导出时保存的文件名.</param>
/// <param name="page">页对象.</param>
/// <param name="dataset">数据集.</param>
public static void ExportExcel(string fileName, Page page, DataTable datatable)
{
try
{
page.Response.Clear();
page.Response.Buffer = false;
page.Response.Charset = "GB2312";
page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文;
page.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
// 指定返回的是一个不能被客户端读取的流,必须被下载
page.Response.ContentType = "application/ms-excel";
page.EnableViewState = false;
page.Response.BufferOutput = false;
page.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");
page.Response.Write(ExportTable(datatable));
page.Response.Flush();
page.Response.End();
}
catch (System.Exception)
{
}
}
/// <summary>
/// 将DataSet信息输出为string
/// </summary>
/// <param name="dataset">DataSet信息</param>
/// <returns>string格式的信息</returns>
public static string ExportTable(DataTable dataTable)
{
StringBuilder strBuilder = new StringBuilder();
strBuilder.AppendLine("<table border=1 cellspacing=0 cellpadding=5 rules=all >");
//输出表头.
strBuilder.AppendLine("<tr style=\"font-weight: bold; white-space: nowrap;\">");
foreach (DataColumn headColumn in dataTable.Columns)
{
strBuilder.AppendLine("<td>" + headColumn.ColumnName + "</td>");
}
strBuilder.AppendLine("</tr>");
//输出数据.
foreach (DataRow row in dataTable.Rows)
{
strBuilder.AppendLine("<tr>");
foreach (DataColumn dataColumn in dataTable.Columns)
{
strBuilder.AppendLine("<td style=\"mso-number-format:'\\@';\">");
strBuilder.AppendLine(row[dataColumn].ToString());
strBuilder.AppendLine("</td>");
}
strBuilder.AppendLine("</tr>");
}
strBuilder.AppendLine("</table>");
return strBuilder.ToString();
}
}