

1 //创建工作薄(excel)
2 Workbook wb = new HSSFWorkbook();
3 //创建sheet
4 Sheet createSheet = wb.createSheet("sheet1");
5
6 //设置标题字体
7 Font fontTitle = wb.createFont();
8 fontTitle.setFontHeightInPoints((short) 18); //字体大小
9 fontTitle.setColor(HSSFColor.BLACK.index); //字体颜色
10 fontTitle.setFontName("宋体"); //字体
11 fontTitle.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗体显示
12 //font.setItalic(true); //是否使用斜体
13 //font.setStrikeout(true); //是否使用划线
14
15 //设置标题单元格类型
16 CellStyle cellStyleTitle = wb.createCellStyle();
17 cellStyleTitle.setFont(fontTitle);
18 cellStyleTitle.setFillForegroundColor(IndexedColors.LIME.getIndex());
19 cellStyleTitle.setFillPattern(CellStyle.SOLID_FOREGROUND);
20 cellStyleTitle.setAlignment(CellStyle.ALIGN_CENTER); //水平布局:居中
21 cellStyleTitle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
22 cellStyleTitle.setWrapText(true);//设置自动换行
23
24 cellStyleTitle.setBorderBottom(CellStyle.BORDER_THIN); //下边框
25 cellStyleTitle.setBorderLeft(CellStyle.BORDER_THIN);//左边框
26 cellStyleTitle.setBorderTop(CellStyle.BORDER_THIN);//上边框
27 cellStyleTitle.setBorderRight(CellStyle.BORDER_THIN);//右边框
28 cellStyleTitle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
29 cellStyleTitle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
30 cellStyleTitle.setTopBorderColor(IndexedColors.BLACK.getIndex());
31 cellStyleTitle.setRightBorderColor(IndexedColors.BLACK.getIndex());
32
33
34 //创建合并单元格 ---begin
35 CellRangeAddress region = new CellRangeAddress(0, 0, 1, 3);// 下标从0开始 起始行号,终止行号, 起始列号,终止列号
36 CellRangeAddress region2 = new CellRangeAddress(1, 2, 0, 0);// 起始行号,终止行号, 起始列号,终止列号
37 CellRangeAddress region3 = new CellRangeAddress(1, 1, 1, 3);// 起始行号,终止行号, 起始列号,终止列号
38 //在sheet里增加合并单元格
39 createSheet.addMergedRegion(region);
40 createSheet.addMergedRegion(region2);
41 createSheet.addMergedRegion(region3);
42
43 // -----------填充第一行数据-------------
44 Row rowTitle = createSheet.createRow(0);
45 Cell cellTitle = rowTitle.createCell(0);
46 cellTitle.setCellStyle(cellStyleTitle);
47 cellTitle.setCellValue("");// 设置标题内容
48 Cell cellTitle1_1 = rowTitle.createCell(1);
49 cellTitle1_1.setCellStyle(cellStyleTitle);
50 cellTitle1_1.setCellValue(dateStr);// 设置标题内容
51 // -----------填充第二行数据-------------
52 Row rowTitle1 = createSheet.createRow(1);
53 Cell cellTitle1 = rowTitle1.createCell(0);
54 cellTitle1.setCellStyle(cellStyleTitle);
55 cellTitle1.setCellValue("店名");// 设置内容
56 Cell cellTitle11 = rowTitle1.createCell(1);
57 cellTitle11.setCellStyle(cellStyleTitle);
58 cellTitle11.setCellValue("王总");//
59 Cell cellTitle111 = rowTitle1.createCell(2);
60 cellTitle111.setCellStyle(cellStyleTitle);
61 cellTitle111.setCellValue("");// 虽然这个单元格不可以不设置,但是要给它设置样式,所以也写了
62 Cell cellTitle1111 = rowTitle1.createCell(3);
63 cellTitle1111.setCellStyle(cellStyleTitle);
64 cellTitle1111.setCellValue("");// 虽然这个单元格不可以不设置,但是要给它设置样式,所以也写了
65 // -----------填充第三行数据-------------
66 Row rowTitle2 = createSheet.createRow(2);
67 Cell cellTitle2 = rowTitle2.createCell(1);
68 cellTitle2.setCellStyle(cellStyleTitle);
69 cellTitle2.setCellValue("装修费");// 设置内容
70 Cell cellTitle22 = rowTitle2.createCell(2);
71 cellTitle22.setCellStyle(cellStyleTitle);
72 cellTitle22.setCellValue("加盟费");// 设置内容
73 Cell cellTitle222 = rowTitle2.createCell(3);
74 cellTitle222.setCellStyle(cellStyleTitle);
75 cellTitle222.setCellValue("人员培训费");// 设置内容
76 // 合并单元格 ----end
77 // 第四行数据留着下面写
78
79 //设置表头字体
80 Font fontHead = wb.createFont();
81 fontHead.setFontHeightInPoints((short) 14); //字体大小
82 fontHead.setColor(Font.COLOR_NORMAL); //字体颜色
83 fontHead.setFontName("Microsoft Sans Serif"); //字体
84 fontHead.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗体显示
85 //font.setItalic(true); //是否使用斜体
86 //font.setStrikeout(true); //是否使用划线
87
88 //设置表头单元格类型
89 CellStyle cellStyleHead = wb.createCellStyle();
90 cellStyleHead.setFont(fontHead);
91 cellStyleHead.setAlignment(CellStyle.ALIGN_CENTER); //水平布局:居中
92 cellStyleHead.setFillForegroundColor(IndexedColors.LIME.getIndex());
93 cellStyleHead.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
94 cellStyleHead.setWrapText(true);//设置自动换行
95
96 cellStyleHead.setBorderBottom(CellStyle.BORDER_THIN); //下边框
97 cellStyleHead.setBorderLeft(CellStyle.BORDER_THIN);//左边框
98 cellStyleHead.setBorderTop(CellStyle.BORDER_THIN);//上边框
99 cellStyleHead.setBorderRight(CellStyle.BORDER_THIN);//右边框
100 cellStyleHead.setBottomBorderColor(IndexedColors.BLACK.getIndex());
101 cellStyleHead.setLeftBorderColor(IndexedColors.BLACK.getIndex());
102 cellStyleHead.setTopBorderColor(IndexedColors.BLACK.getIndex());
103 cellStyleHead.setRightBorderColor(IndexedColors.BLACK.getIndex());
104
105 //创建第一行,标题
106 Row row = createSheet.createRow(3);
107 String[] cellHead = {"", "","", ""};
108 // 设置列宽
109 double[] titleWidth = {10, 24, 24, 24};
110 for (int i = 0; i < cellHead.length; i++) {
111 Cell createCell = row.createCell(i);
112 createCell.setCellValue(cellHead[i]);
113 createCell.setCellStyle(cellStyleHead);
114 }
115
116
117 //设置内容字体
118 Font fontData = wb.createFont();
119 fontData.setFontHeightInPoints((short) 14); //字体大小
120 fontData.setColor(Font.COLOR_NORMAL); //字体颜色
121 fontData.setFontName("Microsoft Sans Serif"); //字体
122 //font.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗体显示
123 //font.setItalic(true); //是否使用斜体
124 //font.setStrikeout(true); //是否使用划线
125
126 //设置内容单元格类型
127 CellStyle cellStyleDataOdd = wb.createCellStyle();
128 cellStyleDataOdd.setFont(fontData);
129 cellStyleDataOdd.setFillPattern(CellStyle.SOLID_FOREGROUND);
130 cellStyleDataOdd.setAlignment(CellStyle.ALIGN_CENTER); //水平布局:居中
131 cellStyleDataOdd.setWrapText(true);
132
133 cellStyleDataOdd.setBorderBottom(CellStyle.BORDER_THIN); //下边框
134 cellStyleDataOdd.setBorderLeft(CellStyle.BORDER_THIN);//左边框
135 cellStyleDataOdd.setBorderTop(CellStyle.BORDER_THIN);//上边框
136 cellStyleDataOdd.setBorderRight(CellStyle.BORDER_THIN);//右边框
137 cellStyleDataOdd.setBottomBorderColor(IndexedColors.BLACK.getIndex());
138 cellStyleDataOdd.setLeftBorderColor(IndexedColors.BLACK.getIndex());
139 cellStyleDataOdd.setTopBorderColor(IndexedColors.BLACK.getIndex());
140 cellStyleDataOdd.setRightBorderColor(IndexedColors.BLACK.getIndex());
141
142 try {
143 int no = 1;
144 for (int i = 0; i < datas.length(); i++) {
145 JSONObject dayData = datas.getJSONObject(i);
146 String date = dayData.optString("date");
147 JSONArray detail = dayData.getJSONArray("detail");
148
149 for (int k = 0; k < detail.length(); k++) {
150 JSONObject ss = detail.getJSONObject(k);
151
152 row = createSheet.createRow(k + 4);
153 int j = 0;
154 // 店名
155 Cell cell1 = row.createCell(j++);
156 cell1.setCellValue(ss.optString("xxxxx"));
157 cell1.setCellStyle(cellStyleDataOdd);
158 // 装修费
159 Cell cell2 = row.createCell(j++);
160 cell2.setCellValue(ss.optString("xxxxx"));
161 cell2.setCellStyle(cellStyleDataOdd);
162 // 加盟费
163 Cell cell7 = row.createCell(j++);
164 cell7.setCellValue(ss.optString("xxxxxx"));
165 cell7.setCellStyle(cellStyleDataOdd);
166 // 人员培训费
167 Cell cellH = row.createCell(j++);
168 cellH.setCellValue(ss.optString("xxxxxx"));
169 cellH.setCellStyle(cellStyleDataOdd);
170 }
171 }
172
173 } catch (JSONException e) {
174 e.printStackTrace();
175 }
176
177 // 设置列宽
178 for (int i = 0; i < titleWidth.length; i++) {
179 createSheet.setColumnWidth((short) i, (short) titleWidth[i] * 256);
180 }
181
182 // 设置上面四行冻结
183 createSheet.createFreezePane( 0, 4, 1, 4); // 前两个参数是你要用来拆分的列数和行数。后两个参数是下面窗口的可见象限,其中第三个参数是右边区域可见的左边列数,第四个参数是下面区域可见的首行
184