如何在apache poi中不使用公式单元格获取行数据
问题描述:
我正在处理一个程序,其中来自excel表格的行数据应该显示为输出。一个id被用作输入,它将映射到显示输出的特定行。我在java上使用apache poi来编写代码。我想显示输出,而不使用我目前在我的编程中使用的单元格。有没有直接打印行对象的行数据的函数?如何在apache poi中不使用公式单元格获取行数据
package exceldata;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
public class exceldata {
private static final String FILE_NAME = "C:\\Users\\agandiko\\Desktop\\data.xlsx";
public static void main(String[] args) {
try {System.out.println("Enter the id:");
Scanner s=new Scanner(System.in);
String field=s.next();
int on=0;
int n=0;
int cc=1;
FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
Workbook wb = new XSSFWorkbook(excelFile);
DataFormatter formatter = new DataFormatter();
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
System.out.println();
System.out.println();
System.out.println("Data for Sheet"+" "+(i+1));
System.out.println("-----------------------------------");
System.out.println();
System.out.println();
Sheet datatypeSheet = wb.getSheetAt(i);
Iterator<Row> iterator = datatypeSheet.iterator();
int noOfColumns = datatypeSheet.getRow(0).getPhysicalNumberOfCells();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
Cell k=currentCell;
//getCellTypeEnum shown as deprecated for version 3.15
//getCellTypeEnum ill be renamed to getCellType starting from version 4.0
String val=formatter.formatCellValue(currentCell).toString();
if((val.equals(field)))
{
while(cellIterator.hasNext()){
if(cc==1){System.out.print(formatter.formatCellValue(k).toString()+" ");}
else{currentCell = cellIterator.next();System.out.print(formatter.formatCellValue(currentCell).toString()+" ");}
cc++;
}
// System.out.print(currentRow.toString()+" ");
// currentRow = iterator.next();
// cellIterator = currentRow.iterator();
// on=1;n++;}
//System.out.println(cc);
}
// if(n==noOfColumns){System.out.println();n=0;}
//on=0;
}
cc=1; }
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
答
这就是我所理解的用例:找一些ID(代码假定它是每个表中的第一列)并打印整行,如果它的ID匹配进入;这里是如何可以解决(取决于您的工作表中的数据,一些空的检查可能需要增加,但总体上它的工作原理“原样”):
public static void main(String[] args) throws IOException {
System.out.println("Enter the id:");
Scanner s = new Scanner(System.in);
String id = s.next();
FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
Workbook wb = new XSSFWorkbook(excelFile);
DataFormatter formatter = new DataFormatter();
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
System.out.println();
System.out.println();
System.out.println("Data for Sheet" + " " + (i + 1));
System.out.println("-----------------------------------");
System.out.println();
System.out.println();
Sheet datatypeSheet = wb.getSheetAt(i);
Iterator<Row> iterator = datatypeSheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
if(id.equals(formatter.formatCellValue(currentRow.getCell(0)))) {
currentRow.cellIterator().forEachRemaining(c -> System.out.print(formatter.formatCellValue(c) + " "));
}
}
}
wb.close();
s.close();
}
+0
谢谢!你的代码是正确的 –
+0
非常好。那么请你接受关闭这个话题的答案。 – JensS
这是我不清楚你想要打印行数据(到System.out)? – JensS
是的,我想打印它在System.out –
所以你的意思是currentRow.toString()的“格式化版本”。据我所知,这是目前不可能的,最好的办法就是你做到这一点(=遍历单元格)。 – JensS