要在Object中显示csv / xlsx文件的内容

要在Object.I中显示csv / xlsx文件的内容,我想在一个Object中显示文件的每一行。 以下是我的代码。 我想创build一个名为Item的类,并在其中存储列的名称。

package com.pack; import java.awt.List; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.commons.io.FilenameUtils; public class Final { public static void main(String[] args) throws IOException { final String extXlsx="xlsx"; final String extCSV="csv"; String ext = "C:\\Users\\swapnil.sanjay.saraf\\Desktop\\.xlsx"; List<Item> itemList = new ArrayList<>(); if(extXlsx.equals(FilenameUtils.getExtension(ext))) { String excelPath = "C:\\Users\\swapnil.sanjay.saraf\\Desktop\\ItemID.xlsx"; FileInputStream fileInputStream = new FileInputStream(new File(excelPath)); // Create Workbook instance holding .xls file XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream); // Get the first worksheet XSSFSheet sheet = workbook.getSheetAt(0); // Iterate through each rows Iterator<Row> rowIterator = sheet.iterator(); System.out.println("\n******XLSX FILE******\n"); while (rowIterator.hasNext()) { Item item; // Get Each Row Row row = rowIterator.next(); // Iterating through Each column of Each Row Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); // Checking the cell format switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: System.out.print(cell.getNumericCellValue() + "\t"); break; case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue() + "\t"); break; case Cell.CELL_TYPE_BOOLEAN: System.out.print(cell.getBooleanCellValue() + "\t"); break; } } System.out.println(""); itemList.add(item); } } else if(extCSV.equals(FilenameUtils.getExtension(ext))) { Scanner scanner = new Scanner(new File("C:\\Users\\swapnil.sanjay.saraf\\Desktop\\KitsCP.csv")); scanner.useDelimiter(","); System.out.println("\n******CSV FILE******\n"); while(scanner.hasNext()){ System.out.print(scanner.next()+" "); } scanner.close(); } else{ System.out.println("Inavlid Extension"); } } }