Welcome

首页 / 软件开发 / C# / C#中如何使用NPOI库读写Excel文件

C#中如何使用NPOI库读写Excel文件2014-10-12NPOI 是开源的 POI 项目的.NET版,可以用来读写Excel,Word,PPT文件。在处理Excel文件上,NPOI 可以同时兼容 xls 和 xlsx。官网提供了一份 Examples,给出了很多应用场景的例子,打包好的二进制文件类库,也仅有几MB,使用非常方便。

读Excel

NPOI使用HSSFWorkbook类来处理xls,XSSFWorkbook类来处理xlsx,它们都继承接口IWorkbook,因此可以通过IWorkbook来统一处理xls和xlsx格式的文件。

以下是简单的例子

public void ReadFromExcelFile(string filePath){IWorkbook wk = null;string extension = System.IO.Path.GetExtension(filePath);try{FileStream fs = File.OpenRead(filePath);if (extension.Equals(".xls")){//把xls文件中的数据写入wk中wk = new HSSFWorkbook(fs);}else{//把xlsx文件中的数据写入wk中wk = new XSSFWorkbook(fs);}fs.Close();//读取当前表数据ISheet sheet = wk.GetSheetAt(0);IRow row = sheet.GetRow(0);//读取当前行数据//LastRowNum 是当前表的总行数-1(注意)int offset = 0;for (int i = 0; i <= sheet.LastRowNum; i++){row = sheet.GetRow(i);//读取当前行数据if (row != null){//LastCellNum 是当前行的总列数for (int j = 0; j < row.LastCellNum; j++){//读取该行的第j列数据string value = row.GetCell(j).ToString();Console.Write(value.ToString() + " ");}Console.WriteLine("
");}}}catch (Exception e){//只在Debug模式下才输出Console.WriteLine(e.Message);}}
Excel中的单元格是有不同数据格式的,例如数字,日期,字符串等,在读取的时候可以根据格式的不同设置对象的不同类型,方便后期的数据处理。

//获取cell的数据,并设置为对应的数据类型public object GetCellValue(ICell cell){object value = null;try{if (cell.CellType != CellType.Blank){switch (cell.CellType){case CellType.Numeric:// Date Type的数据CellType是Numericif (DateUtil.IsCellDateFormatted(cell)){value = cell.DateCellValue;}else{// Numeric typevalue = cell.NumericCellValue;}break;case CellType.Boolean:// Boolean typevalue = cell.BooleanCellValue;break;default:// String typevalue = cell.StringCellValue;break;}}}catch (Exception){value = "";}return value;}
特别注意的是CellType中没有Date,而日期类型的数据类型是Numeric,其实日期的数据在Excel中也是以数字的形式存储。可以使用DateUtil.IsCellDateFormatted方法来判断是否是日期类型。

有了GetCellValue方法,写数据到Excel中的时候就要有SetCellValue方法,缺的类型可以自己补。

//根据数据类型设置不同类型的cellpublic void SetCellValue(ICell cell, object obj){if (obj.GetType() == typeof(int)){cell.SetCellValue((int)obj);}else if (obj.GetType() == typeof(double)){cell.SetCellValue((double)obj);}else if (obj.GetType() == typeof(IRichTextString)){cell.SetCellValue((IRichTextString)obj);}else if (obj.GetType() == typeof(string)){cell.SetCellValue(obj.ToString());}else if (obj.GetType() == typeof(DateTime)){cell.SetCellValue((DateTime)obj);}else if (obj.GetType() == typeof(bool)){cell.SetCellValue((bool)obj);}else{cell.SetCellValue(obj.ToString());}}
cell.SetCellValue()方法只有四种重载方法,参数分别是string, bool, DateTime, double, IRichTextString(公式用IRichTextString)

写Excel

以下是简单的例子,更多信息可以参见官网提供的Examples。

public void WriteToExcel(string filePath){//创建工作薄IWorkbook wb;string extension = System.IO.Path.GetExtension(filePath);//根据指定的文件格式创建对应的类//URL:http://www.bianceng.cn/Programming/csharp/201410/45750.htmif (extension.Equals(".xls")){wb = new HSSFWorkbook();}else{wb = new XSSFWorkbook();}ICellStyle style1 = wb.CreateCellStyle();//样式style1.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;//文字水平对齐方式style1.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;//文字垂直对齐方式//设置边框style1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;style1.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;style1.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;style1.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;style1.WrapText = true;//自动换行ICellStyle style2 = wb.CreateCellStyle();//样式IFont font1 = wb.CreateFont();//字体font1.FontName = "楷体";font1.Color = HSSFColor.Red.Index;//字体颜色font1.Boldweight = (short)FontBoldWeight.Normal;//字体加粗样式style2.SetFont(font1);//样式里的字体设置具体的字体样式//设置背景色style2.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Yellow.Index;style2.FillPattern = FillPattern.SolidForeground;style2.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Yellow.Index;style2.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;//文字水平对齐方式style2.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;//文字垂直对齐方式//创建一个表单ISheet sheet = wb.CreateSheet("Sheet0");//设置列宽int[] columnWidth = { 10, 10, 10, 20 };//测试数据int rowCount = 3, columnCount = 4;object[,] data = {{"列0", "列1", "列2", "列3"},{"", 400, 5.2, 6.01},{"", DateTime.Today, true, "2014-07-02"}};for (int i = 0; i < columnWidth.Length; i++){//设置列宽度,256*字符数,因为单位是1/256个字符sheet.SetColumnWidth(i, 256 * columnWidth[i]);}IRow row;ICell cell;for (int i = 0; i < rowCount; i++){row = sheet.CreateRow(i);//创建第i行for (int j = 0; j < columnCount; j++){cell = row.CreateCell(j);cell.CellStyle = j % 2 == 0 ? style1 : style2;//根据数据类型设置不同类型的cellSetCellValue(cell, data[i, j]);}}//合并单元格,如果要合并的单元格中都有数据,只会保留左上角的//CellRangeAddress(0, 2, 0, 0),合并0-2行,0-0列的单元格CellRangeAddress region = new CellRangeAddress(0, 2, 0, 0);sheet.AddMergedRegion(region);try{FileStream fs = File.OpenWrite(filePath);wb.Write(fs); //向打开的这个xls文件中写入表并保存。fs.Close();}catch (Exception e){Debug.WriteLine(e.Message);}}
如果想要设置单元格为只读或可写,可以参考这里,实际上只需要如下两个步骤:

cell.CellStyle.IsLocked = false;//设置该单元格为非锁定

sheet.ProtectSheet("password");//保护表单,password为解锁密码

cell.CellStyle.IsLocked 默认就是true,因此第2步一定要执行,才能实现锁定单元格,对于不想锁定的单元格,就一定要设置cell.CellStyle.IsLocked = false