0

下面是我将 excel 转换为 pdf 的代码,但我不明白如何从多个 excel 工作表生成多个 pdf。

String files;
File folder = new File(dirpath);
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
    if (listOfFiles[i].isFile()) {
        files = listOfFiles[i].getName();

        if (files.endsWith(".xls") || files.endsWith(".xlsx")) {
            // inputting files one by one
            //here it should take an input one by one
            System.out.println(files);
            String inputR = files.toString();
            FileInputStream input_document = new FileInputStream(new File("D:\\ExcelToPdfProject\\"+inputR));

            // Read workbook into HSSFWorkbook
            Workbook workbook = null;
            if (inputR.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(input_document);
                System.out.println("1");
            } else if (inputR.endsWith(".xls")) {
                workbook = new HSSFWorkbook(input_document);
                System.out.println("GO TO HELL ######");
            } else {
                System.out.println("GO TO HELL");
            }

            Sheet my_worksheet = workbook.getSheetAt(2);
            // Read worksheet into HSSFSheet

            // To iterate over the rows
            Iterator<Row> rowIterator = my_worksheet.iterator();
            //Iterator<Row> rowIterator1 = my_worksheet.iterator();
            //We will create output PDF document objects at this point
            Document iText_xls_2_pdf = new Document();

            PdfWriter writer = PdfWriter.getInstance(iText_xls_2_pdf, new FileOutputStream("D:\\Output.pdf"));
            iText_xls_2_pdf.open();
            //we have two columns in the Excel sheet, so we create a PDF table with two columns
            //Note: There are ways to make this dynamic in nature, if you want to.

            Row row = rowIterator.next();
            row.setHeight((short) 2);

            int count = row.getPhysicalNumberOfCells();

            PdfPTable my_table = new PdfPTable(count);
            float[] columnWidths = new float[count];

            my_table.setWidthPercentage(100f);

            //We will use the object below to dynamically add new data to the table
            PdfPCell table_cell;

我想要一些可以帮助我创建一个充满 pdf 的文件夹的东西。

4

0 回答 0