报表导出功能开发

使用easyExcel开发

实现复杂表头,如图

 

加上如下注解即可

@ExcelProperty({"主标题", "字符串标题"})    
private String string;
@ExcelProperty({
"主标题", "日期标题"}) private Date date; @ExcelProperty({"主标题", "数字标题"})

导出图片

easyExcel有五种实现方式

private File file;
private InputStream inputStream;
/**
 * 如果string类型 必须指定转换器,string默认转换成string
 */
@ExcelProperty(converter = StringImageConverter.class)
private String string;
private byte[] byteArray;
private URL url;

该方式写出的图片只占一行,图片太小,需要更改图片大小,官网给出的解决方案是添加一个拦截器 https://www.yuque.com/easyexcel/faq/wpedtd,我在系统添加了这个拦截器后,会报空指针的异常,所以,在这里我没有使用easyExcel,而是使用了poi

// 图片插入坐标
anchor.setCol1(excelPositionRange.getFirstCol());
anchor.setRow1(excelPositionRange.getFirstRow());

// 指定我想要的长宽
double standardWidth = 112;
double standardHeight = 41;

// 计算单元格的长宽
double cellWidth = finalSheet.getColumnWidthInPixels(cell.getColumnIndex());
double cellHeight = cell.getRow().getHeightInPoints()/72*96;

// 计算需要的长宽比例的系数
double a = standardWidth / cellWidth;
double b = standardHeight / cellHeight;

// 插入图片
Picture pict = patriarch.createPicture(anchor, pictureIdx);
pict.resize(a,b);

poi导出图片的一般方法:https://poi.apache.org/components/spreadsheet/quick-guide.html#Images 

合并单元格

在poi中通过这个

sheet.addMergedRegion(new CellRangeAddress(
1, //first row (0-based)
1, //last row (0-based)
1, //first column (0-based)
2 //last column (0-based)
));
方法来实现合并单元格

 

posted on 2021-08-01 23:39  苇草爱学习  阅读(1)  评论(0)    收藏  举报

导航