admin管理员组文章数量:1609530
依赖
<!-- excel表格 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
生成表格的工具类
生成的Excel表格保存到当前程序的 temp
文件夹
生产表格的实体类放到最下面了
package com.pro.common.utils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.pro.common.vo.ReportedExcelDataVo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.*;
/**
* 签章的 Excel 表格生成工具类
*/
public class SealExcelUtil {
private static Logger logger = LoggerFactory.getLogger(SealExcelUtil.class);
/**
* 创建 Excel 文件
*
* @param dataObject excel文件对象
* @return
*/
public static String createExcel(ReportedExcelDataVo dataObject) {
Workbook wb = createWorkbookExcel(dataObject);
// 生成文件
OutputStream outputStream = null;
try {
// 保存到当前程序的 temp 文件夹
File temp = new File("temp");
if (!temp.exists()) {
temp.mkdirs();
}
logger.warn(">>> Excel文件保存文件夹: [{}]", temp.getAbsolutePath());
String fileName = String.format("%s%sorigin_%s.xlsx", temp.getAbsolutePath(), File.separator, UUID.randomUUID().toString().replace("-", ""));
outputStream = new FileOutputStream(fileName);
wb.write(outputStream);
logger.warn(">>> Excel文件保存位置: [{}]", fileName);
return fileName;
} catch (Exception e) {
e.printStackTrace();
logger.error("生成excel文件异常", e);
} finally {
try {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
} catch (Exception e) {
}
try {
if (wb != null) {
wb.close();
}
} catch (Exception e) {
}
}
return null;
}
/**
* 创建上报 Excel 表格
*
* @param dataObject
*/
public static Workbook createWorkbookExcel(ReportedExcelDataVo dataObject) {
Workbook wb = new HSSFWorkbook();
int rowSize = 0;
// 分隔行数
int separatorRows = 1;
// 创建工作表1
Sheet sheet = wb.createSheet();
// 合并单元格, CellRangeAddress 四个参数为:起始行,结束行,起始列,结束列
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5)); // 合并第一行的1到6列
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 1)); // 合并第二行的1、2列
sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 1)); // 合并第三行的1、2列
// 设置某一列的宽度(列, 宽度), 列宽的字符是 1/256, 所以 *256
sheet.setColumnWidth(0, 20 * 256);
sheet.setColumnWidth(1, 30 * 256);
// 创建右上角第一行(右上角标签行)
Row topRow = sheet.createRow(rowSize);
// 设置行高, 行高像素点是1/20, 所以 *20
topRow.setHeight((short) (40 * 20));
Cell topRightCell = topRow.createCell(0);
topRightCell.setCellValue(dataObject.getFillBatchNum());
// 创建样式
CellStyle topRightCellStyle = wb.createCellStyle();
topRightCellStyle.setVerticalAlignment(VerticalAlignment.TOP); // 垂直对齐方式
topRightCellStyle.setAlignment(HorizontalAlignment.RIGHT); // 水平对齐方式
// 填充样式
topRightCell.setCellStyle(topRightCellStyle);
// 创建第1行(标题行)
rowSize += 1;
Row row1 = sheet.createRow(rowSize);
// 设置当前行的高度, 行高像素点是1/20, 所以 *20
row1.setHeight((short) (40 * 20)); // 行高 *20
// 给列赋值
Cell cell = row1.createCell(0);
cell.setCellValue(dataObject.getTitle());
// 创建样式
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直对齐方式
cellStyle.setAlignment(HorizontalAlignment.CENTER); // 水平对齐方式
// 创建字体
Font font = wb.createFont();
font.setFontHeight((short) (15 * 20)); // 字体大小 *20
font.setBold(true); // 字体加粗
cellStyle.setFont(font);
// 填充样式
cell.setCellStyle(cellStyle);
// 创建第2行(副标题行)
rowSize += 1;
Row row2 = sheet.createRow(rowSize);
// 设置当前行的高度, 行高像素点是1/20, 所以 *20
row2.setHeight((short) (40 * 20)); // 行高 *20
// 设置样式
CellStyle cellStyle2 = wb.createCellStyle();
// 设置对齐方式
cellStyle2.setVerticalAlignment(VerticalAlignment.TOP); // 垂直对齐方式
cellStyle2.setAlignment(HorizontalAlignment.CENTER); // 水平对齐方式
// 创建字体
Font subjectTitleFont = wb.createFont();
subjectTitleFont.setFontHeight((short) (12 * 20));
cellStyle2.setFont(subjectTitleFont);
// 给列赋值
Cell cell1 = row2.createCell(0);
cell1.setCellValue("电子签章文件");
cell1.setCellStyle(cellStyle2);
/**------------------------ 普通内容 ------------------------**/
// 设置当前行的高度, 行高像素点是1/20, 所以行高 = 行高 * 20
short contentRowHeight = 26 * 20;
// 创建第3行
rowSize += separatorRows;
Row row3 = sheet.createRow(rowSize);
row3.setHeight(contentRowHeight);
row3.createCell(0).setCellValue("填报单位:");
row3.createCell(1).setCellValue(dataObject.getFillUnit());
// 创建第4行
rowSize += separatorRows;
Row row4 = sheet.createRow(rowSize);
row4.setHeight(contentRowHeight);
row4.createCell(0).setCellValue("填报人:");
row4.createCell(1).setCellValue(dataObject.getFillUserName());
// 创建第5行
rowSize += separatorRows;
Row row5 = sheet.createRow(rowSize);
row5.setHeight(contentRowHeight);
row5.createCell(0).setCellValue("申请时间:");
row5.createCell(1).setCellValue(DateFormatUtils.formatDateTime(dataObject.getSealTime()));
// 创建第6行
rowSize += separatorRows + 3;
Row row6 = sheet.createRow(rowSize);
row6.setHeight(contentRowHeight);
row6.createCell(0).setCellValue("签章:");
CellStyle contentStyle = wb.createCellStyle();
contentStyle.setWrapText(true); // 自动换行
contentStyle.setVerticalAlignment(VerticalAlignment.TOP); // 垂直对齐方式
contentStyle.setAlignment(HorizontalAlignment.LEFT); // 水平对齐方式
// 创建工作表2
createReportedContentSheet(wb, dataObject);
return wb;
}
/**
* 生成上报工作表
*
* @param wb
* @param dataObject
*/
private static void createReportedContentSheet(Workbook wb, ReportedExcelDataVo dataObject) {
int rowSize = 0;
// 分隔行数
int separatorRows = 2;
// 创建工作表1
Sheet sheet = wb.createSheet();
// 合并单元格, CellRangeAddress 四个参数为:起始行,结束行,起始列,结束列
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5)); // 合并第一行的1到6列
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 1)); // 合并第二行的1、2列
// 设置某一列的宽度(列, 宽度), 列宽的字符是 1/256, 所以 *256
sheet.setColumnWidth(0, 20 * 256);
sheet.setColumnWidth(1, 30 * 256);
// 创建右上角第一行(右上角标签行)
Row topRow = sheet.createRow(rowSize);
// 设置行高, 行高像素点是1/20, 所以 *20
topRow.setHeight((short) (40 * 20));
Cell topRightCell = topRow.createCell(0);
topRightCell.setCellValue("附录内容");
// 创建样式
CellStyle topRightCellStyle = wb.createCellStyle();
topRightCellStyle.setVerticalAlignment(VerticalAlignment.TOP); // 垂直对齐方式
topRightCellStyle.setAlignment(HorizontalAlignment.RIGHT); // 水平对齐方式
// 填充样式
topRightCell.setCellStyle(topRightCellStyle);
// 创建第1行(标题行)
rowSize += 1;
Row row1 = sheet.createRow(rowSize);
// 设置当前行的高度, 行高像素点是1/20, 所以 *20
row1.setHeight((short) (40 * 20)); // 行高 *20
// 给列赋值
Cell cell = row1.createCell(0);
cell.setCellValue(dataObject.getTitle());
// 创建样式
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直对齐方式
cellStyle.setAlignment(HorizontalAlignment.CENTER); // 水平对齐方式
// 创建字体
Font font = wb.createFont();
font.setFontHeight((short) (15 * 20)); // 字体大小 *20
font.setBold(true); // 字体加粗
cellStyle.setFont(font);
// 填充样式
cell.setCellStyle(cellStyle);
/**------------------------ 普通内容 ------------------------**/
// 创建主题标题样式
CellStyle subjectTitleStyle = wb.createCellStyle();
Font subjectTitleFont = wb.createFont();
subjectTitleFont.setFontHeight((short) (12 * 20));
subjectTitleStyle.setFont(subjectTitleFont);
// 设置当前行的高度, 行高像素点是1/20, 所以行高 = 行高 * 20
short rowHeight = 18 * 20;
// 填充主题数据
List<ReportedExcelDataVo.ReportedSubjectDataVo> subjectList = dataObject.getSubjectData();
for (ReportedExcelDataVo.ReportedSubjectDataVo subjectData : subjectList) {
// 创建标题行
rowSize += separatorRows;
Row titleRow = sheet.createRow(rowSize);
// 给列填充内容
Cell titleCell = titleRow.createCell(0);
titleCell.setCellValue(subjectData.getSubjectName());
titleCell.setCellStyle(subjectTitleStyle);
titleRow.setHeight(rowHeight); // 设置行高
for (Map<String, Map<String, String>> stringMapMap : subjectData.getSubjectDataItem()) {
for (Map.Entry<String, Map<String, String>> stringMapEntry : stringMapMap.entrySet()) {
Map<String, String> body = stringMapEntry.getValue();
// 创建内容行
rowSize += 1;
Row contentRow = sheet.createRow(rowSize);
contentRow.createCell(0).setCellValue(String.format("%s:", body.get("key")));
contentRow.createCell(1).setCellValue(body.get("value"));
// 设置当前行的高度, 行高像素点是1/20, 所以 *20
contentRow.setHeight(rowHeight); // 行高 *20
}
// 每个主题行下面的主题列表结束之后隔开一行
rowSize += 1;
}
}
}
public static Map<String, String> createMap(String key, String value) {
Map<String, String> map = new HashMap<>(2);
map.put("key", key);
map.put("value", value);
return map;
}
public static void main(String[] args) {
ReportedExcelDataVo dataObject = new ReportedExcelDataVo();
dataObject.setFillBatchNum("TBPC_2022010414ecf241eb494c21a03dc0cecc52a629");
dataObject.setTitle("xxx评估表");
dataObject.setFillUnit("广东省xxx办事处");
dataObject.setFillUserName("张珊珊");
dataObject.setSealTime(new Date());
ReportedExcelDataVo.ReportedSubjectDataVo infoList1 = new ReportedExcelDataVo.ReportedSubjectDataVo();
infoList1.setSubjectName("基本信息");
ArrayList<Map<String, Map<String, String>>> list = new ArrayList<>();
Map<String, Map<String, String>> map = new LinkedHashMap<>();
map.put("name", createMap("填报单位", "广东省xxx办事处"));
map.put("age", createMap("填报人", "张珊珊"));
map.put("sex", createMap("填报日期", "2022-05-12"));
list.add(map);
infoList1.setSubjectDataItem(list);
ReportedExcelDataVo.ReportedSubjectDataVo infoList2 = new ReportedExcelDataVo.ReportedSubjectDataVo();
infoList2.setSubjectName("详细记录");
ArrayList<Map<String, Map<String, String>>> list2 = new ArrayList<>();
Map<String, Map<String, String>> map2 = new LinkedHashMap<>();
map2.put("name", createMap("实施清单", "上访案件"));
map2.put("age", createMap("当月数量", "18"));
map2.put("sex", createMap("当月解决", "25"));
Map<String, Map<String, String>> map3 = new LinkedHashMap<>();
map3.put("name", createMap("实施清单", "消防巡检"));
map3.put("age", createMap("当月数量", "100"));
map3.put("sex", createMap("当月解决", "25"));
list2.add(map2);
list2.add(map3);
infoList2.setSubjectDataItem(list2);
dataObject.setSubjectData(Arrays.asList(infoList1, infoList2));
createExcel(dataObject);
}
}
实体类
package com.pro.common.vo;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 生成Excel对象
*/
public class ReportedExcelDataVo implements Serializable {
/**
* 填报批次号 (系统赋值)
*/
private String fillBatchNum;
/**
* 表格标题
*/
private String title;
/**
* 填报单位
*/
private String fillUnit;
/**
* 填报人
*/
private String fillUserName;
/**
* 签章时间
*/
private Date sealTime;
/**
* 填报主题数据
*/
private List<ReportedSubjectDataVo> subjectData;
/**
* 主题数据对象
*/
public static class ReportedSubjectDataVo implements Serializable {
/**
* 主题名称
*/
private String subjectName;
/**
* 主题数据项
* [
* { "数据项key1": { key: "数据项名称", value: "数据项值" } },
* { "数据项key2": { key: "数据项名称", value: "数据项值" } }
* ]
*/
private List<Map<String, Map<String, String>>> subjectDataItem;
// 此处省略get、set
}
// 此处省略get、set
}
版权声明:本文标题:使用java生成Excel表格 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1728570454a1164104.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论