【问题标题】:java program to read excel file and create an array to insert it to database tablejava程序读取excel文件并创建一个数组以将其插入数据库表
【发布时间】:2017-12-01 09:49:04
【问题描述】:

我正在编写一个 java 程序,借助 apache poi 从 excel 文件中读取。我想编写一个程序,从 excel 创建对象数组,并更新数据库。

我可以在 apache poi 的帮助下读取 excel 文件(使用提供的示例),但是在读取 excel 文件后,我不确定如何在包含所有数据的 java 中创建数组。 第一行代表表字段(即列名:EX name,id,etc..)

1) 逻辑应该足够聪明,即使在 excel 文件中顺序颠倒/更改,也可以检测列。 表格示例:


代码:

XSSFSheet sheet = workbook.getSheetAt(0);
    Iterator < Row >  rowIterator = sheet.iterator();
    while (rowIterator.hasNext()) {
        row = (XSSFRow) rowIterator.next();
        Iterator < Cell >  cellIterator = row.cellIterator();

        while ( cellIterator.hasNext()) {
           Cell cell = cellIterator.next();

           switch (cell.getCellType()) {
              case Cell.CELL_TYPE_NUMERIC:
              System.out.print( 
              cell.getNumericCellValue() + " \t\t " );
              break;

              case Cell.CELL_TYPE_STRING:
              /*System.out.print(
              cell.getStringCellValue() + " \t\t " );
              */break;
           }
        }
        System.out.println();
     }

【问题讨论】:

  • 将你的代码添加到这篇文章中,没有它没有人可以帮助你。另外,您是否尝试用您的 excel 文件中的数据填充数据库?
  • 你的代码在哪里????
  • 您的问题应该很清楚,包括您当前的代码 sn-p..请避免在未在此处显示您的代码工作的情况下询问直接逻辑
  • 我正在提交我的代码,请稍等
  • 如果要将excel文件中的数据插入数据库,为什么要存储在数组中呢?这只需要您编写更多代码。只需在读取文件时将数据插入数据库。

标签: java excel apache-poi


【解决方案1】:
     XSSFWorkbook workbook = new XSSFWorkbook(file);
        XSSFSheet sheet = workbook.getSheetAt(0);
        Iterator < Row >  rowIterator = sheet.iterator();
// using for loop on rows of excel sheet
        for(int i=sheet.getFirstRowNum()+1;i<=sheet.getLastRowNum();i++){
              Row ro=sheet.getRow(i);
              for(int j=ro.getFirstCellNum();j<ro.getLastCellNum();j++){
                   Cell ce = ro.getCell(j);
                   DataFormatter formatter = new DataFormatter();
                   String str = formatter.formatCellValue(ce);
            //add the logic to update the aaray         
            }

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    • 2016-01-15
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多