Excel 导出通用类

时间:2023-03-09 03:02:12
Excel 导出通用类
  public class ExportToExcelHelper
{
public static void ExportExcel(DataTable dt)
{
try
{
//创建一个工作簿
IWorkbook workbook = new HSSFWorkbook(); //创建一个 sheet 表
ISheet sheet = workbook.CreateSheet("数据"); //创建一行
IRow rowH = sheet.CreateRow(); //创建一个单元格
ICell cell = null; //创建单元格样式
ICellStyle cellStyle = workbook.CreateCellStyle(); //创建格式
IDataFormat dataFormat = workbook.CreateDataFormat(); //设置为文本格式,也可以为 text,即 dataFormat.GetFormat("text");
cellStyle.DataFormat = dataFormat.GetFormat("@"); //设置列名
foreach (DataColumn col in dt.Columns)
{
//创建单元格并设置单元格内容
rowH.CreateCell(col.Ordinal).SetCellValue(col.Caption); //设置单元格格式
rowH.Cells[col.Ordinal].CellStyle = cellStyle;
} //写入数据
for (int i = ; i < dt.Rows.Count; i++)
{
//跳过第一行,第一行为列名
IRow row = sheet.CreateRow(i + ); for (int j = ; j < dt.Columns.Count; j++)
{
cell = row.CreateCell(j);
cell.SetCellValue(dt.Rows[i][j].ToString());
cell.CellStyle = cellStyle;
}
} //设置导出文件路径
string path = HttpContext.Current.Server.MapPath("/Export/"); //设置新建文件路径及名称
string savePath = path + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xls"; //创建文件
FileStream file = new FileStream(savePath, FileMode.CreateNew, FileAccess.Write); //创建一个 IO 流
MemoryStream ms = new MemoryStream(); //写入到流
workbook.Write(ms); //转换为字节数组
byte[] bytes = ms.ToArray(); file.Write(bytes, , bytes.Length);
file.Flush(); //还可以调用下面的方法,把流输出到浏览器下载
OutputClient(bytes); //释放资源
bytes = null; ms.Close();
ms.Dispose(); file.Close();
file.Dispose(); workbook.Close();
sheet = null;
workbook = null;
}
catch (Exception ex)
{ }
} public static void OutputClient(byte[] bytes)
{
HttpResponse response = HttpContext.Current.Response; response.Buffer = true; response.Clear();
response.ClearHeaders();
response.ClearContent(); response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"))); response.Charset = "GB2312";
response.ContentEncoding = Encoding.GetEncoding("GB2312"); response.BinaryWrite(bytes);
response.Flush(); response.Close();
}
}