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);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM