HSSFWorkBooK用法 —Excel表的导出和设置
2013年02月21日 ⁄ 综合 ⁄ 共 9248字 ⁄ 字号 小 中 大
- public ActionResult excelPrint() {
- HSSFWorkbook workbook = new HSSFWorkbook();
- HSSFSheet sheet = workbook.createSheet();
- sheet.createFreezePane(1, 3);
-
- sheet.setColumnWidth(0, 1000);
- sheet.setColumnWidth(1, 3500);
- sheet.setColumnWidth(2, 3500);
- sheet.setColumnWidth(3, 6500);
- sheet.setColumnWidth(4, 6500);
- sheet.setColumnWidth(5, 6500);
- sheet.setColumnWidth(6, 6500);
- sheet.setColumnWidth(7, 2500);
-
- HSSFCellStyle sheetStyle = workbook.createCellStyle();
-
- sheetStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
-
- sheetStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
-
- sheetStyle.setFillPattern(HSSFCellStyle.FINE_DOTS);
-
- for (int i = 0; i <= 14; i++) {
- sheet.setDefaultColumnStyle((short) i, sheetStyle);
- }
-
- HSSFFont headfont = workbook.createFont();
- headfont.setFontName("黑体");
- headfont.setFontHeightInPoints((short) 22);
- headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
-
- HSSFCellStyle headstyle = workbook.createCellStyle();
- headstyle.setFont(headfont);
- headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
- headstyle.setLocked(true);
- headstyle.setWrapText(true);
-
- HSSFFont columnHeadFont = workbook.createFont();
- columnHeadFont.setFontName("宋体");
- columnHeadFont.setFontHeightInPoints((short) 10);
- columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
-
- HSSFCellStyle columnHeadStyle = workbook.createCellStyle();
- columnHeadStyle.setFont(columnHeadFont);
- columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
- columnHeadStyle.setLocked(true);
- columnHeadStyle.setWrapText(true);
- columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);
- columnHeadStyle.setBorderLeft((short) 1);
- columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);
- columnHeadStyle.setBorderRight((short) 1);
- columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
- columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index);
-
- columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);
-
- HSSFFont font = workbook.createFont();
- font.setFontName("宋体");
- font.setFontHeightInPoints((short) 10);
-
- HSSFCellStyle style = workbook.createCellStyle();
- style.setFont(font);
- style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
- style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
- style.setWrapText(true);
- style.setLeftBorderColor(HSSFColor.BLACK.index);
- style.setBorderLeft((short) 1);
- style.setRightBorderColor(HSSFColor.BLACK.index);
- style.setBorderRight((short) 1);
- style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
- style.setBottomBorderColor(HSSFColor.BLACK.index);
- style.setFillForegroundColor(HSSFColor.WHITE.index);
-
- HSSFCellStyle centerstyle = workbook.createCellStyle();
- centerstyle.setFont(font);
- centerstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- centerstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
- centerstyle.setWrapText(true);
- centerstyle.setLeftBorderColor(HSSFColor.BLACK.index);
- centerstyle.setBorderLeft((short) 1);
- centerstyle.setRightBorderColor(HSSFColor.BLACK.index);
- centerstyle.setBorderRight((short) 1);
- centerstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
- centerstyle.setBottomBorderColor(HSSFColor.BLACK.index);
- centerstyle.setFillForegroundColor(HSSFColor.WHITE.index);
-
- try {
-
- HSSFRow row0 = sheet.createRow(0);
-
- row0.setHeight((short) 900);
-
- HSSFCell cell0 = row0.createCell(0);
- cell0.setCellValue(new HSSFRichTextString("中非发展基金投资项目调度会工作落实情况对照表"));
- cell0.setCellStyle(headstyle);
-
- CellRangeAddress range = new CellRangeAddress(0, 0, 0, 7);
- sheet.addMergedRegion(range);
-
- HSSFRow row1 = sheet.createRow(1);
- HSSFCell cell1 = row1.createCell(0);
- cell1.setCellValue(new HSSFRichTextString("本次会议时间:2009年8月31日 前次会议时间:2009年8月24日"));
- cell1.setCellStyle(centerstyle);
-
- range = new CellRangeAddress(1, 2, 0, 7);
- sheet.addMergedRegion(range);
-
- HSSFRow row2 = sheet.createRow(3);
- row2.setHeight((short) 750);
- HSSFCell cell = row2.createCell(0);
- cell.setCellValue(new HSSFRichTextString("责任者"));
- cell.setCellStyle(columnHeadStyle);
- cell = row2.createCell(1);
- cell.setCellValue(new HSSFRichTextString("成熟度排序"));
- cell.setCellStyle(columnHeadStyle);
- cell = row2.createCell(2);
- cell.setCellValue(new HSSFRichTextString("事项"));
- cell.setCellStyle(columnHeadStyle);
- cell = row2.createCell(3);
- cell.setCellValue(new HSSFRichTextString("前次会议要求/n/新项目的项目概要"));
- cell.setCellStyle(columnHeadStyle);
- cell = row2.createCell(4);
- cell.setCellValue(new HSSFRichTextString("上周工作进展"));
- cell.setCellStyle(columnHeadStyle);
- cell = row2.createCell(5);
- cell.setCellValue(new HSSFRichTextString("本周工作计划"));
- cell.setCellStyle(columnHeadStyle);
- cell = row2.createCell(6);
- cell.setCellValue(new HSSFRichTextString("问题和建议"));
- cell.setCellStyle(columnHeadStyle);
- cell = row2.createCell(7);
- cell.setCellValue(new HSSFRichTextString("备 注"));
- cell.setCellStyle(columnHeadStyle);
-
- List<DeitelVO> deitelVOList = getEntityManager().queryDeitelVOList();
- int m = 4;
- int k = 4;
- for (int i = 0; i < deitelVOList.size(); i++) {
- DeitelVO vo = deitelVOList.get(i);
- String dname = vo.getDname();
- List<Workinfo> workList = vo.getWorkInfoList();
- HSSFRow row = sheet.createRow(m);
- cell = row.createCell(0);
- cell.setCellValue(new HSSFRichTextString(dname));
- cell.setCellStyle(centerstyle);
-
- range = new CellRangeAddress(m, m + workList.size() - 1, 0, 0);
- sheet.addMergedRegion(range);
- m = m + workList.size();
-
- for (int j = 0; j < workList.size(); j++) {
- Workinfo w = workList.get(j);
-
- row = sheet.getRow(k + j);
- if (null == row) {
- row = sheet.createRow(k + j);
- }
- cell = row.createCell(1);
- cell.setCellValue(w.getWnumber());
- cell.setCellStyle(centerstyle);
- cell = row.createCell(2);
- cell.setCellValue(new HSSFRichTextString(w.getWitem()));
- cell.setCellStyle(style);
- cell = row.createCell(3);
- cell.setCellValue(new HSSFRichTextString(w.getWmeting()));
- cell.setCellStyle(style);
- cell = row.createCell(4);
- cell.setCellValue(new HSSFRichTextString(w.getWbweek()));
- cell.setCellStyle(style);
- cell = row.createCell(5);
- cell.setCellValue(new HSSFRichTextString(w.getWtweek()));
- cell.setCellStyle(style);
- cell = row.createCell(6);
- cell.setCellValue(new HSSFRichTextString(w.getWproblem()));
- cell.setCellStyle(style);
- cell = row.createCell(7);
- cell.setCellValue(new HSSFRichTextString(w.getWremark()));
- cell.setCellStyle(style);
- }
- k = k + workList.size();
- }
-
- int footRownumber = sheet.getLastRowNum();
- HSSFRow footRow = sheet.createRow(footRownumber + 1);
- HSSFCell footRowcell = footRow.createCell(0);
- footRowcell.setCellValue(new HSSFRichTextString(" 审 定:XXX 审 核:XXX 汇 总:XX"));
- footRowcell.setCellStyle(centerstyle);
- range = new CellRangeAddress(footRownumber + 1, footRownumber + 1, 0, 7);
- sheet.addMergedRegion(range);
-
- HttpServletResponse response = getResponse();
- HttpServletRequest request = getRequest();
- String filename = "未命名.xls";
-
- filename = Util.encodeFilename(filename, request);
- response.setContentType("application/vnd.ms-excel");
- response.setHeader("Content-disposition", "attachment;filename=" + filename);
- OutputStream ouputStream = response.getOutputStream();
- workbook.write(ouputStream);
- ouputStream.flush();
- ouputStream.close();
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
posted @
2016-03-25 15:03
从未被超越
阅读(
967)
评论()
编辑
收藏
举报