Java在按照模板导出excel,java 通过模板导出excel

1 pom

net.sf.jxls

jxls-core

1.0.6

2

package com.meitun.finance.test;

import java.util.ArrayList;

import java.util.Date;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import net.sf.jxls.exception.ParsePropertyException;

import net.sf.jxls.transformer.XLSTransformer;

public class TestExcel {

public static void main(String[] args){

String srcFilePath = "D:\\work\\doc\\exceltemplate\\test.xlsx";

Map beanParams = new HashMap();

List product = new ArrayList();

Product producta = new Product();

producta.setNo("123242344534546456456");

producta.setName("我的aaa");

producta.setPrice(103.369);

producta.setScale("sdfsgfsgdfgdfg");

producta.setCreated(new Date());

product.add(producta);

Product productb = new Product();

productb.setNo("123242344534546456456");

productb.setName("qqqqqq");

productb.setPrice(200.582);

productb.setScale("rrrrrrr");

productb.setCreated(new Date());

product.add(productb);

beanParams.put("vms", product);

String destFilePath = "D:\\work\\doc\\exceltemplate\\simple.xlsx";

try {

XLSTransformer transformer = new XLSTransformer();

transformer.transformXLS(srcFilePath, beanParams, destFilePath);

} catch (ParsePropertyException e) {

e.printStackTrace();

} catch (InvalidFormatException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java通过模板导出Excel可以使用Apache POI和Jxls两种方式。 1. Apache POI Apache POI是一个用于读写Microsoft Office格式文件的Java库。通过POI,可以使用Java代码读取、创建和修改Excel文件、Word文档和PowerPoint演示文稿等Microsoft Office格式文件。 使用POI导出Excel需要先创建Excel模板,然后在Java代码中读取模板文件,根据数据填充模板,最后将填充后的数据写入新的Excel文件。 2. Jxls Jxls是一个用于将Java数据导出Excel、Word和PDF等格式文件的开源Java库。Jxls提供了一种基于Excel模板的数据填充方式,可以通过Java对象、Map或List等数据源填充Excel模板,并将填充后的数据写入新的Excel文件。 使用Jxls导出Excel需要先创建Excel模板,然后在Java代码中读取模板文件,根据数据填充模板,最后将填充后的数据写入新的Excel文件。 下面是使用Apache POI和Jxls两种方式导出Excel的示例代码: 1. 使用Apache POI导出Excel ```java // 导出Excel模板 public void exportExcelTemplate(String templateFilePath, String destFilePath) throws Exception { InputStream is = new FileInputStream(templateFilePath); Workbook workbook = WorkbookFactory.create(is); Sheet sheet = workbook.getSheetAt(0); // 在模板中填充数据 // ... OutputStream os = new FileOutputStream(destFilePath); workbook.write(os); os.close(); is.close(); } // 导出Excel数据 public void exportExcelData(String templateFilePath, String destFilePath, List<Map<String, Object>> dataList) throws Exception { InputStream is = new FileInputStream(templateFilePath); Workbook workbook = WorkbookFactory.create(is); Sheet sheet = workbook.getSheetAt(0); // 在模板中填充数据 for (int i = 0; i < dataList.size(); i++) { Row row = sheet.createRow(i + 1); Map<String, Object> data = dataList.get(i); row.createCell(0).setCellValue(data.get("name").toString()); row.createCell(1).setCellValue(data.get("age").toString()); // ... } OutputStream os = new FileOutputStream(destFilePath); workbook.write(os); os.close(); is.close(); } ``` 2. 使用Jxls导出Excel ```java // 导出Excel模板 public void exportExcelTemplate(String templateFilePath, String destFilePath) throws Exception { InputStream is = new FileInputStream(templateFilePath); OutputStream os = new FileOutputStream(destFilePath); Context context = new Context(); JxlsHelper.getInstance().processTemplate(is, os, context); os.close(); is.close(); } // 导出Excel数据 public void exportExcelData(String templateFilePath, String destFilePath, List<Map<String, Object>> dataList) throws Exception { InputStream is = new FileInputStream(templateFilePath); OutputStream os = new FileOutputStream(destFilePath); Context context = new Context(); context.putVar("dataList", dataList); JxlsHelper.getInstance().processTemplate(is, os, context); os.close(); is.close(); } ``` 以上是两种方式导出Excel的示例代码,其中使用Jxls导出Excel需要在项目中添加jxls和jxls-poi依赖。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值