POI ,Java 操作 Excel 实现行的插入(insert row)

POI ,Java 操作 Excel 实现行的插入(insert row)

前几天,正在做一个项目,主要用 POI 来操作 Excel

其中,要使用一个,插入功能。主要是因为从数据库,返回结果集(数据明细),来动态车生成新行,插入明细

在网上找了找,没有找到,好的方法

自己写了一个:

  public static void insertRow(HSSFWorkbook wb, HSSFSheet sheet, int starRow,int rows) {

  sheet.shiftRows(starRow + 1, sheet.getLastRowNum(), rows,true,false);
//  Parameters:
//   startRow - the row to start shifting
//   endRow - the row to end shifting
//   n - the number of rows to shift
//   copyRowHeight - whether to copy the row height during the shift
//   resetOriginalRowHeight - whether to set the original row's height to the default
  
  starRow = starRow - 1;

  for (int i = 0; i < rows; i++) {

   HSSFRow sourceRow = null;
   HSSFRow targetRow = null;
   HSSFCell sourceCell = null;
   HSSFCell targetCell = null;
   short m;

   starRow = starRow + 1;
   sourceRow = sheet.getRow(starRow);
   targetRow = sheet.createRow(starRow + 1);
   targetRow.setHeight(sourceRow.getHeight());

   for (m = sourceRow.getFirstCellNum(); m < sourceRow.getLastCellNum(); m++) {

    sourceCell = sourceRow.getCell(m);
    targetCell = targetRow.createCell(m);

    targetCell.setEncoding(sourceCell.getEncoding());
    targetCell.setCellStyle(sourceCell.getCellStyle());
    targetCell.setCellType(sourceCell.getCellType());

   }
  }

 }

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Java遍历树形Excel并将数据插入数据库可以使用Apache POI库来读取Excel文件,并使用递归的方式遍历树形结构。以下是一个简单的示例代码: 1. 首先,我们需要导入Apache POI库的依赖。可以在Maven项目添加以下依赖项: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> ``` 2. 然后,我们可以编写一个方法来递归遍历树形Excel,并将数据插入数据库。假设Excel的每一代表一个节点,其包含节点的ID、名称和父节点的ID信息。 ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File; import java.io.FileInputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class ExcelToDatabase { public static void main(String[] args) { String excelFilePath = "path/to/excel.xlsx"; try { FileInputStream inputStream = new FileInputStream(new File(excelFilePath)); Workbook workbook = new XSSFWorkbook(inputStream); // 获取根节点 Sheet sheet = workbook.getSheetAt(0); Row rootRow = sheet.getRow(0); // 连接数据库 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/database", "username", "password"); PreparedStatement stmt = con.prepareStatement("INSERT INTO table_name (id, name, parent_id) VALUES (?, ?, ?)"); // 递归遍历树形结构并插入数据库 traverseTree(sheet, rootRow, 1, stmt); stmt.close(); con.close(); workbook.close(); inputStream.close(); System.out.println("数据插入数据库成功!"); } catch (Exception e) { e.printStackTrace(); } } private static void traverseTree(Sheet sheet, Row currentNode, int level, PreparedStatement stmt) throws SQLException { if (currentNode != null) { // 获取节点相关信息 int id = (int)currentNode.getCell(0).getNumericCellValue(); String name = currentNode.getCell(1).getStringCellValue(); int parentId = (int)currentNode.getCell(2).getNumericCellValue(); // 将节点信息插入数据库 stmt.setInt(1, id); stmt.setString(2, name); stmt.setInt(3, parentId); stmt.executeUpdate(); // 遍历子节点 for (Row row : sheet) { Cell cell = row.getCell(2); // 获取父节点ID所在的列 if (cell != null && cell.getCellType() == CellType.NUMERIC && (int)cell.getNumericCellValue() == id) { traverseTree(sheet, row, level + 1, stmt); } } } } } ``` 在以上代码,我们首先使用`FileInputStream`读取Excel文件,并使用`XSSFWorkbook`创建一个`Workbook`对象。然后,我们获取根节点所在的,连接到数据库,并使用`PreparedStatement`预编译插入语句。接下来,我们通过递归方式遍历Excel的每一,将节点信息逐插入数据库。最后,关闭相关的资源,并输出插入成功的消息。 请注意,以上代码仅提供了一个简单的示例,具体的实现取决于Excel文件的结构和数据库的设计。你可能需要根据实际情况进适当的修改和调整。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值