org.apache.poi 读excel空列跳过的问题

用org.apache.poi的包做excel导入,无意间发现如果excel文件中有空列,空列后面的数据全部读不到。查来查去原来是HSSFRow提供两个方法:getPhysicalNumberOfCells和getLastCellNum。

getPhysicalNumberOfCells 是获取不为空的列个数。

getLastCellNum 是获取最后一个不为空的列是第几个。

同样,HSSFSheet获取行也有类似两个方法,如果excel数据中存在空行或空列,必须用getLast**的方法才能完全读取数据。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java使用POI取本地Excel文件并将数据导入到数据库中的完整代码,包括将数据存入数据库: ```java import java.io.File; import java.io.FileInputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Iterator; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelToDatabase { public static void main(String[] args) { String jdbcURL = "jdbc:mysql://localhost:3306/mydatabase"; String username = "root"; String password = "root"; String excelFilePath = "C:\\Users\\Username\\Desktop\\data.xlsx"; int batchSize = 20; Connection connection = null; try { long start = System.currentTimeMillis(); FileInputStream inputStream = new FileInputStream(new File(excelFilePath)); Workbook workbook = new XSSFWorkbook(inputStream); Sheet firstSheet = workbook.getSheetAt(0); Iterator<Row> rowIterator = firstSheet.iterator(); connection = DriverManager.getConnection(jdbcURL, username, password); connection.setAutoCommit(false); String sql = "INSERT INTO mytable (column1, column2, column3) VALUES (?, ?, ?)"; PreparedStatement statement = connection.prepareStatement(sql); int count = 0; rowIterator.next(); // skip the header row while (rowIterator.hasNext()) { Row nextRow = rowIterator.next(); Iterator<Cell> cellIterator = nextRow.cellIterator(); while (cellIterator.hasNext()) { Cell nextCell = cellIterator.next(); int columnIndex = nextCell.getColumnIndex(); switch (columnIndex) { case 0: String column1 = nextCell.getStringCellValue(); statement.setString(1, column1); break; case 1: String column2 = nextCell.getStringCellValue(); statement.setString(2, column2); break; case 2: if (nextCell.getCellType() == Cell.CELL_TYPE_NUMERIC) { double column3 = nextCell.getNumericCellValue(); statement.setDouble(3, column3); } else { String column3 = nextCell.getStringCellValue(); statement.setString(3, column3); } break; default: break; } } statement.addBatch(); if (count % batchSize == 0) { statement.executeBatch(); } } workbook.close(); // execute the remaining queries statement.executeBatch(); connection.commit(); connection.close(); long end = System.currentTimeMillis(); System.out.printf("Import done in %d ms\n", (end - start)); } catch (Exception e) { e.printStackTrace(); try { connection.rollback(); } catch (SQLException e1) { System.out.println(e1.getMessage()); } } } } ``` 在代码中,你需要将“mydatabase”和“mytable”替换为你的数据库名称和表名。在这个示例中,我们假设Excel文件中的第一行是标题行,因此我们使用`rowIterator.next()`过了它。在将数据插入数据库之前,我们可以检查的数据类型并进行相应的处理,例如,如果第三是数字,则使用`nextCell.getNumericCellValue()`取得值,否则使用`nextCell.getStringCellValue()`获取值。如果某一行中有,则我们不需要为它们设置值,因为它们将默认为null,这将在数据库中存储为NULL。注意,在添加批处理之前,你需要检查`count % batchSize == 0`,并在执行之前执行`statement.executeBatch()`。 希望这能帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值