poi讀取excel模板,填充內(nèi)容并導(dǎo)出,支持導(dǎo)出2007支持公式自動(dòng)計(jì)算
本文關(guān)鍵詞:excel模板路徑
更多相關(guān)文章: 讀取 excel 模板 填充 內(nèi)容 導(dǎo)出 支持 2007 公式 自動(dòng) 計(jì)算
poi讀取excel模板,填充內(nèi)容并導(dǎo)出,支持導(dǎo)出2007支持公式自動(dòng)計(jì)算
發(fā)表于2017/10/16 13:41:36 105人閱讀
分類: Java
/**
* 版權(quán)所有(C) 2016
* @author
* @date 2016-12-7 上午10:03:29
*/
package xlsx;
/**
* @ClassName: CreateExcel
* @Description: TODO()
* @author
* @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 根據(jù)已有的Excel模板,修改模板內(nèi)容生成新Excel
*/
public class CreateExcel {
/**
*
*(2003 xls后綴 導(dǎo)出)
* @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);
//讀取了模板內(nèi)所有sheet內(nèi)容
HSSFSheet sheet = wb.getSheetAt(0);
//如果這行沒有了,,整個(gè)公式都不會(huì)有自動(dòng)計(jì)算的效果的
sheet.setForceFormulaRecalculation(true);
//在相應(yīng)的單元格進(jìn)行賦值
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);
//修改模板內(nèi)容導(dǎo)出新模板
FileOutputStream out = new FileOutputStream("D:/export.xls");
wb.write(out);
out.close();
}
/**
*
*(2007 xlsx后綴 導(dǎo)出)
* @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);
//讀取了模板內(nèi)所有sheet內(nèi)容
XSSFSheet sheet = wb.getSheetAt(0);
//如果這行沒有了,整個(gè)公式都不會(huì)有自動(dòng)計(jì)算的效果的
sheet.setForceFormulaRecalculation(true);
//在相應(yīng)的單元格進(jìn)行賦值
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);
//修改模板內(nèi)容導(dǎo)出新模板
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();
}
}
本文編號(hào):1251766
本文鏈接:http://sikaile.net/wenshubaike/mfmb/1251766.html