Apache POI generate Excel Report Example

The beauty of Excel is that not only you can present data in Tabular form but actually format it along with Header and Footer sections to be printed as a report. I am not very good at using Excel but I have seen people use Excel for a lot of things such as Order forms, no wonder its that popular. In this example we are going to learn the following useful features to generate a Report using the Apache POI classes ...

  • Set Column widths
  • Set Page margins - Left, Right, Top and Bottom
  • Set Header and Footer margins
  • Set Header Left, Center and Right Sections
  • Set Footer with Page numbers
  • Set Cell Borders using custom CellStyle
  • Create Multiple Pages with Heading and Details

Apache POI generate Excel Report Apache POI generate Excel Report

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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package com.as400samplecode;
 
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import org.apache.poi.hssf.usermodel.HSSFHeader;
import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import org.apache.poi.hssf.usermodel.HeaderFooter;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Footer;
import org.apache.poi.ss.usermodel.Header;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
public class ExcelGenerateReport {
 
 private CellStyle cs = null;
 private CellStyle csBold = null;
 private CellStyle csTop = null;
 private CellStyle csRight = null;
 private CellStyle csBottom = null;
 private CellStyle csLeft = null;
 private CellStyle csTopLeft = null;
 private CellStyle csTopRight = null;
 private CellStyle csBottomLeft = null;
 private CellStyle csBottomRight = null;
 
 public static void main(String[] args) {
 
  ExcelGenerateReport myReport = new ExcelGenerateReport();
  myReport.createExcel();
 
 }
 
 public void createExcel() {
 
  try{
 
   Workbook wb = new XSSFWorkbook();
   Sheet sheet = wb.createSheet("My Excel Report");
 
   //Setup some styles that we need for the Cells
   setCellStyles(wb);
 
   //Get current Date and Time
   Date date = new Date(System.currentTimeMillis());
   DateFormat df = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
 
   //Set Column Widths
   sheet.setColumnWidth(0, 2500);
   sheet.setColumnWidth(1, 2500);
   sheet.setColumnWidth(2, 6000);
   sheet.setColumnWidth(3, 10000);
   sheet.setColumnWidth(4, 3000);
 
   //Setup the Page margins - Left, Right, Top and Bottom
   sheet.setMargin(Sheet.LeftMargin, 0.25);
   sheet.setMargin(Sheet.RightMargin, 0.25);
   sheet.setMargin(Sheet.TopMargin, 0.75);
   sheet.setMargin(Sheet.BottomMargin, 0.75);
 
   //Setup the Header and Footer Margins
   sheet.setMargin(Sheet.HeaderMargin, 0.25);
   sheet.setMargin(Sheet.FooterMargin, 0.25);
    
   //If you are using HSSFWorkbook() instead of XSSFWorkbook()
   //HSSFPrintSetup ps = (HSSFPrintSetup) sheet.getPrintSetup();
   //ps.setHeaderMargin((double) .25);
   //ps.setFooterMargin((double) .25);
 
   //Set Header Information
   Header header = sheet.getHeader();
   header.setLeft("*** ORIGINAL COPY ***");
   header.setCenter(HSSFHeader.font("Arial", "Bold") +
     HSSFHeader.fontSize((short) 14) + "SAMPLE ORDER");
   header.setRight(df.format(date));
 
   //Set Footer Information with Page Numbers
   Footer footer = sheet.getFooter();
   footer.setRight( "Page " + HeaderFooter.page() + " of " +
     HeaderFooter.numPages() );
 
 
   int rowIndex = 0;
   rowIndex = insertHeaderInfo(sheet, rowIndex);
   rowIndex = insertDetailInfo(sheet, rowIndex);
 
   rowIndex = 47 * 1;
   rowIndex = insertHeaderInfo(sheet, rowIndex);
   rowIndex = insertDetailInfo(sheet, rowIndex);
 
   rowIndex = 47 * 2;
   rowIndex = insertHeaderInfo(sheet, rowIndex);
   rowIndex = insertDetailInfo(sheet, rowIndex);
 
   //Write the Excel file
   FileOutputStream fileOut = null;
   fileOut = new FileOutputStream("data/myReport.xlsx");
   wb.write(fileOut);
   fileOut.close();
 
  }
  catch (Exception e) {
   System.out.println(e);
  }
 
 }
 
 private void setCellStyles(Workbook wb) {
 
  //font size 10
  Font f = wb.createFont();
  f.setFontHeightInPoints((short) 10);
 
  //Simple style
  cs = wb.createCellStyle();
  cs.setFont(f);
 
  //Bold Fond
  Font bold = wb.createFont();
  bold.setBoldweight(Font.BOLDWEIGHT_BOLD);
  bold.setFontHeightInPoints((short) 10);
 
  //Bold style
  csBold = wb.createCellStyle();
  csBold.setBorderBottom(CellStyle.BORDER_THIN);
  csBold.setBottomBorderColor(IndexedColors.BLACK.getIndex());
  csBold.setFont(bold);
 
  //Setup style for Top Border Line
  csTop = wb.createCellStyle();
  csTop.setBorderTop(CellStyle.BORDER_THIN);
  csTop.setTopBorderColor(IndexedColors.BLACK.getIndex());
  csTop.setFont(f);
 
  //Setup style for Right Border Line
  csRight = wb.createCellStyle();
  csRight.setBorderRight(CellStyle.BORDER_THIN);
  csRight.setRightBorderColor(IndexedColors.BLACK.getIndex());
  csRight.setFont(f);
 
  //Setup style for Bottom Border Line
  csBottom = wb.createCellStyle();
  csBottom.setBorderBottom(CellStyle.BORDER_THIN);
  csBottom.setBottomBorderColor(IndexedColors.BLACK.getIndex());
  csBottom.setFont(f);
 
  //Setup style for Left Border Line
  csLeft = wb.createCellStyle();
  csLeft.setBorderLeft(CellStyle.BORDER_THIN);
  csLeft.setLeftBorderColor(IndexedColors.BLACK.getIndex());
  csLeft.setFont(f);
 
  //Setup style for Top/Left corner cell Border Lines
  csTopLeft = wb.createCellStyle();
  csTopLeft.setBorderTop(CellStyle.BORDER_THIN);
  csTopLeft.setTopBorderColor(IndexedColors.BLACK.getIndex());
  csTopLeft.setBorderLeft(CellStyle.BORDER_THIN);
  csTopLeft.setLeftBorderColor(IndexedColors.BLACK.getIndex());
  csTopLeft.setFont(f);
 
  //Setup style for Top/Right corner cell Border Lines
  csTopRight = wb.createCellStyle();
  csTopRight.setBorderTop(CellStyle.BORDER_THIN);
  csTopRight.setTopBorderColor(IndexedColors.BLACK.getIndex());
  csTopRight.setBorderRight(CellStyle.BORDER_THIN);
  csTopRight.setRightBorderColor(IndexedColors.BLACK.getIndex());
  csTopRight.setFont(f);
 
  //Setup style for Bottom/Left corner cell Border Lines
  csBottomLeft = wb.createCellStyle();
  csBottomLeft.setBorderBottom(CellStyle.BORDER_THIN);
  csBottomLeft.setBottomBorderColor(IndexedColors.BLACK.getIndex());
  csBottomLeft.setBorderLeft(CellStyle.BORDER_THIN);
  csBottomLeft.setLeftBorderColor(IndexedColors.BLACK.getIndex());
  csBottomLeft.setFont(f);
 
  //Setup style for Bottom/Right corner cell Border Lines
  csBottomRight = wb.createCellStyle();
  csBottomRight.setBorderBottom(CellStyle.BORDER_THIN);
  csBottomRight.setBottomBorderColor(IndexedColors.BLACK.getIndex());
  csBottomRight.setBorderRight(CellStyle.BORDER_THIN);
  csBottomRight.setRightBorderColor(IndexedColors.BLACK.getIndex());
  csBottomRight.setFont(f);
 
 }
 
 private int insertHeaderInfo(Sheet sheet, int index){
 
  int rowIndex = index;
  Row row = null;
  Cell c = null;
 
  rowIndex++;
  row = sheet.createRow(rowIndex);
  c = row.createCell(0);
  c.setCellValue("Customer No:");
  c.setCellStyle(csTopLeft);
  c = row.createCell(1);
  c.setCellStyle(csTop);
  c = row.createCell(2);
  c.setCellValue("ABC");
  c.setCellStyle(csTopRight);
 
  rowIndex++;
  row = sheet.createRow(rowIndex);
  c = row.createCell(0);
  c.setCellValue("Order No:");
  c.setCellStyle(csLeft);
  c = row.createCell(2);
  c.setCellValue("123456");
  c.setCellStyle(csRight);
 
  rowIndex++;
  row = sheet.createRow(rowIndex);
  c = row.createCell(0);
  c.setCellStyle(csLeft);
  c = row.createCell(2);
  c.setCellStyle(csRight);
 
  rowIndex++;
  row = sheet.createRow(rowIndex);
  c = row.createCell(0);
  c.setCellValue("Name:");
  c.setCellStyle(csLeft);
  c = row.createCell(2);
  c.setCellValue("ABC Customer");
  c.setCellStyle(csRight);
 
  rowIndex++;
  row = sheet.createRow(rowIndex);
  c = row.createCell(0);
  c.setCellValue("Address:");
  c.setCellStyle(csLeft);
  c = row.createCell(2);
  c.setCellValue("123 Street No.");
  c.setCellStyle(csRight);
 
  rowIndex++;
  row = sheet.createRow(rowIndex);
  c = row.createCell(0);
  c.setCellStyle(csLeft);
  c = row.createCell(2);
  c.setCellValue("Unknown City, State ZIPCODE");
  c.setCellStyle(csRight);
 
  rowIndex++;
  row = sheet.createRow(rowIndex);
  c = row.createCell(0);
  c.setCellStyle(csBottomLeft);
  c = row.createCell(1);
  c.setCellStyle(csBottom);
  c = row.createCell(2);
  c.setCellValue("U.S.A.");
  c.setCellStyle(csBottomRight);
 
  rowIndex = rowIndex + 3;
  row = sheet.createRow(rowIndex);
  c = row.createCell(0);
  c.setCellValue("Line No");
  c.setCellStyle(csBold);
  c = row.createCell(1);
  c.setCellValue("Quantity");
  c.setCellStyle(csBold);
  c = row.createCell(2);
  c.setCellValue("Item No");
  c.setCellStyle(csBold);
  c = row.createCell(3);
  c.setCellValue("Description");
  c.setCellStyle(csBold);
  c = row.createCell(4);
  c.setCellValue("Price");
  c.setCellStyle(csBold);
 
  return rowIndex;
 
 }
 
 private int insertDetailInfo(Sheet sheet, int index){
 
  int rowIndex = 0;
  Row row = null;
  Cell c = null;
 
  for(int i = 1; i < 35; i++){
    
   rowIndex = index + i;
   row = sheet.createRow(rowIndex);
   c = row.createCell(0);
   c.setCellValue(i);
   c.setCellStyle(cs);
   c = row.createCell(1);
   c.setCellValue(10 + i);
   c.setCellStyle(cs);
   c = row.createCell(2);
   c.setCellValue("ITEM" + i);
   c.setCellStyle(cs);
   c = row.createCell(3);
   c.setCellValue("My ITEM" + i + " Decscription");
   c.setCellStyle(cs);
   c = row.createCell(4);
   c.setCellValue(1.11 * i);
   c.setCellStyle(cs);
 
  }
 
  return rowIndex;
 
 }
}

References

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.