zoukankan      html  css  js  c++  java
  • poi读取excel模板,填充内容并导出,支持导出2007支持公式自动计算

    /**
     * 版权所有(C) 2016 
     * @author www.xiongge.club
     * @date 2016-12-7 上午10:03:29 
     */
    package xlsx;
    
    /** 
     * @ClassName: CreateExcel 
     * @Description: TODO() 
     * @author www.xiongge.club
     * @date 2016-12-7 上午10:03:29 
     *  
     */

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;

    
    

    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;
    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    
    

    /**
    * @author Gerrard
    * @Discreption 根据已有的Excel模板,修改模板内容生成新Excel
    */
    public class CreateExcel {

    
    

    /**
    *
    *(2003 xls后缀 导出)
    * @param TODO
    * @return void 返回类型
    * @author xsw
    * @2016-12-7上午10:44:00
    */
    public static void createXLS() throws IOException{
    //excel模板路径
    File fi=new File("D:\offer_template.xls");
    POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fi));
    //读取excel模板
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    //读取了模板内所有sheet内容
    HSSFSheet sheet = wb.getSheetAt(0);

    //如果这行没有了,整个公式都不会有自动计算的效果的
    sheet.setForceFormulaRecalculation(true);


    //在相应的单元格进行赋值
    HSSFCell cell = sheet.getRow(11).getCell(6);//第11行 第6列
    cell.setCellValue(1);
    HSSFCell cell2 = sheet.getRow(11).getCell(7);
    cell2.setCellValue(2);
    sheet.getRow(12).getCell(6).setCellValue(12);
    sheet.getRow(12).getCell(7).setCellValue(12);
    //修改模板内容导出新模板
    FileOutputStream out = new FileOutputStream("D:/export.xls");
    wb.write(out);
    out.close();
    }
    /**
    *
    *(2007 xlsx后缀 导出)
    * @param TODO
    * @return void 返回类型
    * @author xsw
    * @2016-12-7上午10:44:30
    */
    public static void createXLSX() throws IOException{
    //excel模板路径
    File fi=new File("D:\offer_template.xlsx");
    InputStream in = new FileInputStream(fi);
    //读取excel模板
    XSSFWorkbook wb = new XSSFWorkbook(in);
    //读取了模板内所有sheet内容
    XSSFSheet sheet = wb.getSheetAt(0);

    //如果这行没有了,整个公式都不会有自动计算的效果的
    sheet.setForceFormulaRecalculation(true);


    //在相应的单元格进行赋值
    XSSFCell cell = sheet.getRow(11).getCell(6);//第11行 第6列
    cell.setCellValue(1);
    XSSFCell cell2 = sheet.getRow(11).getCell(7);
    cell2.setCellValue(2);
    sheet.getRow(12).getCell(6).setCellValue(12);
    sheet.getRow(12).getCell(7).setCellValue(12);
    //修改模板内容导出新模板
    FileOutputStream out = new FileOutputStream("D:/export.xlsx");
    wb.write(out);
    out.close();
    }
    public static void main(String[] args) throws IOException {
    //excle 2003
    createXLS();
    //excle 2007
    createXLSX();
    }
    }

     

    模板风格不变,只是填充内容,生成新excel,支持xls 和 xlsx。

    The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)

    //如果这行没有了,整个公式都不会有自动计算的效果的
    sheet.setForceFormulaRecalculation(true);

  • 相关阅读:
    Oracle数据库实例的启动及关闭
    SCJP之赋值
    fileupload组件之上传与下载的页面
    commons-fileupload-1.2.1.jar 插件上传与下载
    SCJP读书之知识点:
    filter
    抽象abstract
    搞定导致CPU爆满的“罪魁祸首”
    优化一个小时不出结果的SQL
    最具戏剧性的分析诊断案例——十分钟锁定数据库性能“元凶”
  • 原文地址:https://www.cnblogs.com/xionggeclub/p/6140164.html
Copyright © 2011-2022 走看看