package util; import java.io.IOException; import java.net.URL; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.jxls.exception.ParsePropertyException; import net.sf.jxls.transformer.XLSTransformer; /** * Excel生成类. */ public class ExcelUtil { /** * 根据模板生成Excel文件. * @param templateFileName 模板文件. * @param list 模板中存放的数据. * @param resultFileName 生成的文件. */ public void createExcel(String templateFileName, Map<String,Object> beanParams, String resultFileName){ //创建XLSTransformer对象 XLSTransformer transformer = new XLSTransformer(); //获取java项目编译后根路径 URL url = this.getClass().getClassLoader().getResource(""); //得到模板文件路径 String srcFilePath = url.getPath() + templateFileName; String destFilePath = url.getPath() + resultFileName; try { //生成Excel文件 transformer.transformXLS(srcFilePath, beanParams, destFilePath); } catch (ParsePropertyException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
package test; import java.util.ArrayList; import java.util.List; import po.Fruit; import util.ExcelUtil; /** * 测试类. */ public class Test { public static void main(String[] args) { List<Fruit> list = new ArrayList<Fruit>(); list.add(new Fruit("苹果",2.01f)); list.add(new Fruit("桔子",2.05f)); String templateFileName = "template/template.xls"; String resultFileName = "result/fruit.xls"; Map<String,Object> beanParams = new HashMap<String,Object>(); beanParams.put("list", list); new ExcelUtil().createExcel(templateFileName,beanParams,resultFileName); } }
template.xls模板文件截图:
注意:如果你是用的office 2007生成的excel模板,要另存为97-2003版本的。
package po; /** * 水果. */ public class Fruit { /** * 水果名称. */ private String name; /** * 水果价格. */ private float price; public Fruit() { super(); } public Fruit(String name, float price) { super(); this.name = name; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } }
生成fruit.xls文件截图:
-------注意
如果是测试。需要:
public void export2(String filename, Map dataMap, String model) throws IOException{ XLSTransformer transformer = new XLSTransformer(); HSSFWorkbook workbook = null; InputStream in; try { String templateDir= "D:/logs/areaReport.xls"; in = new FileInputStream(templateDir); workbook = transformer.transformXLS(in, dataMap); String targetDir="D:/logs/testDynaMergeCells.xls"; OutputStream os = new FileOutputStream(targetDir); workbook.write(os); os.close(); } catch (Exception e) { e.printStackTrace(); } }
如果是页面,需要以下方式:
package com.jshx.et.common.cache; import java.io.IOException; import java.util.Map; import com.jshx.core.base.action.BaseAction; import com.jshx.et.common.excelModule.ExportExcel; /** * * @param filename 导出的Excel名 * @param dataMap List的集合 * @param model 需要的Excel的名称 * @return * @throws IOException */ public class BaseExcel extends BaseAction{ public String exportExcel(String filename, Map dataMap, String model) throws IOException{ ExportExcel exportExcel = new ExportExcel(); exportExcel.export(filename, dataMap, model, getResponse()); return null; } }
/** * * @param filename 导出的文件名 * @param dataMap List的集合 * @param model * @param response * @throws IOException */ public void export(String filename, Map dataMap, String model, HttpServletResponse response) throws IOException{ XLSTransformer transformer = new XLSTransformer(); HSSFWorkbook workbook = null; InputStream in; try { //此方法需要类与模板放在同一目录下 in = getClass().getResource(model).openStream(); workbook = transformer.transformXLS(in, dataMap); outExcel(workbook, response, filename); } catch (Exception e) { e.printStackTrace(); } } protected void outExcel(HSSFWorkbook workbook, HttpServletResponse response, String filename) { response.setContentType("application ns.ms-excel"); response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); response.setHeader("Content-disposition", "attachment;filename="+filename+".xls"); try { workbook.write(response.getOutputStream()); } catch (ParsePropertyException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
list= new ArrayList<>(); Map map = new HashMap(); map.put("list", list); // 2.导出数据 try { exportExcel("list", map, "template.xls"); } catch (Exception e) { e.printStackTrace(); }
File outdir = null; File outfile = null; outdir = new File(filePath.toString().trim()); //创建文件夹 if (!outdir.exists()){ outdir.mkdirs(); } outfile = new File(targetDir.toString().trim()); //创建文件 if (!outfile.exists()){ outfile.createNewFile(); }
相关推荐
本例为利用JXL导出Excel的代码示例
这是一个jxl声称excel的示例,及jxl的文档
导入导出、合并单元格处理代码示例 jar包解决导出Excel 出现的问题:Warning: Maximum number of format records exceeded. Using default format.
包含:jxl插件以及源代码示例 jxl是一个韩国人写的java操作excel的工具, 在开源世界中,有两套比较有影响的API可供使用,一个是POI,一个是jExcelAPI。其中功能相对POI比较弱一点。但jExcelAPI对中文支持非常好,API...
jxl.rar源代码,示例,文档,Demo,在文件夹中有个说明.txt中详细讲解了jxl.rar的使用说明。
主要介绍了jquery使用jxl插件导出excel的具体实现步骤,需要的朋友可以参考下
jxls,读取json转为excel文件导出
可以与oracel链接,实现了带有固定列的表格,可以直接将数据导出为EXCEL表格
是一个简单的使用jxl的示例,生成一个excel的示例
网上很多例子,都是用Jxl读或者写excel,本文实现的功能就是将数据源in.xls的第几行第几列数据写入到out.xls的第几行第几列,不覆盖out.xls其他原有的数据。
一个用java做的quartz应用示例源代码,包含quartz源代码及quartz库,里面同时包含有javamail的示例和jxl导入导出excel的示例的源代码
主要介绍了django使用xlwt导出excel文件实例代码,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
利用WritableWorkbook 生成excel功能 所使用的jxl_1.0.0.jar 代码示例见:java实现excel导出功能
soaoffice示例代码 SOAOffice是一种中间件软件,不能直接给最终用户使用,需要软件编程人员将SOAOffice集成到软件系统中才能发挥它的威力。比较直观的来说,通常有以下需求的Web开发者推荐使用SOAOffice: 1. 需要把...