gpt4 book ai didi

java - 使用java读取Excel工作表时,列不按顺序读取

转载 作者:行者123 更新时间:2023-12-01 06:08:59 25 4
gpt4 key购买 nike

我有一个包含 200 列数据的 Excel 工作表,可以导入到我正在使用的数据库中下面的代码

private File upp;
InputStream input = new FileInputStream(upp);
POIFSFileSystem fs = new POIFSFileSystem(input);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);

Iterator rows = sheet.rowIterator();

while(rows.hasNext()) {
String data="";
HSSFRow row = (HSSFRow) rows.next();
if(row.getRowNum() == 0 || row.getRowNum() == 1) {
Iterator rowCells = row.cellIterator();
while(rowCells.hasNext()) {
HSSFCell cell = (HSSFCell) rowCells.next();
for(int i=0; i <= cell.getCellNum(); i++) {

}
data = data + cell.getCellNum() + " ,";
}
System.out.println(data);
}
slrow++;
}

我的异常(exception)是输出

0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12 ,13 ,14 ,15 ,16 ,17 ,18 ,

但是输出是

0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12 ,13 ,14 ,15 ,17 ,16 ,18 ,

它没有按预期按顺序读取,我无法找出此代码中的错误所在

最佳答案

使用 JXL 库

<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6</version>
</dependency

代码示例

FileInputStream inputStream = new FileInputStream(file);
Workbook wb = Workbook.getWorkbook(inputStream);

// TO get the access to the sheet.
//For single sheet
Sheet sheet = wb.getSheet(0);

// To get the number of rows present in sheet
int totalNoOfRows = sheet.getRows();

// To get the number of columns present in sheet
int totalNoOfCols = sheet.getColumns();

Map<String, String> map = new HashMap<>();
for (int row = 1; row < totalNoOfRows; row++) {

for (int col = 0; col < totalNoOfCols; col++) {
map.put(sheet.getCell(col, 0).getContents(),
sheet.getCell(col, row).getContents());

}
}

我希望这会有所帮助。

关于java - 使用java读取Excel工作表时,列不按顺序读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38898017/

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