gpt4 book ai didi

java - 如何使用 apache poi 从 excel 获取值(value)

转载 作者:行者123 更新时间:2023-12-02 05:36:50 34 4
gpt4 key购买 nike

我只是尝试从 Excel 文件中获取每个单元格值,但每个单元格已经合并,但我希望每个单元格不同

import java.io.File;import java.io.FileInputStream;import java.util.Iterator;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;public class Read {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

try
{
FileInputStream file = new FileInputStream(new File("D://new/excelnew/student_usr_mst_dtls.xlsx"));

//Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);

//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);

//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
//For each row, iterate through all the columns

Iterator<Cell> cellIterator = row.cellIterator();

while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
//Check the cell type and format accordingly
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());
break;
}
}
System.out.println("");
}
file.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}

}

OutPut 就像字符串

IDNAME姓氏1.0t阿米特·舒克拉2.0t洛克什古普塔

我希望每个单元格都是唯一的,那么如何做到这一点。如果可以的话请举个例子。

最佳答案

在打印的每个单元格值后放置一个逗号。

System.out.print(cell.getNumericCellValue() + "t,");

System.out.print(cell.getStringCellValue() + ",");

关于java - 如何使用 apache poi 从 excel 获取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24890513/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com