c#NPOI使用簡1
阿新 • • 發佈:2020-07-24
在vs開發軟體中通過nuget安裝npoi
//讀取excel表格資料,並判斷表頭是否符合要求 (電腦可不安裝office) public static void ReadExcel() { string filePath = @"C:\Users\Administrator\Desktop\as.xlsx"; FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); List<IRow> rowlist = newList<IRow>(); //建立工作簿物件 XSSFWorkbook workbook = new XSSFWorkbook(fileStream); ISheet sheet = workbook.GetSheetAt(0);//獲取第一張表 for (int i = 0; i < sheet.LastRowNum; i++) { IRow row = sheet.GetRow(i); rowlist.Add(row); row= null; } IRow indexRow = rowlist[0]; rowlist.Remove(indexRow);//移除表頭,直接迴圈列表值 //迴圈表體每一行資料 for (int i = 0; i < rowlist.Count; i++) { //迴圈每一行中每個單元格資料 for (int j = 0; j < rowlist[i].Cells.Count; j++) {//判斷當前迴圈單元格資料是否符合要求 if (indexRow.GetCell(j).ToString().Contains("oid")) { Console.WriteLine(rowlist[i].GetCell(j).ToString()); } else if (indexRow.GetCell(j).ToString().Contains("卡卡西")) { Console.WriteLine(rowlist[i].GetCell(j).ToString()); } } } }
通過讀取excel表格來判斷表頭資料
NPOI安裝dll檔案下載:
連結:https://pan.baidu.com/s/1QiRVGsjr5qoLkgv64VfOKQ
提取碼:oguw