asp.net mvc利用NPOI导入导出Excel
导出Excel 2003没有问题,导出Excel2007老是出现无法访问已关闭的流,请帮忙解决,或是哪位有mvc导入导出excel的工具类能提供,谢谢!
public static MemoryStream ExportToExcel(DataSet ds,ExcelType excelType,string header)
{
IWorkbook workbook = null;
if (excelType==ExcelType.Office2003)
{
workbook = new HSSFWorkbook();//2003
}
else
{
workbook = new XSSFWorkbook();//2007
}
#region 开始循环DS中的Table,DS中的每个表创建一个Sheet
for (int p = 0; p
{
#region 创建一个sheet
ISheet sheet = workbook.CreateSheet("sheet" + (p + 1));
//设置大标题行
int rowCount = 0;
//设置全局列宽和行高
sheet.DefaultColumnWidth = 14; //全局列宽
sheet.DefaultRowHeightInPoints = 15; //全局行高
//设置标题行数据
int a = 0;
IRow row1 = sheet.CreateRow(rowCount); //创建报表表头标题列
//for (int k = 0; k
//{ //将DataTable的列标题输出到Excel
// columnName = ds.Tables[p].Columns[k].ColumnName;
// row1.CreateCell(a).SetCellValue(columnName);
// a++;
//}
string[] columnHeaders = header.Split(new char[] {','});//按逗号拆分标题
var style = SetCellBorder(workbook);
for (int k = 0; k
{ //将传递过来的字符串表头进行拆分到Excel
string columnName = columnHeaders[k];
ICell cell=row1.CreateCell(a);
cell.SetCellValue(columnName);
#region 设置单元格的边框
cell.CellStyle = style;
#endregion
a++;