【问题标题】:exporting tables to excel / word using POI使用 POI 将表格导出到 excel/word
【发布时间】:2009-05-10 15:06:43
【问题描述】:

我希望代码使用 POI 将表格/报告导出到 Excel 和 Word。 我看到了 POI 附带的示例,但无法理解。谁能给我一个小/简单的代码来做同样的事情。

【问题讨论】:

标签: java jsp


【解决方案1】:

查看您想要使用 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();
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2014-06-20
    • 2018-11-30
    相关资源
    最近更新 更多