記錄一次使用NPOI遇到的問題


在.net 下一般使用NPOI操作Excel相信大家都不陌生,但是本人在操作過程中遇到一個比較奇怪的問題,特寫此博客記錄與大家分享。

例子是使用Winform,點擊按鈕時彈出打開文件對話框,然后選擇文件來讀取Excel。

最開始代碼時這樣寫的:

 1 private void button1_Click(object sender, EventArgs e)  2  {  3             OpenFileDialog ofd = new OpenFileDialog {Filter = "excel文件|*.xls"};  4             if (ofd.ShowDialog() == DialogResult.OK)  5  {  6                 using (FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read))  7  {  8                     IWorkbook workbook = new HSSFWorkbook(fs);  9                     ISheet sheet = workbook.GetSheetAt(0); 10 
11                     //總共有多少行
12                     int lastRowNum = sheet.LastRowNum; 13                     int firstRowNum = sheet.FirstRowNum; 14 
15                     for (int i = firstRowNum + 1; i <= lastRowNum; i++) 16  { 17                         IRow row = sheet.GetRow(i); 18                         if (row == null) 19  { 20                             continue; 21  } 22                         string name = row.Cells[0]?.ToString(); 23 
24                         if (string.IsNullOrEmpty(name)) 25  { 26                             //空行
27                             continue; 28  } 29 
30                         string birthplace = row.Cells[1]?.ToString(); 31                         string major = row.Cells[2]?.ToString(); 32                         string className = row.Cells[3]?.ToString(); 33                         double height = row.Cells[4].NumericCellValue; 34                         double age = row.Cells[5].NumericCellValue; 35 
36                         Console.WriteLine($"name:{name},birthplace:{birthplace},major:{major},className:{className},height:{height},age:{age}"); 37 
38  } 39  } 40  } 41         }
View Code

然后Excel是這樣的:

 

 調試時,遇到錯誤:

 

 監視變量i,看是循環到第幾行:

 

 這里是3,也就是第三行(標題除外),第三行的內容是這樣的:

 

 這里解釋一下,這個表格使用了白色背景填充,然后前面三行(包括標題在內)使用了實線的細邊框。

再在監視里輸入代碼row.Cells.Count,獲取得到的結果是4,也就是第三行只有4“列”(這里列加了雙引號)。明明就有6列,怎么會只有4列,於是再在監視里輸入row.LastCellNum,得到的結果是6。

 

 這里可以看出有6列,我們知道獲取列有row.Cells[i] 或者是 row.GetCell(i) , 於是嘗試在監視里輸入row.GetCell(4),看是否會報錯:

 

 

發現沒有報錯,而且“值“一欄是正確的列的內容。

於是將代碼里row.Cells[i] 改成 row.GetCell(i) 的形式:

private void button1_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog {Filter = "excel文件|*.xls"}; if (ofd.ShowDialog() == DialogResult.OK) { using (FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read)) { IWorkbook workbook = new HSSFWorkbook(fs); ISheet sheet = workbook.GetSheetAt(0); //總共有多少行
                    int lastRowNum = sheet.LastRowNum; int firstRowNum = sheet.FirstRowNum; for (int i = firstRowNum + 1; i <= lastRowNum; i++) { IRow row = sheet.GetRow(i); if (row == null) { continue; } string name = row.GetCell(0)?.ToString(); if (string.IsNullOrEmpty(name)) { //空行
                            continue; } string birthplace = row.GetCell(1)?.ToString(); string major = row.GetCell(2)?.ToString(); string className = row.GetCell(3)?.ToString(); double height = row.GetCell(4).NumericCellValue; double age = row.GetCell(5).NumericCellValue; Console.WriteLine($"name:{name},birthplace:{birthplace},major:{major},className:{className},height:{height},age:{age}"); } } } }
View Code

再次調試,沒有報錯,在輸出窗口有以下的信息:

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM