java代码
try {
Map beans = new HashMap(1);
beans.put("name",conName);
String srcFilePath = Thread.currentThread().getContextClassLoader().getResource("excel/picture.xlsx").getPath();
//Thread.currentThread().getContextClassLoader().getResource("pdf/picturePdf.pdf").getPath();
String fileName = "工程质量巡检记录表";
String desFilePath = "E:/fy/" + fileName;
FileInputStream in =new FileInputStream(srcFilePath);
//读取excel模板
XSSFWorkbook wb = new XSSFWorkbook(in);
//读取了模板内所有sheet内容
XSSFSheet sheet = wb.getSheetAt(0);
//修改需要改的字段
sheet.getRow(1).getCell(0).setCellValue("工程项目名称:"+conName);
String downloadFileName=new String(fileName.getBytes("utf-8"),"iso8859-1");
response.addHeader("Content-Disposition", "attachment;filename="+downloadFileName+".xlsx");
OutputStream os = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/vnd.ms-excel;charset=utf-8");
//将excel写入到输出流中
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
code=31500;
}
特别注意:sheet.getRow(1).getCell(0).setCellValue("工程项目名称:"+conName);
getCell中的数字必须是要修改的列数,如果是多列合并,取被合并列的第一列的列数,否则不会赋值到你想要的地方
pom
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>RELEASE</version>
</dependency>