如何使用apache poi计算excel文件中的colums

时间:2022-06-15 19:12:05

I want to count columns from excel file to create columns for PDF file at PdfPTable my_table = new PdfPTable(countColums);

我想从excel文件中计算列,以便在PdfPTable创建PDF文件的列my_table = new PdfPTable(countColums);

    FileInputStream input_document = new FileInputStream(new File(
            "D:\\excel_to_pdf.xls"));
    HSSFWorkbook my_xls_workbook = new HSSFWorkbook(input_document);
    HSSFSheet my_worksheet = my_xls_workbook.getSheetAt(0);
    Iterator<Row> rowIterator = my_worksheet.iterator();
    Document iText_xls_2_pdf = new Document();
    PdfWriter.getInstance(iText_xls_2_pdf, new FileOutputStream(
            "D:\\Excel2PDF_Output.pdf"));
    iText_xls_2_pdf.open();

    PdfPTable my_table = new PdfPTable(3);
    PdfPCell table_cell;

1 个解决方案

#1


0  

Try this code

试试这个代码

    FileInputStream fis = null;
    try {
        String filename = "E:\\XLS.xls";
        fis = new FileInputStream(filename);
        HSSFWorkbook workbook = new HSSFWorkbook(fis);
        int columnCount = 0;
        HSSFSheet sheet = workbook.getSheetAt(0);
        Iterator<?> rows = sheet.rowIterator();
        while (rows.hasNext()) {
            HSSFRow row = (HSSFRow) rows.next();
            Iterator<?> cells = row.cellIterator();
            while (cells.hasNext()) {
                columnCount++;
                cells.next();
            }
            break;
        }
        System.out.println(columnCount);

    } catch (Exception e) {
        e.printStackTrace();
    }

#1


0  

Try this code

试试这个代码

    FileInputStream fis = null;
    try {
        String filename = "E:\\XLS.xls";
        fis = new FileInputStream(filename);
        HSSFWorkbook workbook = new HSSFWorkbook(fis);
        int columnCount = 0;
        HSSFSheet sheet = workbook.getSheetAt(0);
        Iterator<?> rows = sheet.rowIterator();
        while (rows.hasNext()) {
            HSSFRow row = (HSSFRow) rows.next();
            Iterator<?> cells = row.cellIterator();
            while (cells.hasNext()) {
                columnCount++;
                cells.next();
            }
            break;
        }
        System.out.println(columnCount);

    } catch (Exception e) {
        e.printStackTrace();
    }