使用数据库在Excel中附加数据

时间:2019-02-20 10:01:09

标签: java apache-poi

我有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);
    }

0 个答案:

没有答案