Springboot | 您所在的位置:网站首页 › excel导入模板怎么填写内容 › Springboot |
文章目录
前言依赖引入编写一个导出模板编写工具类编写测试类导出测试使用 jx 表达式填充(2023.08.08)1、简单报表复杂的单行数据填充
前言
jxls的正确使用,需要使用到下面的表达式。 # 设置 列表数据展示 jx:each(items="item.itemsList" var="obj" lastCell="H11" varIndex="ojbIndex") jx:if(condition="item.productType.equals('财务报表')&&ojbIndex==0" lastCell="H11" areas=["A11:H11"]) # 设置表title 范围 jx:area(lastCell=”H12”)但本次,并未使用到上面的要求,直接进行指定xls模板数据填充与导出。 依赖引入 org.jxls jxls 2.8.1 net.sf.jxls jxls-core 1.0.6 org.jxls jxls-poi 2.8.1 编写一个导出模板如下所示: 放在springboot项目中的src\main\resources下的templates_report中,命名为test_user2.xls。 编写工具类 import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.Map; import org.apache.poi.ss.usermodel.Workbook; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import net.sf.jxls.transformer.XLSTransformer; public class ExcelUtil { /** * 下载excel * * @param sourcePath 模板路径 * @param beanParams excel内容 * @return */ public static byte[] downLoadExcel(String sourcePath, Map beanParams) throws Exception { ByteArrayOutputStream os = new ByteArrayOutputStream(); //读取模板 Resource resource = new ClassPathResource(sourcePath); InputStream is = resource.getInputStream(); XLSTransformer transformer = new XLSTransformer(); //向模板中写入内容 Workbook workbook = transformer.transformXLS(is, beanParams); //写入成功后转化为输出流 workbook.write(os); byte[] bytes = os.toByteArray(); return bytes; } } 编写测试类 @GetMapping("/report-xls") public void excelReport(HttpServletResponse response) throws Exception { Map map = new HashMap(); List dataList = Lists.newArrayList(); for (int i = 0; i |
CopyRight 2018-2019 实验室设备网 版权所有 |