简体   繁体   English

使用Java读取Excel工作表时,列未按顺序读取

[英]columns are not reading in sequential order while reading excel sheet using java

I have a excel sheet with data in 200 columns, to import in to DB i'm using below code 我有一个包含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++;
}

i'm excepting output to be 我除了输出是

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

but the output is 但输出是

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

its not reading in sequential order as expected I'm unable to find out where the mistake is in this code 它没有按预期顺序读取我无法找出此代码中的错误

Use JXL library 使用JXL库

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

Code example 代码示例

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());

                }
            }

I hope this will be helpfull. 我希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM