JXLS根据excel模板生成EXCEL并下载

2019-03-01 15:28|来源: 网络

jxl.jar

jxls-core-0.9.9.jar

jxls-reader-0.9.9.jar

poi-3.5-FINAL.jar(必须3.5以上的版本)
其他jar根据提示可以去 http://jarvana.com/jarvana/ 查找


生成excel 传入模板文件 要生成的内容 生成文件 返回生成文件的完整路径

public static String doExcel(String from, Map beans, String to) {
     ServletContext sc = (ServletContext) ActionContext.getContext().get(
             ServletActionContext.SERVLET_CONTEXT);
     String path = sc.getRealPath("/model");
     XLSTransformer transformer = new XLSTransformer();
     String sfrom = path + "\\" + from;// 模板文件
     String sto = path + "\\" + to;// 要生成的文件
     try {
         transformer.transformXLS(sfrom, beans, sto);
     } catch (ParsePropertyException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }
     return sto;
 }

下载

public static void doDownLoad(String path, String name,
         HttpServletResponse response) {
     try {
         response.reset();
         response.setHeader("Content-disposition",
                 "attachment;success=true;filename ="
                         + URLEncoder.encode(name, "utf-8"));
         BufferedInputStream bis = null;
         BufferedOutputStream bos = null;
         OutputStream fos = null;
         InputStream fis = null;
         File uploadFile = new File(path);
         fis = new FileInputStream(uploadFile);
         bis = new BufferedInputStream(fis);
         fos = response.getOutputStream();
         bos = new BufferedOutputStream(fos);
         // 弹出下载对话框
         int bytesRead = 0;
         byte[] buffer = new byte[8192];
         while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
             bos.write(buffer, 0, bytesRead);
         }
         bos.flush();
         fis.close();
         bis.close();
         fos.close();
         bos.close();
     } catch (Exception e) {
         e.printStackTrace();
     }
 }

调用:

List resultList=new ArrayList();          
for (int i = 0; i < list.size(); i++) {
    。。。。。。
    InspectionStisfaction vo=new InspectionStisfaction();
    vo.setXjrq(SystemUtil.getTimeStr2Str(
        String.valueOf(map.get("apply_date"))));
    vo.setXjsl(lxjsl);
    vo.setFcmy(lfcmy);
    vo.setMy(lmy);
    vo.setYb(lyb);
    vo.setBmy(lbmy);
    vo.setFcbmy(lfcbmy);
    resultList.add(vo);
}
     
//生成EXcel
Map beans = new HashMap();
beans.put("resultList",resultList);
String path=SystemUtil.doExcel("巡检商户满意度报表模板.xls", 
    beans, "巡检商户满意度报表.xls");
SystemUtil.doDownLoad(path, "巡检商户满意度报表.xls",this.response);
return null;

模板样式:

巡检数量(个)   总体满意度 非常满意    满意     一般         不满意    非常不满意  合计
                         
${l.xjrq}         ${l.xjsl}     ${l.fcmy}   ${l.my}    ${l.yb}    ${l.bmy}   ${l.fcbmy}   $[B#+C#+D#]
                       
当月数量总计(个)  $[SUM(B4)]   $[SUM(D4)]  $[SUM(E4)] $[SUM(F4)] $[SUM(G4)] $[SUM(H4)]   $[SUM(I4)]


本文链接:JXLS根据excel模板生成EXCEL并下载,转自:http://www.cnblogs.com/yangy608/archive/2010/09/07/1820681.html

相关问答

更多
  • 网上好多啊 我给你找了一个例子 view plaincopy to clipboardprint? public static void main(String[] args) { try { String filepath = "d:\\问题清单.xls"; FileInputStream fis = new FileInputStream(filepath); // POIFSFileSystem pfs = new POIFSFileSystem(new FileInputStream("d:\\OA ...
  • 我这里有两本,一个excel函数教程(PDF格式),一个Excel应用宝典(EXE格式),要的话给我邮件吧:lzgzjwc@163.com
  • 基本上,这取决于你的excel文件应该有多复杂。 有更多的库,我有一些Jasper报告库的经验,我可以推荐它。 有一个Eclipse插件可用,它可以作为GUI编辑器。 你可以设计一个模板等。 下一个选项是使用Apache POI来完成 - 使用它的工作非常简单。 Basically it depends how complicated your excel file should be. There are more libraries, I have some experience with Jasper ...
  • 您总是可以使用= IFERROR(a,b)来修饰公式,其中a是原始公式(或值),如果a是#VALUE,则b是您要显示的内容 The problem is that the ${employee.salary} expands into a multiple rows on the spreadsheet, depending on the number of items in the collection. Excel considers this to be an insertion, so any fo ...
  • 您的代码有一些错误: 你错过了Next x 因为使用For..Next语句,所以不必递增x 。 Next x将为您做到这一点。 工作代码: 对于VB.NET : Dim ExcelObject() As Object ExcelObject = New Object(5) {} For x = 1 To 5 ExcelObject(x) = CreateObject("Excel.Application") ExcelObject(x).visible = True ExcelObj ...
  • 拥有日期/时间字段的想法很好......为此创建一本工作簿 将以下代码添加到ThisWorkbook模块: Private Sub Workbook_Open() If [B2] = "" Then ' timestamp [B2] = Now() ' suppress warning when saving macro containing workbook in non-macro format Application.Displ ...
  • @Pubs,我认为,您可以考虑使用Aspose.Cell for .NET API,因为它们不需要Excel应用程序的可用性来动态创建,操作或转换电子表格。 而且,这些API提供了将数据导入工作表的方法的真实性,例如从Array,ArrayList,DataTable,DataColumn,DataView,DataGrid,DataReader和GridView 导入数据 。 此外,如果您在电子表格中具有预先配置的图表,则可以简单地更新图表的数据源以使用新数据进行刷新,或者从头创建图表以最终将图表/电子表 ...
  • 您可以使用锚标记来提供excel模板以供下载。 确保路径正确。 Download Template You can use the anchor tag to serve excel template for download. Make sure the path is correct.
  • 您可以在Excel模板中使用标记。 例如,如果您有类似于以下的模板 Row 1: Row 2: place holders for data that will be printed in the loop Row 3: 如果someData不为空,则输出中将打印行,但如果someData为空,则此模板不会打印任何行。 我希望我能说清楚。 有关标签的更多信息,请参阅http://jxls.sourcefo ...