POI

==作者:YB-Chi==

[toc]

用户API (HSSF and XSSF)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// 创建一个新文件	
FileOutputStream out = new FileOutputStream("workbook.xls");
// 创建一个新的工作簿
Workbook wb = new HSSFWorkbook();
// 创建一个新工作表
Sheet s = wb.createSheet();
// 声明一个行对象引用
Row r = null;
// 声明一个单元格对象引用
Cell c = null;
// 创建3个单元格样式
CellStyle cs = wb.createCellStyle();
CellStyle cs2 = wb.createCellStyle();
CellStyle cs3 = wb.createCellStyle();
DataFormat df = wb.createDataFormat();
// 创建2个字体对象
Font f = wb.createFont();
Font f2 = wb.createFont();

//设置字体1到12点类型
f.setFontHeightInPoints((short) 12);
//使它成为蓝色
f.setColor( (short)0xc );
// 使其加粗
//arial是默认字体
f.setBoldweight(Font.BOLDWEIGHT_BOLD);

//设置字体2到10点类型
f2.setFontHeightInPoints((short) 10);
//使它成为红色
f2.setColor( (short)Font.COLOR_RED );
//使其粗体
f2.setBoldweight(Font.BOLDWEIGHT_BOLD);

f2.setStrikeout( true );

//设置单元格风格
cs.setFont(f);
//设置单元格格式
cs.setDataFormat(df.getFormat("#,##0.0"));

//设置一个细边框
cs2.setBorderBottom(cs2.BORDER_THIN);
//填充w fg填充颜色
cs2.setFillPattern((short) CellStyle.SOLID_FOREGROUND);
//将单元格格式设置为文本,请参阅DataFormat以获取完整列表
cs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));

// 设置字体
cs2.setFont(f2);

// 在Unicode中设置工作表名称
wb.setSheetName(0, "\u0422\u0435\u0441\u0442\u043E\u0432\u0430\u044F " +
"\u0421\u0442\u0440\u0430\u043D\u0438\u0447\u043A\u0430" );
// in case of plain ascii
// wb.setSheetName(0, "HSSF Test");
// create a sheet with 30 rows (0-29)
int rownum;
for (rownum = (short) 0; rownum < 30; rownum++)
{
// create a row
r = s.createRow(rownum);
// on every other row
if ((rownum % 2) == 0)
{
// make the row height bigger (in twips - 1/20 of a point)
r.setHeight((short) 0x249);
}

//r.setRowNum(( short ) rownum);
// create 10 cells (0-9) (the += 2 becomes apparent later
for (short cellnum = (short) 0; cellnum < 10; cellnum += 2)
{
// create a numeric cell
c = r.createCell(cellnum);
// do some goofy math to demonstrate decimals
c.setCellValue(rownum * 10000 + cellnum
+ (((double) rownum / 1000)
+ ((double) cellnum / 10000)));

String cellValue;

// create a string cell (see why += 2 in the
c = r.createCell((short) (cellnum + 1));

// on every other row
if ((rownum % 2) == 0)
{
// set this cell to the first cell style we defined
c.setCellStyle(cs);
// set the cell's string value to "Test"
c.setCellValue( "Test" );
}
else
{
c.setCellStyle(cs2);
// set the cell's string value to "\u0422\u0435\u0441\u0442"
c.setCellValue( "\u0422\u0435\u0441\u0442" );
}


// make this column a bit wider
s.setColumnWidth((short) (cellnum + 1), (short) ((50 * 8) / ((double) 1 / 20)));
}
}

//draw a thick black border on the row at the bottom using BLANKS
// advance 2 rows
rownum++;
rownum++;

r = s.createRow(rownum);

// define the third style to be the default
// except with a thick black border at the bottom
cs3.setBorderBottom(cs3.BORDER_THICK);

//create 50 cells
for (short cellnum = (short) 0; cellnum < 50; cellnum++)
{
//create a blank type cell (no value)
c = r.createCell(cellnum);
// set it to the thick black border style
c.setCellStyle(cs3);
}

//end draw thick black border


// demonstrate adding/naming and deleting a sheet
// create a sheet, set its title then delete it
s = wb.createSheet();
wb.setSheetName(1, "DeletedSheet");
wb.removeSheetAt(1);
//end deleted sheet

// write the workbook to the output stream
// close our file (don't blow out our file handles
wb.write(out);
out.close();
文章作者: CYBSKY
文章链接: https://cybsky.top/2022/09/07/cyb-mds/java/POI/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 CYBSKY