|
1 package com.bie;
2
3 import java.io.FileOutputStream;
4 import java.io.IOException;
5
6 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
7 import org.apache.poi.hssf.usermodel.HSSFRichTextString;
8 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
9 import org.apache.poi.ss.usermodel.Cell;
10 import org.apache.poi.ss.usermodel.CellStyle;
11 import org.apache.poi.ss.usermodel.IndexedColors;
12 import org.apache.poi.ss.usermodel.Row;
13 import org.apache.poi.ss.usermodel.Sheet;
14 import org.apache.poi.ss.usermodel.Workbook;
15 import org.apache.poi.ss.util.CellRangeAddress;
16
17 /**
18 * @author Author:别先生
19 * @date Date:2017年9月9日 上午10:14:31
20 *
21 *
22 */
23 public>
24
25 /***
26 * 创建一个单元格并为其设定指定的对齐方式
27 * @param wb 工作簿
28 * @param row 行
29 * @param column 列
30 * @param halign 水平对齐的方式
31 * @param valign 垂直对齐的方式
32 */
33 private static void createCell(Workbook wb,Row row,short column,short halign,short valign){
34 //创建单元格
35 Cell cell = row.createCell(column);
36 //设置单元格的值
37 cell.setCellValue(new HSSFRichTextString("测试内容"));
38 //创建单元格的样式
39 CellStyle cellStyle = wb.createCellStyle();
40 //设置单元格水平对齐方式
41 cellStyle.setAlignment(halign);
42 //设置单元格垂直对齐方式
43 cellStyle.setVerticalAlignment(valign);
44
45 //设置边框线和颜色
46 //底部边框
47 cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
48 //底部颜色
49 cellStyle.setBottomBorderColor(IndexedColors.BLUE.getIndex());
50
51 //左边边框
52 cellStyle.setBorderLeft(CellStyle.BORDER_DASH_DOT);
53 //左边颜色
54 cellStyle.setBottomBorderColor(IndexedColors.RED.getIndex());
55
56 //右边边框
57 cellStyle.setRightBorderColor(CellStyle.ALIGN_FILL);
58 //右边边框颜色
59 cellStyle.setBottomBorderColor(IndexedColors.DARK_YELLOW.getIndex());
60
61 //顶部边框
62 cellStyle.setBorderTop(CellStyle.BORDER_DOTTED);
63 //顶部颜色
64 cellStyle.setTopBorderColor(IndexedColors.AUTOMATIC.getIndex());
65
66 //设置单元格填充色和颜色操作
67 //设置背景颜色
68 cellStyle.setFillBackgroundColor(IndexedColors.GREEN.getIndex());
69 cellStyle.setFillPattern(CellStyle.BIG_SPOTS);
70 //设置前景颜色
71 cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
72 cellStyle.setFillPattern(CellStyle.BIG_SPOTS);
73
74 //设置单元格的样式
75 cell.setCellStyle(cellStyle);
76 }
77
78 public static void main(String[] args) throws IOException {
79 //定义一个工作簿
80 Workbook wb =new HSSFWorkbook();
81 //创建第一个sheet页
82 Sheet createSheet = wb.createSheet("第一个sheet");
83 //创建第一行
84 Row createRow = createSheet.createRow(0);
85 createRow.setHeightInPoints(30);
86
87 //调用工具方法,创建单元格
88 //单元格的对齐方式的调用和使用
89 createCell(wb, createRow, (short)0, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_BOTTOM);;
90 createCell(wb, createRow, (short)1, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_CENTER);;
91 createCell(wb, createRow, (short)2, HSSFCellStyle.ALIGN_RIGHT, HSSFCellStyle.VERTICAL_TOP);;
92 createCell(wb, createRow, (short)3, HSSFCellStyle.ALIGN_JUSTIFY, HSSFCellStyle.VERTICAL_JUSTIFY);;
93
94
95 //创建单元格
96 //创建第3行
97 Row createRow2 = createSheet.createRow(2);
98 createRow.setHeightInPoints(30);
99 //创建第一列和第二列
100 Cell createCell = createRow2.createCell(0);
101 createCell.setCellValue("单元格合并1");
102
103 Cell createCell2 = createRow2.createCell(1);
104 createCell2.setCellValue("单元格合并2");
105
106 //单元格合并,起始行,结束行,起始列,结束列
107 createSheet.addMergedRegion(new CellRangeAddress(2, 3, 0, 3));
108
109
110 //创建输出流
111 FileOutputStream fos = new FileOutputStream("d:\\poi.xlsx");
112 //将内容写到excel文件中
113 wb.write(fos);
114 //关闭流
115 fos.close();
116 }
117
118 } |
|