使用POI将表导出为ex​​cel / word

时间:2023-02-05 20:27:57

I want the code to export tables / reports to Excel and Word using POI. I saw the examples that came with POI but could not understand. Can anyone provide me a small/easy code to do the same.

我希望代码使用POI将表/报表导出到Excel和Word。我看到POI附带的例子但无法理解。任何人都可以为我提供一个小/简单的代码来做同样的事情。

1 个解决方案

#1


Seeing as you wanted actual code for using POI. Here is some to do some exporting:

看到您想要使用POI的实际代码。这里有一些做一些导出:

import java.util.Date;
import java.util.List;
import java.util.ListIterator;
import java.util.StringTokenizer;
import java.io.*;
import org.apache.poi.hssf.usermodel.*;


public class XLSExporter implements Exporter {

    /**
     * Constructor for XLSExporter
     */
    public XLSExporter(){
    }


    public void exportFile( File f, List o ) throws IOException{

        HSSFWorkbook wb = new HSSFWorkbook();
        FileOutputStream fileOut = new FileOutputStream(f);

        HSSFSheet sheet = wb.createSheet();


        ListIterator it = o.listIterator();

        //Construct the headings
        HSSFRow headingsRow  = sheet.createRow((short)0); 


        //Heading format
        HSSFFont headingFont = wb.createFont();
        headingFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        HSSFCellStyle headingStyle = wb.createCellStyle();
        headingStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        headingStyle.setFont(headingFont);


        HSSFCell headingA = headingsRow.createCell((short)0);
        headingA.setCellValue("Heading");
        headingA.setCellStyle(headingStyle);




        int i = 1;
        // Iterate over the rows
        while(it.hasNext()){


            //Create the row
            HSSFRow row  = sheet.createRow((short)i); 

            //Write data
            HSSFCell cellRunway = row.createCell((short)0);
            cellRunway.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
            cellRunway.setCellValue("Whateva");             
            cellRunway.setCellStyle(standardStyle);



            i++;
        }

        //Set the column widths where needed
        sheet.setColumnWidth((short)1, (short)4000); 


        wb.write(fileOut); // Write the workbook
        fileOut.close();
    }

}

#1


Seeing as you wanted actual code for using POI. Here is some to do some exporting:

看到您想要使用POI的实际代码。这里有一些做一些导出:

import java.util.Date;
import java.util.List;
import java.util.ListIterator;
import java.util.StringTokenizer;
import java.io.*;
import org.apache.poi.hssf.usermodel.*;


public class XLSExporter implements Exporter {

    /**
     * Constructor for XLSExporter
     */
    public XLSExporter(){
    }


    public void exportFile( File f, List o ) throws IOException{

        HSSFWorkbook wb = new HSSFWorkbook();
        FileOutputStream fileOut = new FileOutputStream(f);

        HSSFSheet sheet = wb.createSheet();


        ListIterator it = o.listIterator();

        //Construct the headings
        HSSFRow headingsRow  = sheet.createRow((short)0); 


        //Heading format
        HSSFFont headingFont = wb.createFont();
        headingFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        HSSFCellStyle headingStyle = wb.createCellStyle();
        headingStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        headingStyle.setFont(headingFont);


        HSSFCell headingA = headingsRow.createCell((short)0);
        headingA.setCellValue("Heading");
        headingA.setCellStyle(headingStyle);




        int i = 1;
        // Iterate over the rows
        while(it.hasNext()){


            //Create the row
            HSSFRow row  = sheet.createRow((short)i); 

            //Write data
            HSSFCell cellRunway = row.createCell((short)0);
            cellRunway.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
            cellRunway.setCellValue("Whateva");             
            cellRunway.setCellStyle(standardStyle);



            i++;
        }

        //Set the column widths where needed
        sheet.setColumnWidth((short)1, (short)4000); 


        wb.write(fileOut); // Write the workbook
        fileOut.close();
    }

}