JXLS Demo

JxlsUtil.java

package template;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jxls.exception.ParsePropertyException;
import net.sf.jxls.transformer.XLSTransformer;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;

public class JxlsUtil {

/**
* 导出excel
* @param templateFile - excel模版名称
* @param beans - 模版中填充的数据
* @param os - 生成模版输出流
*/
public static void exportExcel(String templateFile,Map<String,Object> beans,OutputStream os) {
XLSTransformer transformer = new XLSTransformer();
InputStream is=JxlsUtil.class.getClassLoader().getResourceAsStream(templateFile);
try {
Workbook workbook=transformer.transformXLS(is,beans);
workbook.write(os);
} catch (Exception e) {
throw new RuntimeException("导出excel错误!");
}
}

public static void main(String[] args) throws ParsePropertyException, InvalidFormatException, IOException {
OutputStream os=new FileOutputStream("new.xls");
String templateFile="template/jxls_template.xls";
Map<String, Object> beans=new HashMap<String, Object>();

// fruits
List<Map<String,String>> fruitList=new ArrayList<Map<String,String>>();

Map<String,String> fruit=null;
fruit=new HashMap<String, String>();
fruit.put("name", "苹果");
fruit.put("price", "100");
fruitList.add(fruit);

fruit=new HashMap<String, String>();
fruit.put("name", "香蕉");
fruit.put("price", "200");
fruitList.add(fruit);

beans.put("fruits",fruitList);
exportExcel(templateFile, beans, os);
}
}

template/jxls_template.xls
[img]http://dl.iteye.com/upload/attachment/0069/3315/29a2a324-97f2-3bd2-839e-26a96cab51d8.bmp[/img]

在使用<jx:forEach>做循环时,如果<jx:forEach>和</jx:forEach> 在同一行就可以做横向循环
### JXLS 3.0 版本演示示例 JXLS 是一个用于简化 Excel 报表生成的 Java 库。以下是基于 JXLS 3.0 的简单示例,展示如何读取模板并填充数据到 Excel 文件。 #### 准备工作 确保已经添加了 JXLS 和 POI 依赖项至 Maven `pom.xml` 文件: ```xml <dependency> <groupId>org.jxls</groupId> <artifactId>jxls-core</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.2.3</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.3</version> </dependency> ``` #### 创建模板文件 (template.xlsx) | Name | Age | |------------|-----| | ${person.name} | ${person.age} | 保存此表格作为 `template.xlsx`. #### 编写 Java 代码来处理报表逻辑 ```java import org.jxls.common.Context; import org.jxls.util.JxlsHelper; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; public class ReportGenerator { public static void main(String[] args) throws Exception { List<Person> persons = new ArrayList<>(); persons.add(new Person("John", 30)); persons.add(new Person("Jane", 25)); try ( InputStream is = ReportGenerator.class.getResourceAsStream("/template.xlsx"); OutputStream os = new FileOutputStream("output_report.xlsx") ) { Context context = new Context(); context.putVar("persons", persons); JxlsHelper.getInstance().processTemplate(is, os, context); } } private static class Person { String name; int age; Person(String name, int age) { this.name = name; this.age = age; } // Getters and setters omitted for brevity. } } ``` 上述程序会读入名为 "template.xlsx" 的 Excel 模板文档,并利用给定的数据集替换占位符 `${person.name}` 及 `${person.age}`, 最终输出新的 Excel 文档 "output_report.xlsx"[^1].
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值