利用ApachePOI输出Excel文件
2015-10-20 11:41
579 查看
代码中使用getSheetAt,getRow,getCell,是因为代码是复制模板excel文件然后将数据写入文件中,所以单元格都是已经存在的,如果是创建文件的话,这些地方均使用对应的create方法即可。
具体方法可查看官方doc,介绍的非常清楚
官方文档网址:http://poi.apache.org/spreadsheet/quick-guide.html
具体方法可查看官方doc,介绍的非常清楚
官方文档网址:http://poi.apache.org/spreadsheet/quick-guide.html
<pre name="code" class="java" style="font-family: 宋体; font-size: 21.3333px; background-color: rgb(255, 255, 255);"><pre name="code" class="java">def briefing = Briefing.findById(Long.parseLong(docId)) String realPath = ServletContextHolder.getServletContext().getRealPath('/') File fileSource = new File(realPath + '/download/template/template.xlsx') File dir = new File(realPath + '/download/' + briefing.year) try { if (!dir.exists()) dir.mkdir() def monitor = MonitorEntity.findById(1) //id=1 means sy File fileTarget = new File(dir.getPath() + "\\" + monitor.name + "_" + briefing.year + "年第" + briefing.week + "期.xlsx") if (!fileTarget.exists()) { int sheetNum = 0 //第*表 def entityId = monitor.id.toString() nioTransferCopy(fileSource, fileTarget)//文件复制 InputStream inp = new FileInputStream(fileTarget); XSSFWorkbook xwb = new XSSFWorkbook(OPCPackage.open(inp)) xwb.setActiveSheet(0); XSSFSheet sheet = xwb.getSheetAt(sheetNum++); def resultMap = source4EntityWeekly(entityId, docId) int rowIndex_1 = ROW_LINE resultMap.each { key, value -> XSSFRow row = sheet.getRow(rowIndex_1); XSSFCell cellSource = row.getCell(0) cellSource.setCellValue(String.valueOf(key)) XSSFCell cellCount = row.getCell(1) cellCount.setCellValue(value) rowIndex_1++ } } xwb.setActiveSheet(0) OutputStream out = new FileOutputStream(fileTarget); xwb.write(out); inp.close(); out.close(); } return fileTarget }
相关文章推荐
- [异常] Java文件上传异常org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of mul
- apache24 django1.8.2 mod_wsgi 集成
- 阿里云 Ubuntu操作系统 配置虚拟主机
- Ubuntu14.04+Python3.4+apache2.4+Django1.7站点发布笔记
- apache 服务无法启动 wampserver安装问题
- CentOS 6.5下源码安装LAMP(Linux+Apache+Mysql+Php)环境
- <学习笔记>java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addServlet
- 使用apt-get安装服务器apache2+php+mysql
- devstack apache2/keystone 没有启动
- 高性能jdbc封装工具 Apache Commons DbUtils 1.6(转载)
- Apache2.4.6无需配置NameVirtualHost
- ERROR org.apache.hadoop.hdfs.server.datanode.DataNode: java.io.IOException: Cannot lock storage /op
- Windows下Apache服务器中自动配置二级子域名
- Apache 、PHP的安装
- ubuntu apache24 安装
- linux apache 查看IP连接数
- Meet Apache Wicket
- apache.commons.lang.StringUtils 的一些用法
- Apache中 RewriteRule 规则参数介绍
- Apache站点默认首页设置