JAVA poi设置单元格背景颜色

摘要:
importjava.io.FileOutputStream;importjava.io.IOException;importorg.apache.poi.ss.usermodel.Cell;importorg.apache.poi.ss.usermodel.CellStyle;importorg.apache.poi.ss.usermodel.IndexedColors;importorg.ap
  1. import java.io.FileOutputStream;
  2. import java.io.IOException;
  3. import org.apache.poi.ss.usermodel.Cell;
  4. import org.apache.poi.ss.usermodel.CellStyle;
  5. import org.apache.poi.ss.usermodel.IndexedColors;
  6. import org.apache.poi.ss.usermodel.Row;
  7. import org.apache.poi.ss.usermodel.Sheet;
  8. import org.apache.poi.ss.usermodel.Workbook;
  9. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  10. public class POIFillAndColorExample {
  11. public static void main(String[] args) throws IOException {
  12. // Create a workbook object
  13. Workbook workbook = new XSSFWorkbook();
  14. // Create sheet
  15. Sheet sheet = workbook.createSheet();
  16. // Create a row and put some cells in it.
  17. Row row = sheet.createRow((short) 1);
  18. // Aqua background
  19. CellStyle style = workbook.createCellStyle();
  20. style.setFillForegroundColor(IndexedColors.AQUA.getIndex());
  21. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  22. Cell cell = row.createCell((short) 1);
  23. cell.setCellValue("X1");
  24. cell.setCellStyle(style);
  25. // Orange "foreground", foreground being the fill foreground not the
  26. // font color.
  27. style = workbook.createCellStyle();
  28. style.setFillForegroundColor(IndexedColors.AUTOMATIC.getIndex());
  29. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  30. cell = row.createCell((short) 2);
  31. cell.setCellValue("X2");
  32. cell.setCellStyle(style);
  33. style = workbook.createCellStyle();
  34. style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
  35. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  36. cell = row.createCell((short) 3);
  37. cell.setCellValue("X3");
  38. cell.setCellStyle(style);
  39. style = workbook.createCellStyle();
  40. style.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
  41. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  42. cell = row.createCell((short) 4);
  43. cell.setCellValue("X4");
  44. cell.setCellStyle(style);
  45. style = workbook.createCellStyle();
  46. style.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
  47. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  48. cell = row.createCell((short) 5);
  49. cell.setCellValue("X5");
  50. cell.setCellStyle(style);
  51. // Create a row and put some cells in it.
  52. Row row2 = sheet.createRow((short) 2);
  53. style = workbook.createCellStyle();
  54. style.setFillForegroundColor(IndexedColors.BROWN.getIndex());
  55. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  56. cell = row2.createCell((short) 1);
  57. cell.setCellValue("X6");
  58. cell.setCellStyle(style);
  59. style = workbook.createCellStyle();
  60. style.setFillForegroundColor(IndexedColors.CORAL.getIndex());
  61. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  62. cell = row2.createCell((short) 2);
  63. cell.setCellValue("X7");
  64. cell.setCellStyle(style);
  65. style = workbook.createCellStyle();
  66. style.setFillForegroundColor(IndexedColors.CORNFLOWER_BLUE.getIndex());
  67. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  68. cell = row2.createCell((short) 3);
  69. cell.setCellValue("X8");
  70. cell.setCellStyle(style);
  71. style = workbook.createCellStyle();
  72. style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
  73. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  74. cell = row2.createCell((short) 4);
  75. cell.setCellValue("X9");
  76. cell.setCellStyle(style);
  77. style = workbook.createCellStyle();
  78. style.setFillForegroundColor(IndexedColors.DARK_GREEN.getIndex());
  79. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  80. cell = row2.createCell((short) 5);
  81. cell.setCellValue("X10");
  82. cell.setCellStyle(style);
  83. // Create a row and put some cells in it.
  84. Row row3 = sheet.createRow((short) 3);
  85. style = workbook.createCellStyle();
  86. style.setFillForegroundColor(IndexedColors.DARK_RED.getIndex());
  87. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  88. cell = row3.createCell((short) 1);
  89. cell.setCellValue("X11");
  90. cell.setCellStyle(style);
  91. style = workbook.createCellStyle();
  92. style.setFillForegroundColor(IndexedColors.DARK_TEAL.getIndex());
  93. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  94. cell = row3.createCell((short) 2);
  95. cell.setCellValue("X12");
  96. cell.setCellStyle(style);
  97. style = workbook.createCellStyle();
  98. style.setFillForegroundColor(IndexedColors.DARK_YELLOW.getIndex());
  99. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  100. cell = row3.createCell((short) 3);
  101. cell.setCellValue("X13");
  102. cell.setCellStyle(style);
  103. style = workbook.createCellStyle();
  104. style.setFillForegroundColor(IndexedColors.GOLD.getIndex());
  105. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  106. cell = row3.createCell((short) 4);
  107. cell.setCellValue("X14");
  108. cell.setCellStyle(style);
  109. style = workbook.createCellStyle();
  110. style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
  111. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  112. cell = row3.createCell((short) 5);
  113. cell.setCellValue("X15");
  114. cell.setCellStyle(style);
  115. // Create a row and put some cells in it.
  116. Row row4 = sheet.createRow((short) 4);
  117. style = workbook.createCellStyle();
  118. style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
  119. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  120. cell = row4.createCell((short) 1);
  121. cell.setCellValue("X16");
  122. cell.setCellStyle(style);
  123. style = workbook.createCellStyle();
  124. style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
  125. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  126. cell = row4.createCell((short) 2);
  127. cell.setCellValue("X17");
  128. cell.setCellStyle(style);
  129. style = workbook.createCellStyle();
  130. style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
  131. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  132. cell = row4.createCell((short) 3);
  133. cell.setCellValue("X18");
  134. cell.setCellStyle(style);
  135. style = workbook.createCellStyle();
  136. style.setFillForegroundColor(IndexedColors.GREY_80_PERCENT.getIndex());
  137. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  138. cell = row4.createCell((short) 4);
  139. cell.setCellValue("X19");
  140. cell.setCellStyle(style);
  141. style = workbook.createCellStyle();
  142. style.setFillForegroundColor(IndexedColors.INDIGO.getIndex());
  143. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  144. cell = row4.createCell((short) 5);
  145. cell.setCellValue("X20");
  146. cell.setCellStyle(style);
  147. // Create a row and put some cells in it.
  148. Row row5 = sheet.createRow((short) 5);
  149. style = workbook.createCellStyle();
  150. style.setFillForegroundColor(IndexedColors.LAVENDER.getIndex());
  151. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  152. cell = row5.createCell((short) 1);
  153. cell.setCellValue("X21");
  154. cell.setCellStyle(style);
  155. style = workbook.createCellStyle();
  156. style.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
  157. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  158. cell = row5.createCell((short) 2);
  159. cell.setCellValue("X22");
  160. cell.setCellStyle(style);
  161. style = workbook.createCellStyle();
  162. style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
  163. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  164. cell = row5.createCell((short) 3);
  165. cell.setCellValue("X23");
  166. cell.setCellStyle(style);
  167. style = workbook.createCellStyle();
  168. style.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
  169. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  170. cell = row5.createCell((short) 4);
  171. cell.setCellValue("X24");
  172. cell.setCellStyle(style);
  173. style = workbook.createCellStyle();
  174. style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
  175. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  176. cell = row5.createCell((short) 5);
  177. cell.setCellValue("X25");
  178. cell.setCellStyle(style);
  179. // Create a row and put some cells in it.
  180. Row row6 = sheet.createRow((short) 6);
  181. style = workbook.createCellStyle();
  182. style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE
  183. .getIndex());
  184. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  185. cell = row6.createCell((short) 1);
  186. cell.setCellValue("X26");
  187. cell.setCellStyle(style);
  188. style = workbook.createCellStyle();
  189. style.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
  190. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  191. cell = row6.createCell((short) 2);
  192. cell.setCellValue("X27");
  193. cell.setCellStyle(style);
  194. style = workbook.createCellStyle();
  195. style.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex());
  196. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  197. cell = row6.createCell((short) 3);
  198. cell.setCellValue("X28");
  199. cell.setCellStyle(style);
  200. style = workbook.createCellStyle();
  201. style.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex());
  202. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  203. cell = row6.createCell((short) 4);
  204. cell.setCellValue("X29");
  205. cell.setCellStyle(style);
  206. style = workbook.createCellStyle();
  207. style.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
  208. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  209. cell = row6.createCell((short) 5);
  210. cell.setCellValue("X30");
  211. cell.setCellStyle(style);
  212. // Create a row and put some cells in it.
  213. Row row7 = sheet.createRow((short) 7);
  214. style = workbook.createCellStyle();
  215. style.setFillForegroundColor(IndexedColors.LIME.getIndex());
  216. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  217. cell = row7.createCell((short) 1);
  218. cell.setCellValue("X31");
  219. cell.setCellStyle(style);
  220. style = workbook.createCellStyle();
  221. style.setFillForegroundColor(IndexedColors.MAROON.getIndex());
  222. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  223. cell = row7.createCell((short) 2);
  224. cell.setCellValue("X32");
  225. cell.setCellStyle(style);
  226. style = workbook.createCellStyle();
  227. style.setFillForegroundColor(IndexedColors.OLIVE_GREEN.getIndex());
  228. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  229. cell = row7.createCell((short) 3);
  230. cell.setCellValue("X33");
  231. cell.setCellStyle(style);
  232. style = workbook.createCellStyle();
  233. style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
  234. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  235. cell = row7.createCell((short) 4);
  236. cell.setCellValue("X34");
  237. cell.setCellStyle(style);
  238. style = workbook.createCellStyle();
  239. style.setFillForegroundColor(IndexedColors.ORCHID.getIndex());
  240. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  241. cell = row7.createCell((short) 5);
  242. cell.setCellValue("X35");
  243. cell.setCellStyle(style);
  244. // Create a row and put some cells in it.
  245. Row row8 = sheet.createRow((short) 8);
  246. style = workbook.createCellStyle();
  247. style.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
  248. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  249. cell = row8.createCell((short) 1);
  250. cell.setCellValue("X36");
  251. cell.setCellStyle(style);
  252. style = workbook.createCellStyle();
  253. style.setFillForegroundColor(IndexedColors.PINK.getIndex());
  254. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  255. cell = row8.createCell((short) 2);
  256. cell.setCellValue("X37");
  257. cell.setCellStyle(style);
  258. style = workbook.createCellStyle();
  259. style.setFillForegroundColor(IndexedColors.PLUM.getIndex());
  260. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  261. cell = row8.createCell((short) 3);
  262. cell.setCellValue("X38");
  263. cell.setCellStyle(style);
  264. style = workbook.createCellStyle();
  265. style.setFillForegroundColor(IndexedColors.RED.getIndex());
  266. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  267. cell = row8.createCell((short) 4);
  268. cell.setCellValue("X39");
  269. cell.setCellStyle(style);
  270. style = workbook.createCellStyle();
  271. style.setFillForegroundColor(IndexedColors.ROSE.getIndex());
  272. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  273. cell = row8.createCell((short) 5);
  274. cell.setCellValue("X40");
  275. cell.setCellStyle(style);
  276. // Create a row and put some cells in it.
  277. Row row9 = sheet.createRow((short) 9);
  278. style = workbook.createCellStyle();
  279. style.setFillForegroundColor(IndexedColors.ROYAL_BLUE.getIndex());
  280. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  281. cell = row9.createCell((short) 1);
  282. cell.setCellValue("X41");
  283. cell.setCellStyle(style);
  284. style = workbook.createCellStyle();
  285. style.setFillForegroundColor(IndexedColors.SEA_GREEN.getIndex());
  286. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  287. cell = row9.createCell((short) 2);
  288. cell.setCellValue("X42");
  289. cell.setCellStyle(style);
  290. style = workbook.createCellStyle();
  291. style.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
  292. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  293. cell = row9.createCell((short) 3);
  294. cell.setCellValue("X43");
  295. cell.setCellStyle(style);
  296. style = workbook.createCellStyle();
  297. style.setFillForegroundColor(IndexedColors.TAN.getIndex());
  298. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  299. cell = row9.createCell((short) 4);
  300. cell.setCellValue("X44");
  301. cell.setCellStyle(style);
  302. style = workbook.createCellStyle();
  303. style.setFillForegroundColor(IndexedColors.TEAL.getIndex());
  304. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  305. cell = row9.createCell((short) 5);
  306. cell.setCellValue("X45");
  307. cell.setCellStyle(style);
  308. // Create a row and put some cells in it.
  309. Row row10 = sheet.createRow((short) 10);
  310. style = workbook.createCellStyle();
  311. style.setFillForegroundColor(IndexedColors.TURQUOISE.getIndex());
  312. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  313. cell = row10.createCell((short) 1);
  314. cell.setCellValue("X46");
  315. cell.setCellStyle(style);
  316. style = workbook.createCellStyle();
  317. style.setFillForegroundColor(IndexedColors.VIOLET.getIndex());
  318. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  319. cell = row10.createCell((short) 2);
  320. cell.setCellValue("X47");
  321. cell.setCellStyle(style);
  322. style = workbook.createCellStyle();
  323. style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
  324. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  325. cell = row10.createCell((short) 3);
  326. cell.setCellValue("X48");
  327. cell.setCellStyle(style);
  328. style = workbook.createCellStyle();
  329. style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
  330. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  331. cell = row10.createCell((short) 3);
  332. cell.setCellValue("X49");
  333. cell.setCellStyle(style);
  334. // Write the output to a file
  335. FileOutputStream fileOut = new FileOutputStream(
  336. "POIFillAndColorExample.xlsx");
  337. workbook.write(fileOut);
  338. fileOut.close();
  339. }
  340. }

The generated excel files looks like below images.

JAVA poi设置单元格背景颜色第1张

免责声明:文章转载自《JAVA poi设置单元格背景颜色》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇IE中自定义标签使用自封闭格式引发错误!【解决】GridView设置了 android:listSelector,选中某项背景色也会被选中下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

android使用POI读写word doc文件

目录 1 读word doc文件 1.1 通过WordExtractor读文件 1.2 通过HWPFDocument读文件 2 写word doc文件 Apache poi的hwpf模块是专门用来对word doc文件进行读写操作的。在hwpf里面我们使用HWPFDocument来表示一个word doc文档。在HWPFDocument里面有这么几个概念:...

如何用Apache POI操作Excel文件-----如何对一个单元格加注解?

有的时候,我们需要通过操作Apache POI,在生成Cell数据的同时,能对其生成的Cell,加上注解(comments),类似于下面的。 那么对于这种情况,我们的代码应该如何写呢? 借花献佛,我就用Apache POI官方提供的例子,然后加上一些注解,给大家看一下。本例子的测试代码是基于POI-3.12的。 执行完后,将会生成上图所示的Excel工作...

POI转换word doc文件为(html,xml,txt)

在POI中还存在有针对于word doc文件进行格式转换的功能。我们可以将word的内容转换为对应的Html文件,也可以把它转换为底层用来描述doc文档的xml文件,还可以把它转换为底层用来描述doc文档的xml格式的text文件。这些格式转换都是通过AbstractWordConverter特定的子类来完成的。  1 转换为Html文件 将doc文档转换...

POI导出数据内存溢出问题

POI之前的版本不支持大数据量处理,如果数据过多则经常报OOM错误,有时候调整JVM大小效果也不是太好。3.8版本的POI新出来了SXSSFWorkbook,可以支持大数据量的操作,只是SXSSFWorkbook只支持.xlsx格式,不支持.xls格式。 3.8版本的POI对excel的导出操作,一般只使用HSSFWorkbook以及SXSSFWorkb...

CVE20211732 LPE漏洞分析

概述   CVE-2021-1732是一个发生在windows内核win32kfull模块的LPE漏洞,并且由于创建窗口时调用win32kfull!xxxCreateWindowEx过程中会进行用户模式回调(KeUserModeCallback),从而给了用户态进程利用的机会。   该漏洞由安恒信息在2020年12月在野外攻击样本中发现,在2021年2月份...

POI解析多excel多sheet文件(单文件百万级以下)生成指定文件入Hive

临下班前有个需求,有个同事有一份excel数据需要导入到hive中,到手后发现需要导入的excel文件有5个,且每个excel有60个sheet,每个sheet文件是顶行的,由于文件是xls格式的,单excel文件数据量大概在390万左右,且sheet表有的有标题,有的是空行,且有的sheet要解析有的不要。            直接用poi解析xls格...