[英]How to get row data without using formula cells in apache poi
我正在開發一個程序,其中excel工作表中的行數據應顯示為輸出。 將id作為輸入,它將映射到特定行以顯示輸出。 我在Java上使用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匹配,則打印整行; 解決方法如下(根據工作表中的數據,可能需要添加一些null檢查,但通常它按“原樣”運行):
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.