java - 使用数据库在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);
}
解决方案
推荐阅读
- bash - 管道输入到 pwdx
- php - 在 php 中显示 3 种图像的逻辑问题
- go - 不同的链接行为取决于 go 二进制文件的运行方式
- javascript - 如何在这个登录页面模拟 XSS
- node.js - Node.js | WHATWG URL Query Parameter Key Parsing - 如何让它不被解析?
- postgresql - 在 postgres 中使用 COPY 将表导出到 csv 文件并建议减少 csv 文件大小
- string - Delphi:如何从字符串中获取 *real* ASCII 码
- javascript - 如何在 React with data (Hooks) 中添加动态字段
- python - 递归地将图像文件转换到另一个目标文件夹
- php - Codeigniter 'order_by' 自动按 'id' 添加排序