使用Apache POI将SQLite数据库中的Image文件和String导出到Excel中

时间:2016-04-12 18:15:05

标签: java excel sqlite apache-poi

我正在尝试使用Apache POI将我的SQLite数据库导出到Excel文件中。我可以导出所有字符串和双精度值。但是它上面有图像(Blob文件)。如何将它们一起导出到同一Excel文件中并将其导回?

以下是我实现存储String和Double类型值的代码:

        String query = "select * from CarOwnerDB";
        preparedStatement = connection.prepareStatement(query);
        rs = preparedStatement.executeQuery();

        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet("Car Owner Details");
        XSSFRow header = sheet.createRow(0);

        header.createCell(0).setCellValue("Name");
        header.createCell(1).setCellValue("Username");
        header.createCell(2).setCellValue("Password");
        header.createCell(3).setCellValue("Company ID");
        header.createCell(4).setCellValue("NID");
        header.createCell(5).setCellValue("Bank Account Number");
        header.createCell(6).setCellValue("Bank Name");
        header.createCell(7).setCellValue("Permanent Address");
        header.createCell(8).setCellValue("Present Address");
        header.createCell(9).setCellValue("Phone");
        header.createCell(10).setCellValue("Email");
        header.createCell(11).setCellValue("Date Of Birth");
        header.createCell(12).setCellValue("Age");
        header.createCell(13).setCellValue("Gender");
        header.createCell(14).setCellValue("Maritial Status");
        header.createCell(15).setCellValue("Profile Picture");
        header.createCell(16).setCellValue("Balance");

        int index = 1;

        while (rs.next())
        {
            XSSFRow row = sheet.createRow(index);
            row.createCell(0).setCellValue(rs.getString("name"));
            row.createCell(1).setCellValue(rs.getString("username"));
            row.createCell(2).setCellValue(rs.getString("password"));
            row.createCell(3).setCellValue(rs.getString("comID"));
            row.createCell(4).setCellValue(rs.getString("nid"));
            row.createCell(5).setCellValue(rs.getString("bankAccountNumber"));
            row.createCell(6).setCellValue(rs.getString("bankName"));
            row.createCell(7).setCellValue(rs.getString("permanentAddress"));
            row.createCell(8).setCellValue(rs.getString("presentAddress"));
            row.createCell(9).setCellValue(rs.getString("phone"));
            row.createCell(10).setCellValue(rs.getString("email"));
            row.createCell(11).setCellValue(rs.getString("dob"));
            row.createCell(12).setCellValue(rs.getInt("age"));
            row.createCell(13).setCellValue(rs.getString("gender"));
            row.createCell(14).setCellValue(rs.getString("maritialStatus"));
            row.createCell(15).setCellValue(rs.getBlob("profilePicture"));
            row.createCell(16).setCellValue(rs.getDouble("balance"));

            index++;
        }

        FileOutputStream fout = new FileOutputStream(
                System.getProperty("user.home") + "/Desktop/" + "Car Owner Database.xlsx");
        wb.write(fout);
        fout.close();

        preparedStatement.close();
        rs.close();

在第

  

row.createCell(15).setCellValue(rs.getBlob( “profilePicture”));

我有图像文件。如何在同一个Excel工作表上存储和检索它们?

现在我知道如何使用SQL blob字段中的图像数据获取字节数组。但是不知道如何使用循环和数组来存储它们(为了存储字符串值,我使用了“XSSFRow row = sheet.createRow(index)”)。 现在,我可以通过实现此代码来存储一个图像文件:

        InputStream is = new FileInputStream("a.png");
        byte[] bytes = IOUtils.toByteArray(is);
        int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
        is.close();
        CreationHelper helper = wb.getCreationHelper();
        XSSFSheet sheet1 = wb.createSheet("Car Owner Pictures");
        Drawing drawing = sheet1.createDrawingPatriarch();
        ClientAnchor anchor = helper.createClientAnchor();
        anchor.setCol1(3);
        anchor.setRow1(2);
        Picture pict = drawing.createPicture(anchor, pictureIdx);
        pict.resize();

        String FileName = "Car Owner Database.xlsx";
        FileOutputStream fout = new FileOutputStream(Main.getDirectory() + "/" + FileName);
        wb.write(fout);
        fout.close();

如何添加多张图片(我创建的行号相同)?

0 个答案:

没有答案