i am writing a java program to read an excel sheet (xlsx) using apache poi. I can be able to iterate over all the cells and get all the values. But i am not able to get a specific cell value, say E10. Is there any way to find this?
我正在编写一个java程序来使用apache poi读取excel表(xlsx)。我可以遍历所有单元格并获取所有值。但E10说,我无法获得特定的细胞价值。有没有办法找到这个?
Please see the code that i used for iterating over all cells.
请参阅我用于迭代所有单元格的代码。
package application;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadFromXLSX {
public static void readXLSXFile() throws IOException
{
InputStream ExcelFileToRead = new FileInputStream("C:\\Test.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);
XSSFWorkbook test = new XSSFWorkbook();
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow row;
XSSFCell cell;
Iterator rows = sheet.rowIterator();
while (rows.hasNext())
{
row=(XSSFRow) rows.next();
Iterator cells = row.cellIterator();
while (cells.hasNext())
{
cell=(XSSFCell) cells.next();
if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING)
{
System.out.print(cell.getStringCellValue()+" ");
}
else if(cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC)
{
System.out.print(cell.getNumericCellValue()+" ");
}
else
{
}
}
System.out.println();
}
}
}
5 个解决方案
#1
13
For example, to get E10 of the first worksheet:
例如,要获取第一个工作表的E10:
wb.getSheetAt(0).getRow(9).getCell(4);
Note: subtract one because the indices are null-based.
注意:减去一,因为索引是基于空的。
You can also use this convenience method to map E to 4.
您也可以使用此便捷方法将E映射到4。
wb.getSheetAt(0).getRow(9).getCell(CellReference.convertColStringToIndex("E"));
#2
2
XSSFSheet has the method getRow(int rownum) It returns the logical row ( 0-based). If you ask for a row that is not defined you get a null. This is to say row 4 represents the fifth row on a sheet.
XSSFSheet的方法是getRow(int rownum)它返回逻辑行(从0开始)。如果您要求未定义的行,则会得到null。这就是说第4行代表工作表上的第五行。
Once you get the row, you can call getCell(int cellnum) method of XSSFRow object. It returns the cell at the given (0 based) index.
获得行后,可以调用XSSFRow对象的getCell(int cellnum)方法。它返回给定(基于0)索引的单元格。
#3
1
public class XmlFileRead {
public static void main(String[] args) throws IOException {
FileInputStream fi = new FileInputStream("abc.xls");
ArrayList<EmployeeVo> al = new ArrayList<>();
EmployeeVo evo = null;
Scanner scanner = null;
Workbook wb = new XSSFWorkbook(fi);
Sheet sh = wb.getSheet("Sheet0");
int starRow = sh.getFirstRowNum();
int endRow = sh.getLastRowNum();
for (int i = starRow + 1; i < endRow; i++) {
scanner = new Scanner(System.in);
evo = new EmployeeVo();
Cell c = wb.getSheetAt(0).getRow(i).getCell(1);
evo.setEmployeeId((int) c.getNumericCellValue());
Cell c2 = wb.getSheetAt(0).getRow(i).getCell(2);
evo.setEmployeeName(c2.toString());
// add to collection
al.add(evo);
} // for
al.forEach(i -> {
System.out.println(i.getEmployeeId() + " " + i.getEmployeeName());
});
}
}
#4
0
To get a value from a specific cell in excel you can use the below code line.
要从excel中的特定单元格获取值,可以使用以下代码行。
wb.getSheetAt(0).getRow(1).getCell(1);
#5
0
Just version-up the getCell
method
只需对getCell方法进行版本升级
public XSSFCell getCell(string cellName){
Pattern r = Pattern.compile("^([A-Z]+)([0-9]+)$");
Matcher m = r.matcher(cellName);
if(m.matches()) {
columnName = m.group(1);
rowNumber = Integer.parseInt(m.group(2));
if(rowNumber > 0) {
return wb.getSheetAt(0).getRow(rowNumber-1).getCell(CellReference.convertColStringToIndex(columnName))
}
}
// throw an exception or return a null
}
Now you can get the cell easily by this line
现在,您可以通过此行轻松获取单元格
getCell("E10")
#1
13
For example, to get E10 of the first worksheet:
例如,要获取第一个工作表的E10:
wb.getSheetAt(0).getRow(9).getCell(4);
Note: subtract one because the indices are null-based.
注意:减去一,因为索引是基于空的。
You can also use this convenience method to map E to 4.
您也可以使用此便捷方法将E映射到4。
wb.getSheetAt(0).getRow(9).getCell(CellReference.convertColStringToIndex("E"));
#2
2
XSSFSheet has the method getRow(int rownum) It returns the logical row ( 0-based). If you ask for a row that is not defined you get a null. This is to say row 4 represents the fifth row on a sheet.
XSSFSheet的方法是getRow(int rownum)它返回逻辑行(从0开始)。如果您要求未定义的行,则会得到null。这就是说第4行代表工作表上的第五行。
Once you get the row, you can call getCell(int cellnum) method of XSSFRow object. It returns the cell at the given (0 based) index.
获得行后,可以调用XSSFRow对象的getCell(int cellnum)方法。它返回给定(基于0)索引的单元格。
#3
1
public class XmlFileRead {
public static void main(String[] args) throws IOException {
FileInputStream fi = new FileInputStream("abc.xls");
ArrayList<EmployeeVo> al = new ArrayList<>();
EmployeeVo evo = null;
Scanner scanner = null;
Workbook wb = new XSSFWorkbook(fi);
Sheet sh = wb.getSheet("Sheet0");
int starRow = sh.getFirstRowNum();
int endRow = sh.getLastRowNum();
for (int i = starRow + 1; i < endRow; i++) {
scanner = new Scanner(System.in);
evo = new EmployeeVo();
Cell c = wb.getSheetAt(0).getRow(i).getCell(1);
evo.setEmployeeId((int) c.getNumericCellValue());
Cell c2 = wb.getSheetAt(0).getRow(i).getCell(2);
evo.setEmployeeName(c2.toString());
// add to collection
al.add(evo);
} // for
al.forEach(i -> {
System.out.println(i.getEmployeeId() + " " + i.getEmployeeName());
});
}
}
#4
0
To get a value from a specific cell in excel you can use the below code line.
要从excel中的特定单元格获取值,可以使用以下代码行。
wb.getSheetAt(0).getRow(1).getCell(1);
#5
0
Just version-up the getCell
method
只需对getCell方法进行版本升级
public XSSFCell getCell(string cellName){
Pattern r = Pattern.compile("^([A-Z]+)([0-9]+)$");
Matcher m = r.matcher(cellName);
if(m.matches()) {
columnName = m.group(1);
rowNumber = Integer.parseInt(m.group(2));
if(rowNumber > 0) {
return wb.getSheetAt(0).getRow(rowNumber-1).getCell(CellReference.convertColStringToIndex(columnName))
}
}
// throw an exception or return a null
}
Now you can get the cell easily by this line
现在,您可以通过此行轻松获取单元格
getCell("E10")