forked from anshulc55/JavaTraining
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteExcelFile.java
More file actions
83 lines (59 loc) · 2.15 KB
/
WriteExcelFile.java
File metadata and controls
83 lines (59 loc) · 2.15 KB
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
package utilities;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.poi.sl.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class WriteExcelFile {
public static void main(String[] args) {
//Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
//Create Excel Sheet
XSSFSheet samplesheet = workbook.createSheet("SampleSheet");
//Creating the Data
Map<String, Object[]> dataSet = new TreeMap<String, Object[]>();
dataSet.put("1", new Object[] {"ID", "NAME", "Company"});
dataSet.put("2", new Object[] {"1", "James", "PertLine Inc"});
dataSet.put("3", new Object[] {"2", "Maria", "SumoLogic Inc"});
dataSet.put("4", new Object[] {"3", "Peter", "Siemens Corp."});
dataSet.put("5", new Object[] {"4", "Julia", "Google Inc"});
dataSet.put("6", new Object[] {"5", "Ajay", "FaceBook Inc"});
//Iterate over the Data
Set<String> set = dataSet.keySet();
int rownum = 0;
for (String key : set) {
Row row = samplesheet.createRow(rownum++);
Object[] data = dataSet.get(key);
int cellNum = 0;
for (Object value : data) {
Cell cell = row.createCell(cellNum++);
if (value instanceof String)
cell.setCellValue((String)value);
else if(value instanceof Integer)
cell.setCellValue((Integer)value);
}
}
//Write Down file on HadDisk
try {
FileOutputStream writeFile = new FileOutputStream("sampleTest.xlsx");
// For MacUsers /users/customDir Name/FileName
//For Windows C:/Test/Sample/..../Filename
// C://Test//Sample//....//filename
workbook.write(writeFile);
writeFile.close();
System.out.println("Sample Excel file is being created Successfully");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}