k94314517
2024-02-26 5c7741b3a6c84379b0ac43cfc464e85085c8bc33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//package com.doumee.core.utils;
//
//import java.awt.image.BufferedImage;
//import java.io.ByteArrayOutputStream;
//import java.io.File;
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.net.URL;
//import java.util.stream.IntStream;
//import javax.imageio.ImageIO;
//import org.apache.poi.ss.usermodel.Cell;
//import org.apache.poi.ss.usermodel.Row;
//import org.apache.poi.ss.usermodel.Sheet;
//import org.apache.poi.ss.usermodel.Workbook;
//import org.apache.poi.ss.usermodel.WorkbookFactory;
//import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
//import org.apache.poi.xssf.usermodel.XSSFDrawing;
//import org.apache.poi.xssf.usermodel.XSSFGraphicFrame;
//import org.apache.poi.xssf.usermodel.XSSFShape;
//import org.apache.poi.xssf.usermodel.XSSFSheet;
//import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//import com.itextpdf.text.BaseColor;
//import com.itextpdf.text.Document;
//import com.itextpdf.text.DocumentException;
//import com.itextpdf.text.Element;
//import com.itextpdf.text.Font;
//import com.itextpdf.text.Image;
//import com.itextpdf.text.PageSize;
//import com.itextpdf.text.Paragraph;
//import com.itextpdf.text.pdf.PdfPCell;
//import com.itextpdf.text.pdf.PdfPTable;
//import com.itextpdf.text.pdf.PdfWriter;
//
//public class TestUtil {
//    public static void main(String[] args) {
//        try (Workbook workbook = WorkbookFactory.create(new File("example.xlsx"));
//            FileOutputStream fos = new FileOutputStream("example.pdf")) {
//            Document document = new Document(PageSize.A4, 50, 50, 50, 50);
//            PdfWriter writer = PdfWriter.getInstance(document, fos);
//            document.open();
//            for (Sheet sheet : workbook) {
//                PdfPTable table = new PdfPTable(sheet.getRow(0).getLastCellNum());
//                table.setHeaderRows(1);
//                table.setWidths(IntStream.range(0, sheet.getRow(0).getLastCellNum()).mapToDouble(i -> 1).toArray());
//                table.setWidthPercentage(100);
//                Paragraph title = new Paragraph(sheet.getSheetName(), new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 16, Font.BOLD));
//                title.setAlignment(Element.ALIGN_CENTER);
//                document.add(title);
//                sheet.forEach(row -> {
//                    IntStream.range(0, row.getLastCellNum()).forEach(i -> {
//                        Cell cell = row.getCell(i);
//                        PdfPCell pdfCell = null;
//                        try {
//                            pdfCell = new PdfPCell(new Paragraph(cell.getStringCellValue(), new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 12)));
//                        } catch (DocumentException e) {
//                            throw new RuntimeException(e);
//                        } catch (IOException e) {
//                            throw new RuntimeException(e);
//                        }
//                        pdfCell.setBorderWidth(1f);
//                        pdfCell.setBorderColor(BaseColor.BLACK);
//                        pdfCell.setPadding(5f);
//                        if (row.getRowNum() == 0) {
//                            pdfCell.setBackgroundColor(BaseColor.LIGHT_GRAY);
//                        }
//                        table.addCell(pdfCell);
//                        if (cell.getCellType() == CellType.BLANK || cell.getCellType() == CellType.FORMULA || cell.getCellType() == CellType.ERROR || cell.getCellType() == CellType.STRING && cell.getStringCellValue().trim().isEmpty() || cell.getCellType() == CellType.NUMERIC && cell.getNumericCellValue() == 0 || cell.getCellType() == CellType.BOOLEAN && !cell.getBooleanCellValue() || cell.getCellType() == CellType.STRING && cell.getStringCellValue().startsWith("#") || cell.getCellType() == CellType.FORMULA && cell.getCellFormula().startsWith("#") || cell.getCellType() == CellType.STRING && cell.getStringCellValue().startsWith("=") || cell.getCellType() == CellType.FORMULA && cell.getCellFormula().startsWith("=") || cell.getCellType() == CellType.STRING && cell.getStringCellValue().startsWith("@") || cell.getCellType() == CellType.FORMULA && cell.getCellFormula().startsWith("@") || cell.getCellType() == CellType.STRING && cell.getStringCellValue().startsWith("'") || cell.getCellType() == CellType.FORMULA && cell.getCellFormula().startsWith("'")) {
//                            return;
//                        }
//                        if (cell.getCellType() == CellType.STRING && cell.getStringCellValue().startsWith("http")) {
//                            try {
//                                BufferedImage image = ImageIO.read(new URL(cell.getStringCellValue()));
//                                if (image != null) {
//                                    addImageToTable(table, image);
//                                }
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
//                        } else {
//                            if (sheet instanceof XSSFSheet) {
//                                XSSFSheet xssfSheet = (XSSFSheet) sheet;
//                                XSSFDrawing drawing = xssfSheet.createDrawingPatriarch();
//                                for (XSSFShape shape : drawing.getShapes()) {
//                                    if (shape instanceof XSSFClientAnchor) {
//                                        XSSFClientAnchor anchor = (XSSFClientAnchor) shape.getAnchor();
//                                        if (anchor.getRow1() == cell.getRowIndex() && anchor.getCol1() == cell.getColumnIndex()) {
//                                            if (shape instanceof XSSFGraphicFrame) {
//                                                XSSFGraphicFrame frame = (XSSFGraphicFrame) shape;
//                                                BufferedImage image = frame.getGraphic().getBufferedImage();
//                                                try {
//                                                    addImageToTable(table, image);
//                                                } catch (Exception e) {
//                                                    throw new RuntimeException(e);
//                                                }
//                                            }
//                                        }
//                                    }
//                                }
//                            }
//                        }
//                    });
//                });
//                table.setSpacingBefore(20f);
//                table.setSpacingAfter(20f);
//                table.setKeepTogether(true);
//                document.add(table);
//            }
//            document.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    private static void addImageToTable(PdfPTable table, BufferedImage image) throws Exception {
//        ByteArrayOutputStream baos = new ByteArrayOutputStream();
//        ImageIO.write(image, "png", baos);
//        baos.flush();
//        byte[] imageData = baos.toByteArray();
//        baos.close();
//        Image pdfImage = Image.getInstance(imageData);
//        pdfImage.scaleToFit(100f, 100f);
//        PdfPCell imageCell = new PdfPCell(pdfImage, true);
//        imageCell.setBorderWidth(1f);
//        imageCell.setBorderColor(BaseColor.BLACK);
//        imageCell.setPadding(5f);
//        table.addCell(imageCell);
//    }
//}