需要将 excel sheet 中的特定列存储在 java 中
Need to store a particular column from excel sheet in java
我正在尝试使用 java 读取 excel sheet,我想读取第一行并在该行中找到一个特定的单词并读取该特定的列list/array 并执行进一步的计算。这是我阅读 excel sheet 的代码,我使用了 poi apache 库。
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelSheetReader {
public static void main(String[] args) throws IOException {
System.out.println("Working Directory = " + System.getProperty("user.dir"));
String excelFilePath = "book.xlsx";
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();
while (iterator.hasNext()) {
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if(cell.getStringCellValue() == "Mitesh") { // why this is not working?
columnIndex = cell.getColumnIndex();
System.out.println("\nCELL: " + columnIndex);
}
if(nextRow.getRowNum() > 0){ //To filter column headings
if(cell.getColumnIndex() == columnIndex) {// cell.getColumnIndex() == 1){// To match column index: columnIndex, cell.getStringCellValue() == "Coverge"
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
columnData.add(cell.getNumericCellValue()+"");
break;
case Cell.CELL_TYPE_STRING:
columnData.add(cell.getStringCellValue());
break;
}
}
}
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "POP");
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "PIP");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "PEP");
break;
}
//System.out.print(" - ");
}
System.out.println();
}
workbook.close();
inputStream.close();
}
}
实际问题是什么?
注意:您不是在阅读公式值...为此您需要类似以下内容:
int cellType = (cell.getCellType() == Cell.CELL_TYPE_FORMULA)
? cell.getCachedFormulaResultType() : cell.getCellType();
switch (cellType) {
...
default: System.out.println("Unhandeled cell type " + cellType);
}
if(cell.getStringCellValue() == "Mitesh") { // why this is not working?
这一行有两个问题。
1. string.equals("Mitesh")
2. 你必须在调用 .getStringCellValue()
之前检查单元格的类型
这应该有效:
private String[] getColumnAsArray(Sheet sheet, String header) {
List<String> values=new ArrayList<>();
int columnOfIntrest=-1;
//find the interresting column
for(Cell cell:sheet.getRow(sheet.getFirstRowNum())){
if(cell.getCellType()==Cell.CELL_TYPE_STRING &&
cell.getStringCellValue().equals("Mitesh")) { //this is working
columnOfIntrest = cell.getColumnIndex();
break;
}
}
//nothing found
if(columnOfIntrest<0)
return null;
Iterator<Row> rowIter=sheet.iterator();
if(rowIter.hasNext())
rowIter.next(); //skip the first row
while(rowIter.hasNext()){
Cell currCell=rowIter.next().getCell(columnOfIntrest);
switch (currCell.getCellType()) {
case Cell.CELL_TYPE_STRING:
values.add(currCell.getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN:
values.add(Boolean.toString(currCell.getBooleanCellValue()));
break;
case Cell.CELL_TYPE_NUMERIC:
values.add(Double.toString(currCell.getNumericCellValue()));
break;
}
}
return values.toArray(new String[values.size()]);
}
我正在尝试使用 java 读取 excel sheet,我想读取第一行并在该行中找到一个特定的单词并读取该特定的列list/array 并执行进一步的计算。这是我阅读 excel sheet 的代码,我使用了 poi apache 库。
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelSheetReader {
public static void main(String[] args) throws IOException {
System.out.println("Working Directory = " + System.getProperty("user.dir"));
String excelFilePath = "book.xlsx";
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();
while (iterator.hasNext()) {
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if(cell.getStringCellValue() == "Mitesh") { // why this is not working?
columnIndex = cell.getColumnIndex();
System.out.println("\nCELL: " + columnIndex);
}
if(nextRow.getRowNum() > 0){ //To filter column headings
if(cell.getColumnIndex() == columnIndex) {// cell.getColumnIndex() == 1){// To match column index: columnIndex, cell.getStringCellValue() == "Coverge"
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
columnData.add(cell.getNumericCellValue()+"");
break;
case Cell.CELL_TYPE_STRING:
columnData.add(cell.getStringCellValue());
break;
}
}
}
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "POP");
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "PIP");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "PEP");
break;
}
//System.out.print(" - ");
}
System.out.println();
}
workbook.close();
inputStream.close();
}
}
实际问题是什么?
注意:您不是在阅读公式值...为此您需要类似以下内容:
int cellType = (cell.getCellType() == Cell.CELL_TYPE_FORMULA)
? cell.getCachedFormulaResultType() : cell.getCellType();
switch (cellType) {
...
default: System.out.println("Unhandeled cell type " + cellType);
}
if(cell.getStringCellValue() == "Mitesh") { // why this is not working?
这一行有两个问题。 1. string.equals("Mitesh") 2. 你必须在调用 .getStringCellValue()
之前检查单元格的类型这应该有效:
private String[] getColumnAsArray(Sheet sheet, String header) {
List<String> values=new ArrayList<>();
int columnOfIntrest=-1;
//find the interresting column
for(Cell cell:sheet.getRow(sheet.getFirstRowNum())){
if(cell.getCellType()==Cell.CELL_TYPE_STRING &&
cell.getStringCellValue().equals("Mitesh")) { //this is working
columnOfIntrest = cell.getColumnIndex();
break;
}
}
//nothing found
if(columnOfIntrest<0)
return null;
Iterator<Row> rowIter=sheet.iterator();
if(rowIter.hasNext())
rowIter.next(); //skip the first row
while(rowIter.hasNext()){
Cell currCell=rowIter.next().getCell(columnOfIntrest);
switch (currCell.getCellType()) {
case Cell.CELL_TYPE_STRING:
values.add(currCell.getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN:
values.add(Boolean.toString(currCell.getBooleanCellValue()));
break;
case Cell.CELL_TYPE_NUMERIC:
values.add(Double.toString(currCell.getNumericCellValue()));
break;
}
}
return values.toArray(new String[values.size()]);
}