/// <summary>
/// 导出
/// </summary>
/// <param name="table">数据表</param>
/// <param name="SheetName">工作簿名称</param>
/// <param name="reportName">报表名称</param>
public static void Output(DataTable table, string SheetName, string reportName)
{
string result = string.Empty;
try
{
HSSFWorkbook workBook = new HSSFWorkbook();
Sheet sheet = workBook.CreateSheet(SheetName); //sheet页名称
NPOI.SS.UserModel.Font font = workBook.CreateFont();
font.FontName = "微软雅黑"; font.FontHeight = ;
CellStyle style = workBook.CreateCellStyle();
style.Alignment = HorizontalAlignment.CENTER;
style.VerticalAlignment = VerticalAlignment.CENTER;
style.SetFont(font);
style.WrapText = true; Row rows = sheet.CreateRow();
rows.Height = ;
rows.CreateCell().SetCellValue(reportName); //报表名称
rows.GetCell().CellStyle = style;
sheet.AddMergedRegion(new CellRangeAddress(, , , table.Columns.Count - )); rows = sheet.CreateRow();
for (int i = ; i < table.Columns.Count; i++)
{
rows.CreateCell(i).SetCellValue(table.Columns[i].ColumnName.ToString());
rows.Sheet.SetColumnWidth(i, );
rows.GetCell(i).CellStyle = style;
}
CellStyle style2 = workBook.CreateCellStyle();
style2.Alignment = HorizontalAlignment.LEFT;
style2.VerticalAlignment = VerticalAlignment.CENTER;
style2.SetFont(font);
style2.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
//style2.WrapText = true;
int tem = ;
int sheetCount = ;
for (int j = ; j <= table.Rows.Count; j++)
{
tem++;
if (tem == )//每页最多导出60000
{
tem = ;
sheetCount++;
sheet = workBook.CreateSheet(SheetName + sheetCount);
rows = sheet.CreateRow();
for (int i = ; i < table.Columns.Count; i++)
{
rows.CreateCell(i).SetCellValue(table.Columns[i].ColumnName.ToString());
rows.Sheet.SetColumnWidth(i, );
rows.GetCell(i).CellStyle = style;
}
}
Row row = sheet.CreateRow(tem + );
for (int k = ; k < table.Columns.Count; k++)
{
row.CreateCell(k).SetCellValue(table.Rows[j - ][k].ToString());
row.GetCell(k).CellStyle = style2;
}
#region
//Row row = sheet.CreateRow(j + 1);
//for (int k = 0; k < table.Columns.Count; k++)
//{
// row.CreateCell(k).SetCellValue(table.Rows[j - 1][k].ToString());
// row.GetCell(k).CellStyle = style2;
//}
#endregion
} int maxColumn = table.Columns.Count;
//列宽自适应,只对英文和数字有效
for (int i = ; i <= maxColumn; i++)
{
sheet.AutoSizeColumn(i);
} using (MemoryStream ms = new MemoryStream())
{
workBook.Write(ms);
ms.Flush();
ms.Position = ;
workBook = null; HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";//HttpContext.Current.Response string browser = HttpContext.Current.Request.Browser.Browser.ToString();
string header = string.Empty;
if (browser == "Firefox")
{
header = string.Format("attachment; filename={0}", string.Format("{0}-{1}.xls", reportName, DateTime.Now.ToString("yyyy-MM-dd")), Encoding.UTF8).ToString();
}
else
{
header = string.Format("attachment; filename={0}", HttpUtility.UrlEncode(string.Format("{0}-{1}.xls", reportName, DateTime.Now.ToString("yyyy-MM-dd")), Encoding.UTF8)).ToString();
}
HttpContext.Current.Response.AddHeader("Content-Disposition", header);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.BinaryWrite(ms.GetBuffer());
HttpContext.Current.Response.End();
}
}
catch
{
throw new Exception("导出异常");
}
}