若依框架(ruoyi vue版)集成jeecg积木报表详细开发过程
@Override
public void exportExcel(String templateUrl, String fileName, Map<String, Object> dataMap, HttpServletResponse response) {
// 获取模板输入流
InputStream inputStream = null;
ServletOutputStream outputStream = null;
try {
inputStream = new FileInputStream(templateUrl);
// 读取模板并填充数据
XLSTransformer transformer = new XLSTransformer();
Workbook workbook = transformer.transformXLS(inputStream, dataMap);
// 导出Excel
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.setContentType("application/vnd.ms-excel;charset=utf-8");
outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
这段代码示例展示了如何使用XLSTransformer
来填充模板并导出Excel文件。首先,通过文件路径获取模板输入流,然后使用XLSTransformer
的transformXLS
方法填充数据,并且设置响应头和内容类型以准备导出。最后,将填充后的Excel工作簿写入响应输出流,并关闭相关资源。
评论已关闭