1 | : |
import java.io.FileOutputStream;
|
2 | : |
import java.util.Date;
|
3 | : |
import org.apache.poi.hssf.usermodel.*;
|
4 | : |
import org.apache.poi.hssf.usermodel.contrib.HSSFCellUtil;
|
5 | : |
import org.apache.poi.hssf.usermodel.contrib.HSSFRegionUtil;
|
6 | : |
import org.apache.poi.hssf.util.HSSFColor;
|
7 | : |
import org.apache.poi.hssf.util.Region;
|
8 | : |
|
9 | : |
public class HSSFWriteSample {
|
10 | : |
|
11 | : |
public static void main(String[] args) throws Exception {
|
12 | : |
|
13 | : |
// ワークブックとシートの作成
|
14 | : |
HSSFWorkbook book = new HSSFWorkbook(); // ← (1)
|
15 | : |
HSSFSheet sheet = book.createSheet("sheet1"); // ← (2)
|
16 | : |
|
17 | : |
// セルへの値の設定
|
18 | : |
HSSFRow row = sheet.createRow((short)1); // ← (3)
|
19 | : |
row.createCell((short)0).setCellValue(12345.0); // ← (5)
|
20 | : |
row.createCell((short)1).setCellValue("Hello!"); // ← (5)
|
21 | : |
row.createCell((short)2).setCellValue(true); // ← (5)
|
22 | : |
|
23 | : |
// 文字列が日本語の場合
|
24 | : |
HSSFCell cell = row.createCell((short)3); // ← (4)
|
25 | : |
cell.setEncoding(HSSFCell.ENCODING_UTF_16); // ← (6)
|
26 | : |
cell.setCellValue("日本語");
|
27 | : |
|
28 | : |
sheet.setColumnWidth((short)4, (short)3840); // 列の幅設定
|
29 | : |
|
30 | : |
// 日時を設定
|
31 | : |
HSSFCellStyle style = book.createCellStyle(); ← (9)
|
32 | : |
style.setDataFormat( // ← (7)
|
33 | : |
HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm")); // ← (7)
|
34 | : |
cell = row.createCell((short)4); // ← (7)
|
35 | : |
cell.setCellValue(new Date()); // ← (7)
|
36 | : |
cell.setCellStyle(style); // ← (10)
|
37 | : |
|
38 | : |
// 式
|
39 | : |
row = sheet.createRow((short)2);
|
40 | : |
row.createCell((short)0).setCellValue(2);
|
41 | : |
row.createCell((short)1).setCellValue(4);
|
42 | : |
row.createCell((short)2).setCellValue(6);
|
43 | : |
cell = row.createCell((short)3);
|
44 | : |
cell.setCellFormula("SUM(A3:C3)"); // ← (8)
|
45 | : |
|
46 | : |
// 罫線
|
47 | : |
row = sheet.createRow((short)3);
|
48 | : |
short[] border_type = {
|
49 | : |
HSSFCellStyle.BORDER_THIN, HSSFCellStyle.BORDER_MEDIUM,
|
50 | : |
HSSFCellStyle.BORDER_DASHED, HSSFCellStyle.BORDER_DOUBLE };
|
51 | : |
for (int i=0; i<border_type.length; i++) {
|
52 | : |
style = book.createCellStyle();
|
53 | : |
style.setBorderBottom(border_type[i]); // ← (11)
|
54 | : |
HSSFCellUtil.createCell(row, i, "BORDER", style); // ← (15)
|
55 | : |
}
|
56 | : |
|
57 | : |
// 色
|
58 | : |
row = sheet.createRow((short)6); // ← (12)
|
59 | : |
style = book.createCellStyle(); // ← (12)
|
60 | : |
style.setFillForegroundColor(HSSFColor.RED.index); // ← (12)
|
61 | : |
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // ← (12)
|
62 | : |
HSSFCellUtil.createCell(row, 2, "Color", style); // ← (12)
|
63 | : |
|
64 | : |
// フォント
|
65 | : |
row = sheet.createRow((short)7); // ← (13)
|
66 | : |
row.setHeightInPoints((short)30); // 列の高さ設定 // ← (13)
|
67 | : |
style = book.createCellStyle(); // ← (13)
|
68 | : |
HSSFFont font = book.createFont(); // ← (13)
|
69 | : |
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // ← (13)
|
70 | : |
font.setFontHeightInPoints((short)24); // ← (13)
|
71 | : |
font.setColor(HSSFFont.COLOR_RED); // ← (13)
|
72 | : |
style.setFont(font); // ← (13)
|
73 | : |
HSSFCellUtil.createCell(row, 4, "FONT", style); // ← (13)
|
74 | : |
|
75 | : |
// 罫線
|
76 | : |
Region region = new Region(6, (short)2, 7, (short)4);
|
77 | : |
short border = HSSFCellStyle.BORDER_DASHED;
|
78 | : |
HSSFRegionUtil.setBorderTop(border, region, sheet, book); // ← (16)
|
79 | : |
HSSFRegionUtil.setBorderLeft(border, region, sheet, book); // ← (16)
|
80 | : |
HSSFRegionUtil.setBorderRight(border, region, sheet, book); // ← (16)
|
81 | : |
HSSFRegionUtil.setBorderBottom(border, region, sheet, book); // ← (16)
|
82 | : |
|
83 | : |
// 出力
|
84 | : |
FileOutputStream out = new FileOutputStream("Sample.xls");
|
85 | : |
book.write(out); // ← (14)
|
86 | : |
out.close();
|
87 | : |
}
|
88 | : |
}
|