java-poi-excel报表
2017-06-14 09:20
309 查看
java-poi-excel报表
配置
pom.xml<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.5-FINAL</version> </dependency>
代码示例
/** * 拆分主营占比 */ @Override public void splitMainProportion(HttpServletResponse response){ OutputStream os = null; HSSFWorkbook wb = null; List<List<String>> resultList = new ArrayList<>(); try { resultList = getDatas();//获取数据内容 List<String> titleList = new ArrayList<>(); titleList.add("房地产主营"); titleList.add("证券代码"); titleList.add("产品名称"); titleList.add("占比"); titleList.add("图谱节点"); wb = new HSSFWorkbook(); HSSFSheet outHs = wb.createSheet("房地产主营"); createExcelTitle(wb, outHs, titleList);//创建标题 createExcelContent(wb, resultList, outHs);//创建内容 response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=mainRealEstate.xls"); os = response.getOutputStream(); wb.write(os); }catch (Exception e){ e.printStackTrace(); }finally { try { if (os != null) {//不关闭资源,会导致java内存泄漏哦 os.close(); os = null; } }catch (Exception e){ e.printStackTrace(); } } } 4000 /** * 获取数据内容 * @return * @throws IOException */ private List<List<String>> getDatas() throws IOException { List<List<String>> resultList = new ArrayList<>(); HSSFWorkbook wb = null; wb = new HSSFWorkbook(new FileInputStream("C:\\Users\\Administrator\\Desktop\\task\\graphData\\房地产主营\\房地产主营.xls"));//创建工作簿 HSSFSheet hs = wb.getSheetAt(0); HSSFRow row = null; for(int i=1;i < hs.getLastRowNum();i++){ row = hs.getRow(i); String stockCode = row.getCell(1).getStringCellValue(); String stockName = row.getCell(2).getStringCellValue(); String[] productOccupation = row.getCell(3).getStringCellValue().split(",,"); for(String str : productOccupation){ if(str.contains(":")){ String[] proOccu = str.split(":"); List<String> list = new ArrayList<>(); list.add(stockCode); list.add(stockName); list.add(proOccu[0]); list.add(proOccu[1]); list.add(ksi.getGraphByProductName(proOccu[0])); resultList.add(list); } } } return resultList; } /** * 创建excel内容 * @param wb 工作簿 * @param resultList 内容数据 * @param outHs sheet页 */ private void createExcelContent(HSSFWorkbook wb, List<List<String>> resultList, HSSFSheet outHs) { HSSFRow row = null; for(int i = 0; i<resultList.size(); i++){ row = outHs.createRow(i+1); List<String> oneContentList = resultList.get(i); for(int j=0;j<oneContentList.size();j++){ row.createCell(j).setCellStyle(getStyle(wb)); row.getCell(j).setCellValue(oneContentList.get(j)); } } } /** * 创建excel标题 * @param wb 工作簿 * @param outHs sheel页 * @param titleList 标题集合 */ private void createExcelTitle(HSSFWorkbook wb, HSSFSheet outHs, List<String> titleList) { HSSFRow row = outHs.createRow(0); for(int i=0;i<titleList.size();i++){ outHs.setColumnWidth(i, 20 * 256);//动态设置宽度 row.createCell(i).setCellStyle(getColumnTopStyle(wb)); row.getCell(i).setCellValue(titleList.get(i)); } } /** * 列头单元格样式 * @param workbook * @return */ public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) { // 设置字体 HSSFFont font = workbook.createFont(); //设置字体大小 font.setFontHeightInPoints((short)11); //字体加粗 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //设置字体名字 font.setFontName("Courier New"); //设置样式; HSSFCellStyle style = workbook.createCellStyle(); //设置底边框; style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //设置底边框颜色; style.setBottomBorderColor(HSSFColor.BLACK.index); //设置左边框; style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //设置左边框颜色; style.setLeftBorderColor(HSSFColor.BLACK.index); //设置右边框; style.setBorderRight(HSSFCellStyle.BORDER_THIN); //设置右边框颜色; style.setRightBorderColor(HSSFColor.BLACK.index); //设置顶边框; style.setBorderTop(HSSFCellStyle.BORDER_THIN); //设置顶边框颜色; style.setTopBorderColor(HSSFColor.BLACK.index); //在样式用应用设置的字体; style.setFont(font); //设置自动换行; style.setWrapText(false); //设置水平对齐的样式为居中对齐; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //设置垂直对齐的样式为居中对齐; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } /** * 列数据信息单元格样式 * @param workbook * @return */ public HSSFCellStyle getStyle(HSSFWorkbook workbook) { // 设置字体 HSSFFont font = workbook.createFont(); //设置字体大小 //font.setFontHeightInPoints((short)10); //字体加粗 //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //设置字体名字 font.setFontName("Courier New"); //设置样式; HSSFCellStyle style = workbook.createCellStyle(); //设置底边框; style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //设置底边框颜色; style.setBottomBorderColor(HSSFColor.BLACK.index); //设置左边框; style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //设置左边框颜色; style.setLeftBorderColor(HSSFColor.BLACK.index); //设置右边框; style.setBorderRight(HSSFCellStyle.BORDER_THIN); //设置右边框颜色; style.setRightBorderColor(HSSFColor.BLACK.index); //设置顶边框; style.setBorderTop(HSSFCellStyle.BORDER_THIN); //设置顶边框颜色; style.setTopBorderColor(HSSFColor.BLACK.index); //在样式用应用设置的字体; style.setFont(font); //设置自动换行; style.setWrapText(false); //设置水平对齐的样式为居中对齐; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //设置垂直对齐的样式为居中对齐; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; }
简单的实现了对excel文件的读和写以及涵盖了文件下载的功能
相关文章推荐
- Java-Excel报表开发POI
- java使用POI操作Excel报表
- Java和poi导出excel报表
- javaweb 利用POI生成excel报表 相关代码
- Java实现POI导出Excel报表功能
- 引用 JAVA POI导出EXCEL报表的操作
- Java使用POI实现数据导出excel报表
- 详解JAVA POI导出EXCEL报表的操作(包括各种格式及样式的实现)
- 10、借助POI实现Java生成并打印excel报表(1)
- Java-Excel报表开发POI(转于老彭的博客)
- Java-Excel 报表开发 POI
- Java使用POI实现数据导出excel报表
- Java程序员从笨鸟到菜鸟之(一百零四)java操作office和pdf文件(二)利用POI实现数据导出excel报表
- Java使用POI实现数据导出excel报表
- Java Excel 报表开发POI
- java利用poi生成excel报表
- Java程序员从笨鸟到菜鸟之(一百零五)java操作office和pdf文件(三)利用jxl实现数据导出excel报表以及与POI的区别
- Java程序员从笨鸟到菜鸟之(一百零四)java操作office和pdf文件(二)利用POI实现数据导出excel报表
- Java使用POI实现数据导出excel报表
- Java使用POI实现数据导出excel报表