C#NPOI操作Excel
生活随笔
收集整理的這篇文章主要介紹了
C#NPOI操作Excel
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C#NPOI
- 一、NPOI的引用
- 二、操作Excel
- 1.創建新的Excel工作簿
- 2.讀取現有的Excel工作簿
- 3.操作sheet工作表
- 4.操作workbook工作簿
- 5.跨workbook工作簿復制sheet工作表
編輯器:Visual Studio
語言:C#
一、NPOI的引用
1.根據Visual Studio版本安裝NuGet.Tools
網址:https://www.nuget.org/downloads
2.工具→NuGet包管理器→程序包管理器控制臺
3.輸入"Install-Package NPOI",回車
4.此時會發現項目下的“引用”多了NPOI相關引用
二、操作Excel
1.創建新的Excel工作簿
IWorkbook workbook = new XSSFWorkbook(); ISheet worksheet = workbook.CreateSheet("工作表名稱");說明:
XSSFWorkbook操作擴展名為“.xlsx”的工作簿;
HSSFWorkbook操作擴展名為“.xls”的工作簿;
可使用IWorkbook統一定義;
如果需要多次創建workbook、worksheet等變量,可定義為公共變量,不用多次定義。
2.讀取現有的Excel工作簿
獲取Excel工作簿
FileStream fs; fs = File.Open(path);//path:讀取文件的路徑 if (Path.GetExtension(strPath) == ".xls") { workbook = new HSSFWorkbook(fs); } else { workbook = new XSSFWorkbook(fs); }獲取sheet工作表
worksheet = workbook.GetSheetAt(0);//0即為工作簿中的第一張工作表3.操作sheet工作表
在新工作表中插入值,首先需要創建第一行,然后創建單元格并賦值
IRow row = worksheet.CreatRow(0);//創建行 row.CreateCell(0).SetCellValue("行1列1的單元格的值"); //第二行代碼和下面兩行代碼效果相同 //ICell cell = row.CreateCell(0); //cell.SetCellValue("行1列1的單元格的值");獲取工作表最后一行
row = worksheet.GetRow(worksheet.LastRowNum); //worksheet.LastRowNum指最后一行的索引獲取行的最后一個單元格
ICell cell = row.GetCell(row.LastCellNum); //row.LastCellNum指此行最后一個單元格的索引設置單元格格式:居中(可以此推導其他屬性使用)
ICellStyle cellstyle = workbook.CreateCellStyle(); //水平居中 cellstyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; //垂直居中 cellstyle.VerticalAlignment = VerticalAlignment.Center; //自動換行 cellstyle.WrapText = true; //把定義好的cellstyle賦值給單元格 row.GetCell(0).CellStyle = cellstyle;設置列寬:worksheet.SetColumnWidth的第一個參數是要設置的列索引,第二個參數是要設置的寬度(若設置為24個字符寬度,則需25624;若設置為1個字符寬度,則需2561)
4.操作workbook工作簿
若sheet工作表個數不為1,但是想要只保留第一個工作表
if (workbook.NumberOfSheets != 1) { for (int i = workbook.NumberOfSheets - 1; i > 0; i--) { workbook.RemoveSheetAt(i); } }修改sheet工作表名字
workbook.SetSheetName(a, "名字");//a:sheet索引5.跨workbook工作簿復制sheet工作表
//復制合并的單元格 public static void MergerRegion(ISheet fromSheet, ISheet toSheet) {int sheetMergerCount = fromSheet.NumMergedRegions;for (int i = 0; i < sheetMergerCount; i++) { toSheet.AddMergedRegion(fromSheet.GetMergedRegion(i)); } } //復制sheet public static void MyCopySheet(IWorkbook fromWorkbook, IWorkbook toWorkbook, ISheet fromSheet, ISheet toSheet) {MergerRegion(fromSheet, toSheet);//合并的單元格IRow fromRow, toRow;ICellStyle fromCellStyle, toCellStyle;CellType fromCellType;int Column_Num = -1;for (int i = 0; i <= fromSheet.LastRowNum; i++){if (fromSheet.GetRow(i) != null){fromRow = fromSheet.GetRow(i);toRow = toSheet.CreateRow(i);for (int j = 0; j <= fromRow.LastCellNum; j++){if (fromRow.GetCell(j) != null){#region 內容fromCellType = fromRow.GetCell(j).CellType;toRow.CreateCell(j).SetCellType(fromRow.GetCell(j).CellType);if (fromCellType == CellType.Numeric) { toRow.GetCell(j).SetCellValue(fromRow.GetCell(j).NumericCellValue); }switch (fromCellType){case CellType.Boolean: toRow.GetCell(j).SetCellValue(fromRow.GetCell(j).BooleanCellValue); break;case CellType.Numeric: if (HSSFDateUtil.IsCellDateFormatted(fromRow.GetCell(j))){ toRow.GetCell(j).SetCellValue(fromRow.GetCell(j).DateCellValue); }else { toRow.GetCell(j).SetCellValue(fromRow.GetCell(j).NumericCellValue); }break;case CellType.String: toRow.GetCell(j).SetCellValue(fromRow.GetCell(j).RichStringCellValue); break;case CellType.Formula: toRow.GetCell(j).SetCellValue(fromRow.GetCell(j).CellFormula); break;case CellType.Error: toRow.GetCell(j).SetCellValue(fromRow.GetCell(j).ErrorCellValue); break;}#endregion#region 復制單元格格式fromCellStyle = fromRow.GetCell(j).CellStyle;toCellStyle = toWorkbook.CreateCellStyle();if (fromCellStyle != null){toCellStyle.CloneStyleFrom(fromCellStyle); toRow.GetCell(j).CellStyle = toCellStyle;}#endregion}}if (fromRow.LastCellNum > Column_Num) { Column_Num = fromRow.LastCellNum; }}}for (int i = 0; i < Column_Num; i++){toSheet.SetColumnWidth(i, fromSheet.GetColumnWidth(i));//列寬} }總結
以上是生活随笔為你收集整理的C#NPOI操作Excel的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常用搜索引擎使用技巧
- 下一篇: 三国豪杰不怕失业