导入jar包:

<dependency>
<groupId>net.sf.jxls</groupId>
<artifactId>jxls-core</artifactId>
<version>1.0.5</version>
</dependency>

工具类:

import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.ss.usermodel.Workbook;
import org.jxls.common.Context;
import org.jxls.expression.JexlExpressionEvaluator;
import org.jxls.transform.Transformer;
import org.jxls.transform.poi.PoiTransformer;
import org.jxls.util.JxlsHelper; import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map; /**
* @author klguang
*/
public class JxlsUtils { public static void exportExcel(InputStream is, OutputStream os, Map<String, Object> model) throws IOException{
Context context = PoiTransformer.createInitialContext();
if (model != null) {
for (String key : model.keySet()) {
context.putVar(key, model.get(key));
}
}
JxlsHelper jxlsHelper = JxlsHelper.getInstance();
Transformer transformer = jxlsHelper.createTransformer(is, os);
//获得配置
JexlExpressionEvaluator evaluator = (JexlExpressionEvaluator)transformer.getTransformationConfig().getExpressionEvaluator();
//设置静默模式,不报警告
//evaluator.getJexlEngine().setSilent(true);
//函数强制,自定义功能
Map<String, Object> funcs = new HashMap<String, Object>();
funcs.put("utils", new JxlsUtils()); //添加自定义功能
evaluator.getJexlEngine().setFunctions(funcs);
//必须要这个,否者表格函数统计会错乱
jxlsHelper.setUseFastFormulaProcessor(false).processTemplate(context, transformer);
} public static void exportExcel(File xls, File out, Map<String, Object> model) throws FileNotFoundException, IOException {
exportExcel(new FileInputStream(xls), new FileOutputStream(out), model);
} public static void exportExcel(String templatePath, OutputStream os, Map<String, Object> model) throws Exception {
File template = getTemplate(templatePath);
if(template != null){
exportExcel(new FileInputStream(template), os, model);
} else {
throw new Exception("Excel 模板未找到。");
}
} //获取jxls模版文件
public static File getTemplate(String path){
File template = new File(path);
if(template.exists()){
return template;
}
return null;
} // 日期格式化
public String dateFmt(Date date, String fmt) {
if (date == null) {
return "";
}
try {
SimpleDateFormat dateFmt = new SimpleDateFormat(fmt);
return dateFmt.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return "";
} // if判断
public Object ifelse(boolean b, Object o1, Object o2) {
return b ? o1 : o2;
} public static void exportExcelByJxls(InputStream is, OutputStream os,Map<String , Object> model) {
//模拟数据
Map<String,Object> beans = model;
XLSTransformer transformer = new XLSTransformer();
InputStream in=is;
OutputStream out=os;
try {
Workbook workbook=transformer.transformXLS(in, beans);
//将内容写入输出流并把缓存的内容全部发出去
workbook.write(out);
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in!=null){try {in.close();} catch (IOException e) {}}
if (out!=null){try {out.close();} catch (IOException e) {}}
}
}
}

map模板:

Map<String , Object> model=new HashMap<String , Object>();
model.put("maps", listExportMap);

注解:

jx:each(items="maps" var="map" lastCell="I4")

map行格式:

${maps.get("serial")}    ${maps.get("wardName")}    ${maps.get("pushNum")}    ${maps.get("readNum")}    ${maps.get("readRate")}    ${maps.get("pushPatientNum")}    ${maps.get("readPatientNum")}    ${maps.get("perCapitaPushNum")}    ${maps.get("perCapitaReadNum")}

普通格式:${nowDate}

浏览器下载格式代码:

public void export(HttpServletRequest request,HttpServletResponse response){
String templateFileName= request.getServletContext().getRealPath("/") + "/resources/templateFileName.xls";
String destFileName= "destFileName.xls";
//模拟数据
List<Employee> staff = new ArrayList<Employee>();
staff.add(new Employee("Derek", 35, 3000, 0.30));
staff.add(new Employee("Elsa", 28, 1500, 0.15));
staff.add(new Employee("Oleg", 32, 2300, 0.25));
Map<String,Object> beans = new HashMap<String,Object>();
beans.put("employees", staff);
XLSTransformer transformer = new XLSTransformer();
InputStream in=null;
OutputStream out=null;
//设置响应
response.setHeader("Content-Disposition", "attachment;filename=" + destFileName);
response.setContentType("application/vnd.ms-excel");
try {
in=new BufferedInputStream(new FileInputStream(templateFileName));
Workbook workbook=transformer.transformXLS(in, beans);
out=response.getOutputStream();
//将内容写入输出流并把缓存的内容全部发出去
workbook.write(out);
out.flush();
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in!=null){try {in.close();} catch (IOException e) {}}
if (out!=null){try {out.close();} catch (IOException e) {}}
}
}
}

类模板:

最新文章

  1. Bootstrap&lt;基础十六&gt; 导航元素
  2. PHP框架中的日志系统
  3. 爬虫例子及知识点(scrapy知识点)
  4. [LeetCode] TwoSum
  5. 限制EditText只能输入小数点后两位
  6. LeetCode344:Reverse String@Python
  7. Transactional replication 的Snapshot 文件在什么情况下会被清除
  8. struts2文件上传大小限制问题小结
  9. Hadoop2.7.2安装笔记
  10. C++中模板使用详解
  11. iOS __block用法
  12. UVA 1614 - Hell on the Markets
  13. SharePoint迁移数据到生产环境
  14. 导航条css实现和table实现
  15. redis实现队列消息的ack
  16. tensorflow l2_normalize函数
  17. 解决Android下元素滑动问题
  18. (一)IDEA工具开第一个springboot应用之helloworld
  19. Link-Cut-Trees
  20. Pycharm节能模式

热门文章

  1. phpstorm git配置
  2. PHP 配置多站点多目录
  3. Centos6安装zabbix-agent
  4. 【WebSocket】WebSocket消息推送
  5. flutter页面布局三
  6. WORM Worm worm 毛毛虫爬树爬树~
  7. 26 October in 614
  8. css &gt; 的写法 html
  9. pycharm运行html文件报404错误
  10. 【GDAL】GDAL栅格数据结构学习笔记(一): 关于Metadata