博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poi 导出 excel
阅读量:6574 次
发布时间:2019-06-24

本文共 6483 字,大约阅读时间需要 21 分钟。

private void exportAssetExcel(HttpServletRequest request,            HttpServletResponse response) throws IOException {        String ogid = RequestUtil.getRequestBytes(request, "ogid").trim();        String objectgroupname = RequestUtil.getRequestBytes(request, "objectgroupname").trim();        String excelTitle = new String(objectgroupname.getBytes("ISO-8859-1"), "utf-8");        if(objectgroupname==null || objectgroupname==""){            objectgroupname = "备播单";        }        ObjectGroupRObjectMgr ogroMgr = new ObjectGroupRObjectMgr();        BaseDAO basedao = ogroMgr.getBasedao();        String strCondition= "select "                + " ogro.new_assetname,"                + " a.assetname,"                + " m.filepath,"                + " a.programtype,"                + " a.videotype,"                + " a.duration,"                + " ogro.asset_price,"                + " ogro.online_time,"                + " ogro.asset_path"                + " from "                + " Objectgrouprobject ogro,"                + " movie m,"                + " asset a "                + " where "                + " ogro.objectid = a.assetid"                + " and a.assetid = m.objectguid"                + " and ogro.ogid = ?"                + " order by ogro.ordernum asc";        List
paramlist = new ArrayList(); paramlist.add(ogid); // 关联objectgrouprobject asset查询 List
list = basedao.SQLQuery(strCondition, paramlist); //创建一个workbook 对应一个excel文件 HSSFWorkbook book = new HSSFWorkbook(); //创建工作簿,对应一个sheet HSSFSheet sheet = book.createSheet(excelTitle); sheet.setColumnWidth((short)0,(short)1000); sheet.setColumnWidth((short)1,(short)4000); sheet.setColumnWidth((short)2,(short)4000); sheet.setColumnWidth((short)3,(short)10000); sheet.setColumnWidth((short)4,(short)4000); sheet.setColumnWidth((short)5,(short)4000); sheet.setColumnWidth((short)6,(short)4000); sheet.setColumnWidth((short)7,(short)2000); sheet.setColumnWidth((short)8,(short)4000); sheet.setColumnWidth((short)9,(short)10000); sheet.setColumnWidth((short)10,(short)10000); //设置格式 HSSFCellStyle style_1 = book.createCellStyle(); style_1.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFFont f = book.createFont(); f.setFontHeightInPoints((short) 14);//字号 f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);//加粗 style_1.setFont(f); //创建第一行 合并单元格 HSSFRow firstRow = sheet.createRow(0); sheet.addMergedRegion(new Region(0, (short)0, 0, (short)10)); //组织第一行的数据 HSSFCell cell = firstRow.createCell((short) 0); cell.setCellValue(excelTitle); cell.setCellStyle(style_1);// cell.setCellType(HSSFCell.ENCODING_UTF_16); //创建第二行 合并单元格 HSSFRow secondeRow = sheet.createRow(1); sheet.addMergedRegion(new Region(1, (short)0, 1, (short)8)); //组织第二行的数据 Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); String calendarStr = year + "年" + month + "月" + day + "日"; cell = secondeRow.createCell((short)0); cell.setCellValue("制表时间: "+ calendarStr); cell = secondeRow.createCell((short)9); cell.setCellValue("制表部门:内容(集成)中心"); cell = secondeRow.createCell((short)10); cell.setCellValue("节目单类型:上片单"); //组织第三行数据 HSSFRow thirdRow = sheet.createRow(2); HSSFCellStyle style_2 = book.createCellStyle(); style_2.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFFont f2 = book.createFont(); f2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);//加粗 style_2.setFont(f2); HSSFCell third_cell = thirdRow.createCell((short)0); third_cell = thirdRow.createCell((short)1); third_cell.setCellValue("节目名称"); third_cell.setCellStyle(style_2); third_cell = thirdRow.createCell((short)2); third_cell.setCellValue("正题名称"); third_cell.setCellStyle(style_2); third_cell = thirdRow.createCell((short)3); third_cell.setCellValue("文件名称"); third_cell.setCellStyle(style_2); third_cell = thirdRow.createCell((short)4); third_cell.setCellValue("节目类型"); third_cell.setCellStyle(style_2); third_cell = thirdRow.createCell((short)5); third_cell.setCellValue("高标清"); third_cell.setCellStyle(style_2); third_cell = thirdRow.createCell((short)6); third_cell.setCellValue("节目时长"); third_cell.setCellStyle(style_2); third_cell = thirdRow.createCell((short)7); third_cell.setCellValue("价格"); third_cell.setCellStyle(style_2); third_cell = thirdRow.createCell((short)8); third_cell.setCellValue("上片时间"); third_cell = thirdRow.createCell((short)9); third_cell.setCellValue("节目路径"); third_cell.setCellStyle(style_2); third_cell = thirdRow.createCell((short)10); third_cell.setCellValue("备注"); third_cell.setCellStyle(style_2); int listSize = 0; if(list!=null && list.size()>0){ listSize = list.size(); HSSFCellStyle style_3 = book.createCellStyle(); style_3.setAlignment(HSSFCellStyle.ALIGN_CENTER); style_3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //组织节目数据 for(int i=0; i
150) {                   newFileName = new String(filename.getBytes("GB2312"), "ISO8859-1");                   newFileName = StringUtils.replace(newFileName, " ", "%20");                 }                 return newFileName;               }               if ((agent != null) && (-1 != agent.indexOf("Mozilla")))                 return MimeUtility.encodeText(filename, "UTF-8", "B");                        return filename;             } catch (Exception ex) {               return filename;             }           }

以上是java部分的代码, 导出按照workbook-->sheet-->row-->cell的大体流程走, 再加上格式设置, 加上值等等

前端需要弹出下载框,可以用:

window.open("objectgroup.do"+"?action=exportAssetExcel&ogid="+ogid+"&objectgroupname="+objectgroupname);

或者用form提交

jsp页面

js处理

$("#searchXML").val(searchXML);   $("#exportExcel_program_form").submit();

 

这里参数可能有中文,所以java部分要处理,可以参见上面java代码

转载于:https://www.cnblogs.com/rocky-fang/p/5422241.html

你可能感兴趣的文章
JavaScript高级程序设计(5) 引用类型 (上)
查看>>
QT学习-10/31/2012
查看>>
python学习交流 - 匿名函数
查看>>
文章1(转)
查看>>
schedule调用相关整理
查看>>
node.js-session问题
查看>>
拦截器和过滤器的区别 -- 简单分析篇
查看>>
Python版本微信跳一跳,软件配置
查看>>
PropertyGrid仿VS的属性事件窗口
查看>>
ahjesus自定义隐式转换和显示转换
查看>>
@PathVariable、@RequestHeader与@CookieValue注解的使用案例
查看>>
【笔记】jquery判断两个日期之间相差多少天
查看>>
PYTHON1.day01
查看>>
CSS 定位 (Positioning) 实例
查看>>
css怎么写链接到图片和地址
查看>>
js--小结⑥---typeof
查看>>
从别的网站摘抄的,挺有用的
查看>>
更改一个主键的列的类型的步骤
查看>>
neo4j 如何删除所以的节点和关系
查看>>
Markdown的常用使用语法
查看>>