POI

转自:http://zmx.iteye.com/blog/622536

 

关于POI的介绍请google一下!

 

Java代码     收藏代码
  1. public ActionResult excelPrint() {  
  2.     HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件  
  3.     HSSFSheet sheet = workbook.createSheet();// 创建一个Excel的Sheet  
  4.     sheet.createFreezePane(13);// 冻结  
  5.     // 设置列宽  
  6.     sheet.setColumnWidth(01000);  
  7.     sheet.setColumnWidth(13500);  
  8.     sheet.setColumnWidth(23500);  
  9.     sheet.setColumnWidth(36500);  
  10.     sheet.setColumnWidth(46500);  
  11.     sheet.setColumnWidth(56500);  
  12.     sheet.setColumnWidth(66500);  
  13.     sheet.setColumnWidth(72500);  
  14.     // Sheet样式  
  15.     HSSFCellStyle sheetStyle = workbook.createCellStyle();  
  16.     // 背景色的设定  
  17.     sheetStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);  
  18.     // 前景色的设定  
  19.     sheetStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);  
  20.     // 填充模式  
  21.     sheetStyle.setFillPattern(HSSFCellStyle.FINE_DOTS);  
  22.     // 设置列的样式  
  23.     for (int i = 0; i <= 14; i++) {  
  24.       sheet.setDefaultColumnStyle((short) i, sheetStyle);  
  25.     }  
  26.     // 设置字体  
  27.     HSSFFont headfont = workbook.createFont();  
  28.     headfont.setFontName("黑体");  
  29.     headfont.setFontHeightInPoints((short22);// 字体大小  
  30.     headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗  
  31.     // 另一个样式  
  32.     HSSFCellStyle headstyle = workbook.createCellStyle();  
  33.     headstyle.setFont(headfont);  
  34.     headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中  
  35.     headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中  
  36.     headstyle.setLocked(true);  
  37.     headstyle.setWrapText(true);// 自动换行  
  38.     // 另一个字体样式  
  39.     HSSFFont columnHeadFont = workbook.createFont();  
  40.     columnHeadFont.setFontName("宋体");  
  41.     columnHeadFont.setFontHeightInPoints((short10);  
  42.     columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
  43.     // 列头的样式  
  44.     HSSFCellStyle columnHeadStyle = workbook.createCellStyle();  
  45.     columnHeadStyle.setFont(columnHeadFont);  
  46.     columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中  
  47.     columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中  
  48.     columnHeadStyle.setLocked(true);  
  49.     columnHeadStyle.setWrapText(true);  
  50.     columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色  
  51.     columnHeadStyle.setBorderLeft((short1);// 边框的大小  
  52.     columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色  
  53.     columnHeadStyle.setBorderRight((short1);// 边框的大小  
  54.     columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体  
  55.     columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色  
  56.     // 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)  
  57.     columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);  
  58.   
  59.     HSSFFont font = workbook.createFont();  
  60.     font.setFontName("宋体");  
  61.     font.setFontHeightInPoints((short10);  
  62.     // 普通单元格样式  
  63.     HSSFCellStyle style = workbook.createCellStyle();  
  64.     style.setFont(font);  
  65.     style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中  
  66.     style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 上下居中  
  67.     style.setWrapText(true);  
  68.     style.setLeftBorderColor(HSSFColor.BLACK.index);  
  69.     style.setBorderLeft((short1);  
  70.     style.setRightBorderColor(HSSFColor.BLACK.index);  
  71.     style.setBorderRight((short1);  
  72.     style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体  
  73.     style.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.  
  74.     style.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色.  
  75.     // 另一个样式  
  76.     HSSFCellStyle centerstyle = workbook.createCellStyle();  
  77.     centerstyle.setFont(font);  
  78.     centerstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中  
  79.     centerstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中  
  80.     centerstyle.setWrapText(true);  
  81.     centerstyle.setLeftBorderColor(HSSFColor.BLACK.index);  
  82.     centerstyle.setBorderLeft((short1);  
  83.     centerstyle.setRightBorderColor(HSSFColor.BLACK.index);  
  84.     centerstyle.setBorderRight((short1);  
  85.     centerstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体  
  86.     centerstyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.  
  87.     centerstyle.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色.  
  88.   
  89.     try {  
  90.       // 创建第一行  
  91.       HSSFRow row0 = sheet.createRow(0);  
  92.       // 设置行高  
  93.       row0.setHeight((short900);  
  94.       // 创建第一列  
  95.       HSSFCell cell0 = row0.createCell(0);  
  96.       cell0.setCellValue(new HSSFRichTextString("中非发展基金投资项目调度会工作落实情况对照表"));  
  97.       cell0.setCellStyle(headstyle);  
  98.       /** 
  99.        * 合并单元格 
  100.        *    第一个参数:第一个单元格的行数(从0开始) 
  101.        *    第二个参数:第二个单元格的行数(从0开始) 
  102.        *    第三个参数:第一个单元格的列数(从0开始) 
  103.        *    第四个参数:第二个单元格的列数(从0开始) 
  104.        */  
  105.       CellRangeAddress range = new CellRangeAddress(0007);  
  106.       sheet.addMergedRegion(range);  
  107.       // 创建第二行  
  108.       HSSFRow row1 = sheet.createRow(1);  
  109.       HSSFCell cell1 = row1.createCell(0);  
  110.       cell1.setCellValue(new HSSFRichTextString("本次会议时间:2009年8月31日       前次会议时间:2009年8月24日"));  
  111.       cell1.setCellStyle(centerstyle);  
  112.       // 合并单元格  
  113.       range = new CellRangeAddress(1207);  
  114.       sheet.addMergedRegion(range);  
  115.       // 第三行  
  116.       HSSFRow row2 = sheet.createRow(3);  
  117.       row2.setHeight((short750);  
  118.       HSSFCell cell = row2.createCell(0);  
  119.       cell.setCellValue(new HSSFRichTextString("责任者"));  
  120.       cell.setCellStyle(columnHeadStyle);  
  121.       cell = row2.createCell(1);  
  122.       cell.setCellValue(new HSSFRichTextString("成熟度排序"));  
  123.       cell.setCellStyle(columnHeadStyle);  
  124.       cell = row2.createCell(2);  
  125.       cell.setCellValue(new HSSFRichTextString("事项"));  
  126.       cell.setCellStyle(columnHeadStyle);  
  127.       cell = row2.createCell(3);  
  128.       cell.setCellValue(new HSSFRichTextString("前次会议要求\n/新项目的项目概要"));  
  129.       cell.setCellStyle(columnHeadStyle);  
  130.       cell = row2.createCell(4);  
  131.       cell.setCellValue(new HSSFRichTextString("上周工作进展"));  
  132.       cell.setCellStyle(columnHeadStyle);  
  133.       cell = row2.createCell(5);  
  134.       cell.setCellValue(new HSSFRichTextString("本周工作计划"));  
  135.       cell.setCellStyle(columnHeadStyle);  
  136.       cell = row2.createCell(6);  
  137.       cell.setCellValue(new HSSFRichTextString("问题和建议"));  
  138.       cell.setCellStyle(columnHeadStyle);  
  139.       cell = row2.createCell(7);  
  140.       cell.setCellValue(new HSSFRichTextString("备 注"));  
  141.       cell.setCellStyle(columnHeadStyle);  
  142.       // 访问数据库,得到数据集  
  143.       List<DeitelVO> deitelVOList = getEntityManager().queryDeitelVOList();  
  144.      
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值