java读取excel隐藏列,#用poi做excle导入时怎样判断行是否隐藏#poi excle读出数据

本文介绍了使用Java POI库在处理Excel文件时如何读取隐藏列和行的问题,包括检查列是否隐藏的代码示例,并提供了处理数字类型读取、时间格式以及公式单元格的解决方案。
摘要由CSDN通过智能技术生成

急!!!java用poi导出excel文件,打开导出的文件时报错“文件错误,数据可能丢失”

我遇到过这个问题后调查发现两个原因:

1.你的excel模版本身有问题,可以尝试新建一个模版。

2.你的excel使用了一些POI不支持的函数。

java poi 读取excel 数字类型的怎么读到以后1都变成了1.0?

我也碰到了这个问题,解决很简单,就是修改Excel表的单元格格式(修改成文本)就可以,如果必须需要其他类型,那就只有在java后台手动转换了。

用poi做excle导入时怎样判断行是否隐藏

//引用下面回答码,加了判断列隐藏

//创建Excel

String path = "c:/test.xlsx";

InputStream is = new FileInputStream(path);

Workbook wb = WorkbookFactory.create(is);

Sheet sheet=wb.getSheetAt(0);

//判断列隐藏

//columnIndex指的列下标

//sheet.isColumnHidden(columnIndex) == true;

//columnIndex所属的列隐藏

int rowNum=sheet.getPhysicalNumberOfRows();

Row row=null;

//查找所有隐藏的行

for (int i = 0; i 

{

row=sheet.getRow(i)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值