excel
2016-05-05 14:44
288 查看
excel try { HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(path + "/report/model.xls"));// 声明一个工作薄 HSSFSheet sheet = workbook.createSheet(sheetName); workbook.removeSheetAt(workbook.getSheetIndex("Sheet"));//删除默认可视sheet HSSFCellStyle headerStyle = createHeaderStyle(workbook); HSSFCellStyle textStyle = createTextStyle(workbook); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); fileName = "/report/"+excelName + "_"+ sdf.format(new Date()) + ".xls"; //产生表格标题行 HSSFRow row = sheet.createRow(0); for(int i = 0; i < headers.size(); i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(headerStyle); cell.setCellValue(headers.get(i)); int clength = headers.get(i).length()*600; if(clength > sheet.getColumnWidth(i)){ sheet.setColumnWidth(i, clength); }else{ sheet.setColumnWidth(i, sheet.getColumnWidth(i)); } } //遍历集合数据,产生数据行 if(dataset != null){ for(int index = 0; index < dataset.size() ; index++){ row = sheet.createRow(index + 1); ExcelObj bean = (ExcelObj) dataset.get(index); String[] strs = bean.changeMetadatas(); HSSFCell cell0 = row.createCell(0); cell0.setCellStyle(textStyle); cell0.setCellValue(startIndex + index + 1 + ""); for (short i = 0; i < props.size(); i++) { HSSFCell cell = row.createCell(i + 1); cell.setCellStyle(textStyle); if(null == strs[i] || strs[i].equals("")){ cell.setCellValue(""); }else{ cell.setCellValue(strs[i]); // sheet.autoSizeColumn(i); } } } } //写出文件流 String nowPath = ""; if(path.endsWith(".xls")){ nowPath = path; }else{ nowPath = path + fileName; } File file = new File(nowPath); if(!file.exists()){ file.createNewFile(); } fos = new FileOutputStream(file); workbook.write(fos); } catch (Exception e) { logger.error("异常", e); } finally { try { if(fos != null) fos.close(); } catch (IOException e) { logger.error("异常", e); } }
相关文章推荐
- c#的static什么时候使用
- jQuery验证控件jquery.validate.js使用说明+中文API - Rex.He - 博客园
- Leetcode - Valid Anagram
- ADB不识别device not found
- Activity之间参数传递方式
- CGI-BIN目录
- hdu4635
- 移植u-boot到mini2440--SPL初探
- @Register指令
- 在静态函数调用指针函数写法
- 如何画Flot折线图
- textarea框换行处理
- ES6 let
- android OTG支持问题整理
- 【深入理解JVM】:垃圾收集(GC)概述
- 十八 访问限制
- c#调用带有安全认证的java webservice
- 大型网站系统架构演化之路
- 图片下载工具类,可更新UI
- plsql中&符号的处理