0

below is my code to convert excel to pdf, but i dont understand how do i generate multiple pdf from multiple excel sheets.

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;

I want something that can help me create a folder full of pdfs.

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
  • What isn't working with this code? What are you expecting it to do that it doesn't? – Gagravarr Apr 14 '15 at 21:46
  • PdfWriter writer = PdfWriter.getInstance(iText_xls_2_pdf, new FileOutputStream("D:\\Output.pdf")); iText_xls_2_pdf.open(); this part should be in loop to generate multiple pdfs or every time creating new object for pdf would be better ? – Neha Thawani Apr 15 '15 at 05:38

0 Answers0