213. // Style the cell with borders all around. 214.
215. HSSFCellStyle style = wb.createCellStyle(); 216.
217. style.setBorderBottom(HSSFCellStyle.BORDER_THIN); 218.
219. style.setBottomBorderColor(HSSFColor.BLACK.index); 220.
221. style.setBorderLeft(HSSFCellStyle.BORDER_THIN); 222.
223. style.setLeftBorderColor(HSSFColor.GREEN.index); 224.
225. style.setBorderRight(HSSFCellStyle.BORDER_THIN); 226.
227. style.setRightBorderColor(HSSFColor.BLUE.index); 228.
229. style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED); 230.
231. style.setTopBorderColor(HSSFColor.BLACK.index); 232.
233. cell.setCellStyle(style); 234.
235. // Write the output to a file 236.
237. FileOutputStream fileOut = new FileOutputStream(\ 238.
239. wb.write(fileOut); 240.
241. fileOut.close(); 242.
243. 8.填充和颜色设置 244.
245. HSSFWorkbook wb = new HSSFWorkbook(); 246.
247. HSSFSheet sheet = wb.createSheet(\ 248.
249. // Create a row and put some cells in it. Rows are 0 based. 250.
251. HSSFRow row = sheet.createRow((short) 1); 252.
253. // Aqua background 254.
255. HSSFCellStyle style = wb.createCellStyle(); 256.
257. style.setFillBackgroundColor(HSSFColor.AQUA.index); 258.
259. style.setFillPattern(HSSFCellStyle.BIG_SPOTS); 260.
261. HSSFCell cell = row.createCell((short) 1); 262.
263. cell.setCellValue(\ 264.
265. cell.setCellStyle(style); 266.
267. // Orange \ 268.
269. style = wb.createCellStyle(); 270.
271. style.setFillForegroundColor(HSSFColor.ORANGE.index); 272.
273. style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 274.
275. cell = row.createCell((short) 2); 276.
277. cell.setCellValue(\ 278.
279. cell.setCellStyle(style); 280.
281. // Write the output to a file 282.
283. FileOutputStream fileOut = new FileOutputStream(\ 284.
285. wb.write(fileOut); 286.
287. fileOut.close(); 288.
289. 9.合并单元格操作 290.
291. HSSFWorkbook wb = new HSSFWorkbook(); 292.
293. HSSFSheet sheet = wb.createSheet(\ 294.
295. HSSFRow row = sheet.createRow((short) 1); 296.
297. HSSFCell cell = row.createCell((short) 1); 298.
299. cell.setCellValue(\ 300.
301. sheet.addMergedRegion(new Region(1,(short)1,1,(short)2)); 302.
303. // Write the output to a file 304.
305. FileOutputStream fileOut = new FileOutputStream(\ 306.
307. wb.write(fileOut); 308.
309. fileOut.close(); 310.
311. 10.字体设置 312.
313. HSSFWorkbook wb = new HSSFWorkbook(); 314.
315. HSSFSheet sheet = wb.createSheet(\ 316.
317. // Create a row and put some cells in it. Rows are 0 based. 318.
319. HSSFRow row = sheet.createRow((short) 1); 320.
321. // Create a new font and alter it. 322.
323. HSSFFont font = wb.createFont(); 324.
325. font.setFontHeightInPoints((short)24); 326.
327. font.setFontName(\ 328.
329. font.setItalic(true); 330.
331. font.setStrikeout(true); 332.
333. // Fonts are set into a style so create a new one to use. 334.
335. HSSFCellStyle style = wb.createCellStyle(); 336.
337. style.setFont(font); 338.
339. // Create a cell and put a value in it. 340.
341. HSSFCell cell = row.createCell((short) 1); 342.
343. cell.setCellValue(\ 344.
345. cell.setCellStyle(style); 346.
347. // Write the output to a file 348.
349. FileOutputStream fileOut = new FileOutputStream(\ 350.
351. wb.write(fileOut); 352.
353. fileOut.close(); 354.
355. 11.自定义颜色 356.
357. HSSFWorkbook wb = new HSSFWorkbook(); 358.
359. HSSFSheet sheet = wb.createSheet(); 360.
361. HSSFRow row = sheet.createRow((short) 0); 362.
363. HSSFCell cell = row.createCell((short) 0); 364.
365. cell.setCellValue(\ 366.
367. //apply some colors from the standard palette, 368.
369. // as in the previous examples. 370.
371. //we'll use red text on a lime background 372.
373. HSSFCellStyle style = wb.createCellStyle(); 374.
375. style.setFillForegroundColor(HSSFColor.LIME.index); 376.
377. style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 378.
379. HSSFFont font = wb.createFont(); 380.
381. font.setColor(HSSFColor.RED.index); 382.
383. style.setFont(font); 384.
385. cell.setCellStyle(style); 386.
387. //save with the default palette 388.
389. FileOutputStream out = new FileOutputStream(\ 390.
391. wb.write(out); 392.
393. out.close(); 394.
395. //now, let's replace RED and LIME in the palette 396.
397. // with a more attractive combination 398.
399. // (lovingly borrowed from freebsd.org) 400.
401. cell.setCellValue(\ 402.
403. //creating a custom palette for the workbook 404.
405. HSSFPalette palette = wb.getCustomPalette(); 406.
407. //replacing the standard red with freebsd.org red 408.
409. palette.setColorAtIndex(HSSFColor.RED.index, 410.
411. (byte) 153, //RGB red (0-255) 412.
413. (byte) 0, //RGB green 414.
415. (byte) 0 //RGB blue 416. 417. ); 418.
419. //replacing lime with freebsd.org gold 420.
421. palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);
422.
423. //save with the modified palette 424.
425. // note that wherever we have previously used RED or LIME, the 426.
427. // new colors magically appear 428.
429. out = new FileOutputStream(\ 430.
431. wb.write(out);