-
Notifications
You must be signed in to change notification settings - Fork 0
/
Part1.java
87 lines (68 loc) · 2.71 KB
/
Part1.java
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
package forBlog;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FillPatternType;
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;
import com.fasterxml.jackson.databind.ObjectMapper;
import generic.GeneralCode;
public class Part1 {
public static void main(String args[]) throws IOException {
byte[] json = Files.readAllBytes(Paths.get("testPart1.json"));
ObjectMapper mapper = new ObjectMapper();
List<TestJson> myObjects = Arrays.asList(mapper.readValue(json, TestJson[].class));
String[] myHeaders = {"First Name","Last Name","Occupation","City"};
int sheetRowCount = 0;
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("FirstSheet");
Row row = sheet.createRow(sheetRowCount++);
Cell cell= null;
int cellCounter = 0;
for(String key: myHeaders) {
cell = row.createCell(cellCounter);
cell.setCellValue(myHeaders[cellCounter++]);
cell.setCellStyle(GeneralCode.getBorderedCellStyle(wb, BorderStyle.THICK));
}
cellCounter = 0;
int numRows = myObjects.size();
for(int i=0; i<numRows;i++) {
row = sheet.createRow(sheetRowCount++);
TestJson currentObject = myObjects.get(i);
cellCounter = 0;
for(String key: myHeaders) {
cell = row.createCell(cellCounter);
cell.setCellValue(currentObject.getData(myHeaders[cellCounter++]));
cell.setCellStyle(GeneralCode.getBorderedCellStyleWithBackground(wb, BorderStyle.HAIR, IndexedColors.AQUA.getIndex(), FillPatternType.SOLID_FOREGROUND, IndexedColors.BLACK.getIndex()));
}
}
sheet.addMergedRegion(new CellRangeAddress(5,10,6,11));
row = (sheet.getRow(5)==null)?(sheet.createRow(5)):(sheet.getRow(5));
cell = (row.getCell(6)==null)?(row.createCell(6)):(row.getCell(6));
cell.setCellValue("THIS IS MERGERD REGION");
//=====CODE FOR AUTOSIZING COLUMNS=====
for(int i=0; i< myHeaders.length; i++)
sheet.autoSizeColumn(i);
FileOutputStream fileOut = new FileOutputStream("BlogDemo.xlsx");
wb.write(fileOut);
fileOut.close();
if(wb!=null)
wb.close();
System.out.println("Done!");
}
}