How to get the Excel Cell value by address (A1, A2) using NPOI

一曲冷凌霜 提交于 2019-12-22 08:05:14

问题


I have an Excel Cell Address like A1, A2. How do I access this cell programmatically using NPOI framework in C#?

Some Java POI example code I found:

CellReference cr = new CellReference("A1"); 
row = mySheet.getRow(cr.getRow()); 
cell = row.getCell(cr.getCol());

回答1:


The Java code you found translates 1:1 into C#:

  • First you convert the cell description (A1) into a CellReference
  • Use the Row and Col from that CellReference to lookup the actual cell.

Here is some sample code

var workbook = new XSSFWorkbook(stream);
var sheet = workbook.GetSheetAt(0);

var cr = new CellReference("D5");
var row = sheet.GetRow(cr.Row);
var cell = row.GetCell(cr.Col);

Console.Write(cell.StringCellValue);

Do note that referencing an empty Cell will result in an exception. Excel doesn't store non-used cells internally, and (N)POI does not attempt to hide that fact from the developer.



来源:https://stackoverflow.com/questions/37608477/how-to-get-the-excel-cell-value-by-address-a1-a2-using-npoi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!