ZBLOG

import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import com.itextpdf.text.BadElementException; import com.itextpdf.text....

这段代码中使用了((Sheet) cell).getDrawingPatriarch()来获取单元格中图片的绘图区域对象,但是这里的cell实际上应该是一个Picture类型的单元格。所以需要修改成(HSSFPicture)cell来获取绘图区域对象。

另外,在处理图片时,可以使用POI提供的API来获取图片数据和MIME类型,而不是直接从单元格中读取。因为可能会存在一些特殊情况下无法正确读取图片数据的问题。

优化后的代码如下:

  1. import java.awt.image.BufferedImage;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import javax.imageio.ImageIO;
  10. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  11. import org.apache.poi.ss.usermodel.Cell;
  12. import org.apache.poi.ss.usermodel.Row;
  13. import org.apache.poi.ss.usermodel.Sheet;
  14. import org.apache.poi.ss.usermodel.Workbook;
  15. import com.itextpdf.text.BadElementException;
  16. import com.itextpdf.text.Document;
  17. import com.itextpdf.text.Element;
  18. import com.itextpdf.text.Font;
  19. import com.itextpdf.text.Image;
  20. import com.itextpdf.text.Paragraph;
  21. import com.itextpdf.text.Rectangle;
  22. import com.itextpdf.text.pdf.PdfPCell;
  23. import com.itextpdf.text.pdf.PdfWriter;
  24. public class ExcelToPdfConverter {
  25. public static void main(String[] args) throws Exception {
  26. // Read the Excel file
  27. FileInputStream inputStream = new FileInputStream(new File("C:\\Users\\hspcadmin\\Desktop\\测试\\秦皇岛银行资管系统对接ECIF测试案例.xls"));
  28. Workbook workbook = new HSSFWorkbook(inputStream);
  29. Sheet sheet = workbook.getSheetAt(0);
  30. // Create the PDF document
  31. Document document = new Document();
  32. PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File("C:\\Users\\hspcadmin\\Desktop\\测试\\output.pdf")));
  33. document.open();
  34. // Set up fonts for the PDF document
  35. Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
  36. Font headerFont = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD);
  37. Font cellFont = new Font(Font.FontFamily.COURIER, 10);
  38. // Process the Excel data
  39. for (int i=0; i<sheet.getLastRowNum()+1; i++) {
  40. Row row = sheet.getRow(i);
  41. if (row == null) {
  42. continue; // skip blank rows
  43. }
  44. PdfPTable table = new PdfPTable(row.getLastCellNum());
  45. table.setWidthPercentage(100);
  46. for (int j=0; j<row.getLastCellNum(); j++) {
  47. Cell cell = row.getCell(j);
  48. if (cell == null) {
  49. PdfPCell pdfCell = new PdfPCell();
  50. pdfCell.addElement(new Paragraph("", cellFont));
  51. table.addCell(pdfCell);
  52. continue; // skip blank cells
  53. }
  54. PdfPCell pdfCell = new PdfPCell();
  55. switch (cell.getCellTypeEnum()) {
  56. case STRING:
  57. String value = cell.getStringCellValue();
  58. Paragraph paragraph = new Paragraph(value, cellFont);
  59. pdfCell.addElement(paragraph);
  60. break;
  61. case NUMERIC:
  62. double numericValue = cell.getNumericCellValue();
  63. Paragraph numericParagraph = new Paragraph(Double.toString(numericValue), cellFont);
  64. pdfCell.addElement(numericParagraph);
  65. break;
  66. case BOOLEAN:
  67. boolean boolValue = cell.getBooleanCellValue();
  68. Paragraph boolParagraph = new Paragraph(Boolean.toString(boolValue), cellFont);
  69. pdfCell.addElement(boolParagraph);
  70. break;
  71. case FORMULA:
  72. if (cell.getCachedFormulaResultTypeEnum() == CellType.STRING) {
  73. String formulaValue = cell.getStringCellValue();
  74. Paragraph formulaParagraph = new Paragraph(formulaValue, cellFont);
  75. pdfCell.addElement(formulaParagraph);
  76. } else if (cell.getCachedFormulaResultTypeEnum() == CellType.NUMERIC) {
  77. double formulaNumericValue = cell.getNumericCellValue();
  78. Paragraph formulaNumericParagraph = new Paragraph(Double.toString(formulaNumericValue),
  79. cellFont);
  80. pdfCell.addElement(formulaNumericParagraph);
  81. }
  82. break;
  83. case BLANK:
  84. // Do nothing
  85. break;
  86. default:
  87. // Handle all other cases using the same approach as above
  88. String otherValue=cell.getStringCellValue();
  89. Paragraph otherPara=new Paragraph(otherValue,cellFont);
  90. pdfCell.addElement(otherPara);
  91. }
  92. table.addCell(pdfCell);
  93. // Handle images separately using ImageIO library
  94. if (cell.getCellTypeEnum() == CellType.PICTURE && ((HSSFPicture)cell).getPictureData().getData() != null) {
  95. byte[] pictureData = workbook.getAllPictures().get(((HSSFPicture)cell).getPictureIndex()-1).getData();
  96. InputStream inputStream = new ByteArrayInputStream(pictureData);
  97. BufferedImage image = ImageIO.read(inputStream);
  98. Image img = getImageFromBufferedImage(image);
  99. PdfPCell imageCell = getPdfPCellFromImage(img);
  100. table.addCell(imageCell);
  101. }
  102. }
  103. // Add the table to the PDF document
  104. document.add(table);
  105. }
  106. // Save the PDF document
  107. document.close();
  108. }
  109. // 从BufferedImage获取iText中的Image
  110. private static Image getImageFromBufferedImage(BufferedImage image) throws BadElementException, IOException {
  111. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  112. ImageIO.write(image, "png", baos);
  113. byte[] imageData = baos.toByteArray();
  114. return Image.getInstance(imageData);
  115. }
  116. // 从Image获取PdfPCell,设置单元格属性和样式
  117. private static PdfPCell getPdfPCellFromImage(Image img) {
  118. PdfPCell cell = new PdfPCell(img, true);
  119. cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置水平居中
  120. cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
  121. cell.setBorder(Rectangle.NO_BORDER); // 去除边框线条
  122. return cell;
  123. }
  124. }

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=5149

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?