标签:pen stc error 引用 finally 运行程序 mode mod pre
程序处理excel使用using Microsoft.Office.Interop.Excel方式,运行程序需要电脑安装excel,而且excel版本还需要一样,使用起来不方便。使用NPOI不用电脑安装office.
下载地址:http://npoi.codeplex.com/
下载NPOI 2.2.1 binary package.zip,解压缩里面有Net20和Net40。我使用的Net40,把里面所有的dll添加引用。
读excel
using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System.IO; private void ReadPatternExcel(string filePath,int sheetIndex) { IWorkbook workBook = null; ISheet sheet; try { FileInfo fileInfo = new FileInfo(filePath); if(fileInfo.Exists) { FileStream fs = fileInfo.OpenRead(); switch (fileInfo.Extension) { //xls是03,用HSSFWorkbook打开,.xlsx是07或者10用XSSFWorkbook打开 case ".xls": workBook = new HSSFWorkbook(fs); break; case ".xlsx": workBook = new XSSFWorkbook(fs); break; default: break; } fs.Close();//关闭文件流 } if(workBook!=null) { sheet = workBook.GetSheetAt(sheetIndex); IRow headerRow = sheet.GetRow(0); int colCount = headerRow.LastCellNum;//列数 //遍历 for(int i=sheet.FirstRowNum;i<=sheet.LastRowNum;i++) { Console.Write("row "+i.ToString()+" "); NPOI.SS.UserModel.IRow row = sheet.GetRow(i);//得到一行 for(int j=row.FirstCellNum;j<row.LastCellNum;j++) { string data = row.GetCell(j).ToString(); Console.Write(data); } Console.WriteLine(); } } } catch(Exception e) { MessageBox.Show(e.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { workBook = null; sheet = null; } }
标签:pen stc error 引用 finally 运行程序 mode mod pre
原文地址:http://www.cnblogs.com/ike_li/p/6349114.html