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(); }}
相关资源:poi导出excel2007