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


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?