生成excel有两种方式,第一:用代码画表格生成,第二:读取字段信息进行模板的填充
本文只叙述第一种方式,第二种以后更新。
- 1.引入对应的依赖:pom文件
<!--excel poi--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.2</version> </dependency>
- 2.excel的填充以及生成
@Override
public String ExcelOpinion( XSSFWorkbook book) throws Exception{
String temp = "1111111";
Sheet sheet = book.createSheet("text1");
// ---------- 设置单元格长度 -----------
//第一行
Row row1 = sheet.createRow(0);
row1.setHeight((short) 600);
Cell cell1 = row1.createCell(0);
sheet.setColumnWidth(1,256*22); //设置列宽
cell1.setCellValue("意见表");
cell1.setCellStyle(ExcelStyleUtils.Heade(book));//设置格式,此处我抽取写了公用方法
sheet.addMergedRegion(new CellRangeAddress(0,0,0,7));//合并
//第二行
Row row2 = sheet.createRow(1);
row2.setHeight((short) 460);
Cell cell2 = null;
for (int i=0; i<8; i++) {
cell2 = row2.createCell(i);
cell2.setCellStyle(ExcelStyleUtils.HeadeSceond(book));
sheet.setColumnWidth(i,256*15);
if (i==0) {
cell2.setCellValue(temp);