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字尾 導出)
* @2016-12-7上午10:44:30
public static void createxlsx() throws ioexception{
file fi=new file("d:\\offer_template.xlsx");
inputstream in = new fileinputstream(fi);
xssfworkbook wb = new xssfworkbook(in);
xssfsheet sheet = wb.getsheetat(0);
xssfcell cell = sheet.getrow(11).getcell(6);//第11行 第6列
xssfcell cell2 = sheet.getrow(11).getcell(7);
fileoutputstream out = new fileoutputstream("d:/export.xlsx");
public static void main(string[] args) throws ioexception {
//excle 2003
createxls();
//excle 2007
createxlsx();