POI Excel导出样式设置

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. HSSFSheet sheet = workbook.createSheet("sheetName");    //创建sheet  
  2. sheet.setVerticallyCenter(true);  
  3.   
  4. //下面样式可作为导出左右分栏的表格模板  
  5. sheet.setColumnWidth((short) 0, (short) 2600);// 设置列宽  
  6. sheet.setColumnWidth((short) 1, (short) 2400);  
  7. sheet.setColumnWidth((short) 2, (short) 2300);  
  8. sheet.setColumnWidth((short) 3, (short) 1600);  
  9. sheet.setColumnWidth((short) 4, (short) 1800);  
  10. sheet.setColumnWidth((short) 5, (short) 1000);// 空列设置小一些  
  11. sheet.setColumnWidth((short) 6, (short) 2600);// 设置列宽  
  12. sheet.setColumnWidth((short) 7, (short) 2400);  
  13. sheet.setColumnWidth((short) 8, (short) 2300);  
  14. sheet.setColumnWidth((short) 9, (short) 1600);  
  15. sheet.setColumnWidth((short) 10, (short) 1800);  
  16.   
  17. HSSFCellStyle cellstyle = (HSSFCellStyle) workbook.createCellStyle();// 设置表头样式  
  18. cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 设置居中  
  19.   
  20. HSSFCellStyle headerStyle = (HSSFCellStyle) workbook .createCellStyle();// 创建标题样式  
  21. headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);    //设置垂直居中  
  22. headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);   //设置水平居中  
  23. HSSFFont headerFont = (HSSFFont) workbook.createFont(); //创建字体样式  
  24. headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗  
  25. headerFont.setFontName("Times New Roman");  //设置字体类型  
  26. headerFont.setFontHeightInPoints((short) 8);    //设置字体大小  
  27. headerStyle.setFont(headerFont);    //为标题样式设置字体样式  
  28.   
  29. HSSFCellStyle headerStyle1 = (HSSFCellStyle) workbook .createCellStyle();// 创建标题样式1  
  30. headerStyle1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  
  31. headerStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
  32. HSSFFont headerFont1 = (HSSFFont) workbook.createFont();  
  33. headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字体加粗  
  34. headerFont1.setFontName("Times New Roman");  
  35. headerFont1.setFontHeightInPoints((short) 8);  
  36. headerStyle1.setFont(headerFont1);  
  37.   
  38. HSSFCellStyle headerStyle2 = (HSSFCellStyle) workbook .createCellStyle();// 创建标题样式2  
  39. headerStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  
  40. headerStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
  41. HSSFFont headerFont2 = (HSSFFont) workbook.createFont();  
  42. headerFont2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字体加粗  
  43. headerFont2.setFontName("Times New Roman");  
  44. headerFont2.setFontHeightInPoints((short) 8);  
  45. headerStyle2.setFont(headerFont2);  
  46. headerStyle2.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框  
  47. headerStyle2.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框  
  48. headerStyle2.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框  
  49. headerStyle2.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框  
  50.   
  51. HSSFCellStyle cell_Style = (HSSFCellStyle) workbook .createCellStyle();// 设置字体样式  
  52. cell_Style.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
  53. cell_Style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直对齐居中  
  54. cell_Style.setWrapText(true); // 设置为自动换行  
  55. HSSFFont cell_Font = (HSSFFont) workbook.createFont();  
  56. cell_Font.setFontName("宋体");  
  57. cell_Font.setFontHeightInPoints((short) 8);  
  58. cell_Style.setFont(cell_Font);  
  59. cell_Style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框  
  60. cell_Style.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框  
  61. cell_Style.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框  
  62. cell_Style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框  
  63.   
  64. HSSFRow row = sheet.createRow((short)1);    //创建行  
  65. HSSFCell cell = row.createCell((short)1);   //创建列  
  66. cell.setCellStyle(headerStyle2);    //单元格引用样式  


POI-Excel导出样式设置.java67行Java

posted @ 2016-04-11 15:00  牧之丨  阅读(822)  评论(0编辑  收藏  举报