首页 > 解决方案 > 使用数据库在excel中追加数据

问题描述

我有 2 节课

1)首先用于生成excel

2)将数据附加到同一个excel

但问题是我能够生成 excel 但我无法将数据附加到它

我有一个主类,我在其中检查文件是否不存在生成文件以及是否将数据附加到它

但是当我尝试附加数据时,它会出现以下错误

Generationjava.io.IOException:无法读取整个标头;读取 0 个字节;预期 512 字节

有谁能帮忙!!!

提前致谢

  public void generateExcelFile() throws ClassNotFoundException, SQLException, IOException {
        try(  Connection connect = DriverManager.getConnection( 
                   "jdbc:mysql://localhost:3306/test" , 
                   "root" ,""
                );

                Statement statement = connect.createStatement();
                ResultSet resultSet = statement.executeQuery("select * from table );
                   FileOutputStream out = new FileOutputStream(new File("sample.xls"));
                )
        {
        Class.forName("com.mysql.jdbc.Driver");
        XSSFWorkbook workbook = new XSSFWorkbook(); 
        XSSFSheet spreadsheet = workbook.createSheet("sample");
        XSSFRow row = spreadsheet.createRow(0);
        XSSFCell cell;
        cell = row.createCell(0);
        cell.setCellValue("ID");
        cell = row.createCell(1);
        cell.setCellValue("NAME");
        cell = row.createCell(2);
        cell.setCellValue("ADDRESS");
        cell = row.createCell(3);

        int i = 1;

        while(resultSet.next()) {
           row = spreadsheet.createRow(i);
           cell = row.createCell(0);
           cell.setCellValue(resultSet.getString("id"));
           cell = row.createCell(1);
           cell.setCellValue(resultSet.getString("name"));
           cell = row.createCell(2);
           cell.setCellValue(resultSet.getString("address"));
           i++;
        }
        workbook.write(out);
       logger.info("Excel file generated!!");
     }
        catch(Exception e){
            System.out.println("Exception in Excel Generation");
        }
        }


    public void appendFile()   {
        try(  Connection connect = DriverManager.getConnection( 
               "jdbc:mysql://localhost:3306/test" , 
               "root" ,""
            );

            Statement statement = connect.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from table where date=some date after excel generation);//for eg

               FileOutputStream out = new FileOutputStream("sample.xls");
            )
    {

    Class.forName("com.mysql.jdbc.Driver");
    FileInputStream input = new FileInputStream("sample.xls");
     HSSFWorkbook workbook1 = new HSSFWorkbook(input);

     HSSFSheet sheet = workbook1.getSheetAt(0);
     int lastrow= sheet.getLastRowNum();
     System.out.println("*********lastrow************"+lastrow);
     Row row = sheet.createRow(lastrow);
     Cell cell;
     int i = lastrow;

      while(resultSet.next()) {
           row = spreadsheet.createRow(i);
           cell = row.createCell(lastrow);
           cell.setCellValue(resultSet.getString("id"));
           cell = row.createCell(lastrow+1);
           cell.setCellValue(resultSet.getString("name"));
           cell = row.createCell(lastrow+2);
           cell.setCellValue(resultSet.getString("address"));
           i++;
        }


     workbook1.write(out);
    logger.info("Excel file generated!!");
  }
    catch(Exception e){
        System.out.println("Exception in Excel Generation"+e);
    }

标签: javaapache-poi

解决方案


推荐阅读