http://www.cnblogs.com/yinrq/p/5590970.html
.NET NPOI导出Excel详解
NPOI,顾名思义,就是POI的.NET版本。那POI又是什么呢?POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office的文件。
支持的文件格式包括xls, doc, ppt等。
官方网站:http://npoi.codeplex.com/
nuget直接获取使用
一、NPOI生成Excel
//创建工作薄var workbook = new HSSFWorkbook();//创建表var table = workbook.CreateSheet("joye.net");//模拟20行20列数据for (var i = 0; i < 20; i++){var row = table.CreateRow(i);for (int j = 0; j < 20; j++){var cell = row.CreateCell(j);cell.SetCellValue(j);}}//打开xls文件,如没有则创建,如存在则在创建是不要打开该文件using (var fs = File.OpenWrite(@"c:/joye.net.xls")){workbook.Write(fs); //向打开的这个xls文件中写入mySheet表并保存。Console.WriteLine("生成成功");}
二、NPOI读取Excel
using (var fs = File.OpenRead(@"c:/joye.net.xls")) {//把xls文件中的数据写入workbook1中var workbook1 = new HSSFWorkbook(fs);for (var i = 0; i < workbook1.NumberOfSheets; i++){var sheet = workbook1.GetSheetAt(i);for (var j = 0; j <= sheet.LastRowNum; j++){ //读取当前行数据var row = sheet.GetRow(j);if (row != null){for (var k = 0; k <= row.LastCellNum; k++){ //当前表格var cell = row.GetCell(k);if (cell != null){Console.Write(cell.ToString() + " ");}}}Console.WriteLine();}}}
读出的结果
三、简单学习
学习代码
代码调用
四、NPOI导出Excel 65536问题
public static HSSFWorkbook BuildWorkbook(DataTable dt){var book = new HSSFWorkbook(); ISheet sheet = book.CreateSheet("Sheet1");//Data Rowsfor (int i = 0; i < dt.Rows.Count; i++){IRow drow = sheet.CreateRow(i);for (int j = 0; j < dt.Columns.Count; j++){ICell cell = drow.CreateCell(j, CellType.String);cell.SetCellValue(dt.Rows[i][j].ToString());}}//自动列宽for (int i = 0; i <= dt.Columns.Count; i++)sheet.AutoSizeColumn(i, true);return book;}
NPOI导出Excel超过65536会报异常,原来是由于NPOI这个动态库导致的,然后看了下版本,发现是1.2.5。然后百度了下,发现这个版本的NPOI只支持office2003,二office2003最多支持65536行
解决方式:
1、只是在插入数据的时候,加个判断,如果数据条数大于65536时,就在创建一个sheet
//65536判断处理public static HSSFWorkbook BuildWorkbook(DataTable dt){var book = new HSSFWorkbook();ISheet sheet1 = book.CreateSheet("Sheet1");ISheet sheet2 = book.CreateSheet("Sheet2");//填充数据for (int i = 0; i < dt.Rows.Count; i++){if (i < 65536){IRow drow = sheet1.CreateRow(i);for (int j = 0; j < dt.Columns.Count; j++){ICell cell = drow.CreateCell(j, CellType.String);cell.SetCellValue(dt.Rows[i][j].ToString());}}if (i >= 65536){IRow drow = sheet2.CreateRow(i - 65536);for (int j = 0; j < dt.Columns.Count; j++){ICell cell = drow.CreateCell(j, CellType.String);cell.SetCellValue(dt.Rows[i][j].ToString());}}}//自动列宽for (int i = 0; i <= dt.Columns.Count; i++){sheet1.AutoSizeColumn(i, true);sheet2.AutoSizeColumn(i, true);}return book;}
2、考虑使用高版本Office,使用用对象支持高版本的NPOI
//高版本public static XSSFWorkbook BuildWorkbook(DataTable dt){var book = new XSSFWorkbook();ISheet sheet = book.CreateSheet("Sheet1");//Data Rowsfor (int i = 0; i < dt.Rows.Count; i++){IRow drow = sheet.CreateRow(i);for (int j = 0; j < dt.Columns.Count; j++){ICell cell = drow.CreateCell(j, CellType.String);cell.SetCellValue(dt.Rows[i][j].ToString());}}//自动列宽for (int i = 0; i <= dt.Columns.Count; i++)sheet.AutoSizeColumn(i, true);return book;}
五、web开发中导出Excel
public static void ExportExcel(DataTable dt, string fileName = ""){//生成ExcelIWorkbook book = BuildWorkbook(dt);//web 下载if (fileName == "")fileName = string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now);fileName = fileName.Trim();string ext = Path.GetExtension(fileName);if (ext.ToLower() == ".xls" || ext.ToLower() == ".xlsx")fileName = fileName.Replace(ext, string.Empty);HttpResponse httpResponse = HttpContext.Current.Response;httpResponse.Clear();httpResponse.Buffer = true;httpResponse.Charset = Encoding.UTF8.BodyName;httpResponse.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");httpResponse.ContentEncoding = Encoding.UTF8;httpResponse.ContentType = "application/vnd.ms-excel; charset=UTF-8";book.Write(httpResponse.OutputStream);httpResponse.End();}
基于.xls模板生成Excel文件有时间再看
代码下载:https://yunpan.cn/cRBVnTCSchz7k (提取码:779e)