POI中可能会用到一些需要设置EXCEL单元格格式的操作小结

POI中可能会用到一些需要设置EXCEL单元格格式的操作小结:

先获取工作薄对象:

Java代码 复制代码  收藏代码
  1. HSSFWorkbook wb new HSSFWorkbook();  
  2.   
  3. HSSFSheet sheet wb.createSheet();  
  4.   
  5. HSSFCellStyle setBorder wb.createCellStyle();  


一、设置背景色:
Java代码 复制代码  收藏代码
  1. setBorder.setFillForegroundColor((short13);// 设置背景色  
  2. setBorder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  

二、设置边框:

Java代码 复制代码  收藏代码
  1. setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框  
  2. setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框  
  3. setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框  
  4. setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框  


三、设置居中:
Java代码 复制代码  收藏代码
  1. setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中  


四、设置字体:

Java代码 复制代码  收藏代码
  1. HSSFFont font wb.createFont();  
  2. font.setFontName("黑体");  
  3. font.setFontHeightInPoints((short16);//设置字体大小  
  4.   
  5. HSSFFont font2 wb.createFont();  
  6. font2.setFontName("仿宋_GB2312");  
  7. font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示  
  8. font2.setFontHeightInPoints((short12);  
  9.   
  10. setBorder.setFont(font);//选择需要用到的字体格式  


五、设置列宽:

Java代码 复制代码  收藏代码
  1. sheet.setColumnWidth(03766); //第一个参数代表列id(从0开始),第2个参数代表宽度值  


六、设置自动换行:

Java代码 复制代码  收藏代码
  1. setBorder.setWrapText(true);//设置自动换行  


七、合并单元格:

Java代码 复制代码  收藏代码
  1. Region region1 new Region(0(short00(short6);  
  2.   
  3. //参数1:行号 参数2:起始列号 参数3:行号 参数4:终止列号  
  4. sheet.addMergedRegion(region1);  


附一个完整的例子:

Java代码 复制代码  收藏代码
  1. package cn.com.util;  
  2.   
  3. import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
  4. import org.apache.poi.hssf.usermodel.HSSFFont;  
  5. import org.apache.poi.hssf.usermodel.HSSFRichTextString;  
  6. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  7. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  8. import org.apache.poi.hssf.usermodel.HSSFRow;  
  9. import org.apache.poi.hssf.usermodel.HSSFCell;  
  10. import org.apache.poi.hssf.util.CellRangeAddress;  
  11. import org.apache.poi.hssf.util.Region;  
  12. import org.apache.poi.ss.usermodel.CellStyle;  
  13.   
  14. import java.io.FileOutputStream;  
  15.   
  16. import javax.servlet.http.HttpServlet;  
  17.   
  18. public class CreateXL extends HttpServlet  
  19.   
  20. public static String outputFile "c:\\test.xls" 
  21.   
  22. private void cteateCell(HSSFWorkbook wb, HSSFRow row, short col, String val)  
  23. HSSFCell cell row.createCell(col);  
  24. // cell.setEncoding(HSSFCell.ENCODING_UTF_16);  
  25. cell.setCellValue(val);  
  26. HSSFCellStyle cellstyle wb.createCellStyle();  
  27. cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);  
  28. cell.setCellStyle(cellstyle);  
  29. }   
  30.   
  31. public static void main(String argv[])  
  32. try  
  33. // 创建新的Excel 工作簿  
  34. HSSFWorkbook workbook new HSSFWorkbook();  
  35.   
  36. // 设置字体  
  37. HSSFFont font workbook.createFont();  
  38. // font.setColor(HSSFFont.COLOR_RED);  
  39. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
  40. font.setFontHeightInPoints((short14);  
  41.   
  42. // HSSFFont font2 workbook.createFont();  
  43. // font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
  44. // font.setFontHeightInPoints((short)14);  
  45.   
  46. // 设置样式  
  47. HSSFCellStyle cellStyle workbook.createCellStyle();  
  48. cellStyle.setFont(font);  
  49. cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
  50.   
  51. // HSSFCellStyle cellStyle2= workbook.createCellStyle();  
  52. // cellStyle.setFont(font2);  
  53. // cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
  54.   
  55. // 在Excel工作簿中建一工作表,其名为缺省值  
  56. // 如要新建一名为"月报表"的工作表,其语句为:  
  57. HSSFSheet sheet workbook.createSheet("月报表");  
  58. CellRangeAddress cellRangeAddress new CellRangeAddress(000 
  59. 11);  
  60. sheet.addMergedRegion(cellRangeAddress);  
  61.   
  62. //第一行  
  63. // 在索引0的位置创建行(最顶端的行)  
  64. HSSFRow row sheet.createRow(0);  
  65. // 在索引0的位置创建单元格(左上端)  
  66. HSSFCell cell row.createCell(0);  
  67. // 定义单元格为字符串类型  
  68. cell.setCellType(HSSFCell.CELL_TYPE_STRING);  
  69. cell.setCellStyle(cellStyle);  
  70. // 在单元格中输入一些内容  
  71. cell.setCellValue(new HSSFRichTextString("北京亿卡联科技发展有限公司小区门禁维修月报表"));  
  72.   
  73. //第二行  
  74. cellRangeAddress new CellRangeAddress(1136);  
  75. sheet.addMergedRegion(cellRangeAddress);  
  76. row sheet.createRow(1);  
  77. HSSFCell datecell row.createCell(3);  
  78. datecell.setCellType(HSSFCell.CELL_TYPE_STRING);  
  79. datecell.setCellStyle(cellStyle);  
  80. datecell.setCellValue("时间间隔xxxxx");  
  81.   
  82. cellRangeAddress new CellRangeAddress(119 
  83. 10);  
  84. sheet.addMergedRegion(cellRangeAddress);  
  85. row.createCell(9).setCellValue("单位:元");  
  86.   
  87. //第三行  
  88. row=sheet.createRow(2);  
  89. row.createCell(0).setCellValue("一、");  
  90. row.createCell(1).setCellValue("基本资料");  
  91.   
  92. //第4行  
  93. row=sheet.createRow(3);  
  94. row.createCell(1).setCellValue("小区名称:");  
  95. cellRangeAddress=new CellRangeAddress(3,3,2,11);  
  96. sheet.addMergedRegion(cellRangeAddress);  
  97. row.createCell(2).setCellValue("xxxxx");  
  98.   
  99. //第5行  
  100. row=sheet.createRow(4);  
  101. row.createCell(1).setCellValue("座落地点:");  
  102. cellRangeAddress=new CellRangeAddress(4,4,2,11);  
  103. sheet.addMergedRegion(cellRangeAddress);  
  104. row.createCell(2).setCellValue("xxxxx");  
  105.   
  106. //第6行  
  107. row=sheet.createRow(5);  
  108. row.createCell(1).setCellValue("建成年月:");  
  109. cellRangeAddress=new CellRangeAddress(5,5,2,4);  
  110. sheet.addMergedRegion(cellRangeAddress);  
  111. row.createCell(2).setCellValue("年月日:xxxxx");  
  112. row.createCell(5).setCellValue("联系人");  
  113. cellRangeAddress=new CellRangeAddress(5,5,6,8);  
  114. sheet.addMergedRegion(cellRangeAddress);  
  115. row.createCell(6).setCellValue("XXX");  
  116. row.createCell(9).setCellValue("电话");  
  117. cellRangeAddress=new CellRangeAddress(5,5,10,11);  
  118. sheet.addMergedRegion(cellRangeAddress);  
  119. row.createCell(10).setCellValue("XXX");  
  120.   
  121. //第7行  
  122. row=sheet.createRow(6);  
  123. row.createCell(1).setCellValue("住户:");  
  124. row.createCell(2).setCellValue("(XX)");  
  125. row.createCell(3).setCellValue("(户)");  
  126. cellRangeAddress=new CellRangeAddress(6,6,4,5);  
  127. sheet.addMergedRegion(cellRangeAddress);  
  128. row.createCell(4).setCellValue("共计(      )");         
  129. row.createCell(6).setCellValue("幢");  
  130. cellRangeAddress=new CellRangeAddress(6,6,7,8);  
  131. sheet.addMergedRegion(cellRangeAddress);  
  132. row.createCell(7).setCellValue("发卡张数");     
  133. cellRangeAddress=new CellRangeAddress(6,6,9,10);  
  134. sheet.addMergedRegion(cellRangeAddress);  
  135. row.createCell(9).setCellValue("xxxx");     
  136.   
  137.   
  138. //第9行  
  139. row=sheet.createRow(8);  
  140. row.createCell(0).setCellValue("二、");  
  141. cellRangeAddress=new CellRangeAddress(8,8,1,2);  
  142. sheet.addMergedRegion(cellRangeAddress);  
  143. row.createCell(1).setCellValue("维修用材料台账");  
  144. row.createCell(6).setCellValue("三、");  
  145. cellRangeAddress=new CellRangeAddress(8,8,7,9);  
  146. sheet.addMergedRegion(cellRangeAddress);  
  147. row.createCell(7).setCellValue("维修工时记录");  
  148. //第10行  
  149. row=sheet.createRow(9);  
  150. row.createCell(0).setCellValue("日期");  
  151. row.createCell(1).setCellValue("维修事项");  
  152. row.createCell(2).setCellValue("材料清单");  
  153. row.createCell(3).setCellValue("数量");  
  154. row.createCell(4).setCellValue("单价");  
  155. row.createCell(5).setCellValue("材料金额");  
  156.   
  157. row.createCell(7).setCellValue("日期");  
  158. row.createCell(8).setCellValue("技工");  
  159. row.createCell(9).setCellValue("工时数");  
  160. row.createCell(10).setCellValue("单价");  
  161. row.createCell(11).setCellValue("工时金额");  
  162.   
  163. //填充数据  
  164. for (int 010i++)  
  165. row=sheet.createRow(9+i+1);  
  166. row.createCell(0).setCellValue("日期");  
  167. row.createCell(1).setCellValue("维修事项");  
  168. row.createCell(2).setCellValue("材料清单");  
  169. row.createCell(3).setCellValue("数量");  
  170. row.createCell(4).setCellValue("单价");  
  171. row.createCell(5).setCellValue("材料金额");  
  172.   
  173. row.createCell(7).setCellValue("日期");  
  174. row.createCell(8).setCellValue("技工");  
  175. row.createCell(9).setCellValue("工时数");  
  176. row.createCell(10).setCellValue("单价");  
  177. row.createCell(11).setCellValue("工时金额");  
  178. }   
  179.   
  180.   
  181. //第n+10行  
  182. row=sheet.createRow(9+10+1);  
  183. //cellRangeAddress=new CellRangeAddress(19,19,0,4);  
  184. //sheet.addMergedRegion(cellRangeAddress);  
  185. row.createCell(0).setCellValue("累计:");  
  186. row.createCell(1).setCellValue("xxx");  
  187. row.createCell(7).setCellValue("累计:");  
  188. row.createCell(8).setCellValue("xxx");  
  189.   
  190.   
  191.   
  192.   
  193. // 新建一输出文件流  
  194. FileOutputStream fOut new FileOutputStream(outputFile);  
  195. // 把相应的Excel 工作簿存盘  
  196. workbook.write(fOut);  
  197. fOut.flush();  
  198. // 操作结束,关闭文件  
  199. fOut.close();  
  200. System.out.println("文件生成...");  
  201. catch (Exception e)  
  202. System.out.println("已运行 xlCreate() " e);  
  203. }   
  204. }   
  205. }

  206. POI设置EXCEL单元格格式为文本、小数、百分比、货币、日期、科学计数法和中文大写

    标签: POI
    3730人阅读 评论(1) 收藏 举报
    分类:

    1. 第一种:日期格式  
    2.             cell.setCellValue(new Date(2008,5,5));  
    3.             //set date format  
    4.             HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();  
    5.             HSSFDataFormat formatdemoWorkBook.createDataFormat();  
    6.             cellStyle.setDataFormat(format.getFormat("yyyy年m月d日"));  
    7.             cell.setCellStyle(cellStyle);  
    8.    
    9. 第二种:保留两位小数格式  
    10.             cell.setCellValue(1.2);  
    11.             HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();  
    12.             cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));  
    13.             cell.setCellStyle(cellStyle);  
    14.    
    15. 这里与上面有所不同,用的是HSSFDataFormat.getBuiltinFormat()方法,之所以用这个,是因为0.00是Excel内嵌的格式,完整的Excel内嵌格式列表大家可以看这个窗口中的自定义列表:  
    16.   
    17. 这里就不一一列出了  
    18.    
    19. 第三种:货币格式  
    20.             cell.setCellValue(20000);  
    21.             HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();  
    22.             HSSFDataFormat formatdemoWorkBook.createDataFormat();  
    23.             cellStyle.setDataFormat(format.getFormat("¥#,##0"));  
    24.             cell.setCellStyle(cellStyle);  
    25.    
    26. 第四种:百分比格式  
    27.             cell.setCellValue(20);  
    28.             HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();  
    29.             cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%"));  
    30.             cell.setCellStyle(cellStyle);  
    31.   此种情况跟第二种一样  
    32.    
    33. 第五种:中文大写格式  
    34.             cell.setCellValue(20000);  
    35.             HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();  
    36.             HSSFDataFormat formatdemoWorkBook.createDataFormat();  
    37.             cellStyle.setDataFormat(format.getFormat("[DbNum2][$-804]0"));  
    38.             cell.setCellStyle(cellStyle);  
    39.    
    40. 第六种:科学计数法格式  
    41.             cell.setCellValue(20000);  
    42.             HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();  
    43.             cellStyle.setDataFormat( HSSFDataFormat.getBuiltinFormat("0.00E+00"));  
    44.             cell.setCellStyle(cellStyle);  
    45. 此种情况也与第二种情况一样 
     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值