| 4209 |
rajveer |
1 |
package in.shop2020.support.services;
|
|
|
2 |
|
|
|
3 |
|
|
|
4 |
import java.io.ByteArrayOutputStream;
|
|
|
5 |
import java.io.FileInputStream;
|
|
|
6 |
import java.io.FileNotFoundException;
|
|
|
7 |
import java.io.FileOutputStream;
|
|
|
8 |
import java.io.IOException;
|
|
|
9 |
import java.util.ArrayList;
|
|
|
10 |
import java.util.HashMap;
|
|
|
11 |
import java.util.Iterator;
|
|
|
12 |
import java.util.List;
|
|
|
13 |
import java.util.Map;
|
|
|
14 |
|
|
|
15 |
import org.apache.poi.hssf.usermodel.HSSFCell;
|
|
|
16 |
import org.apache.poi.hssf.usermodel.HSSFRow;
|
|
|
17 |
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
18 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
19 |
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
|
|
20 |
import org.apache.poi.ss.usermodel.Cell;
|
|
|
21 |
import org.apache.poi.ss.usermodel.CellStyle;
|
|
|
22 |
import org.apache.poi.ss.usermodel.CreationHelper;
|
|
|
23 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
24 |
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
25 |
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
26 |
import org.slf4j.Logger;
|
|
|
27 |
import org.slf4j.LoggerFactory;
|
|
|
28 |
|
|
|
29 |
public class CourierDetailsReportMerger {
|
|
|
30 |
private static Logger logger = LoggerFactory.getLogger(CourierDetailsReportMerger.class);
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
public List<List<Cell>> getCourierDetailsReportRows(String fileName){
|
|
|
34 |
|
|
|
35 |
List<List<Cell>> rows = new ArrayList<List<Cell>>();
|
|
|
36 |
|
|
|
37 |
try{
|
|
|
38 |
/** Creating Input Stream**/
|
|
|
39 |
FileInputStream myInput = new FileInputStream(fileName);
|
|
|
40 |
|
|
|
41 |
/** Create a POIFSFileSystem object**/
|
|
|
42 |
POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
|
|
|
43 |
|
|
|
44 |
/** Create a workbook using the File System**/
|
|
|
45 |
HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
|
|
|
46 |
|
|
|
47 |
/** Get the first sheet from workbook**/
|
|
|
48 |
HSSFSheet mySheet = myWorkBook.getSheetAt(0);
|
|
|
49 |
|
|
|
50 |
/** We now need something to iterate through the cells.**/
|
|
|
51 |
Iterator<Row> rowIter = mySheet.rowIterator();
|
|
|
52 |
boolean firstRow = false;
|
|
|
53 |
while(rowIter.hasNext()){
|
|
|
54 |
HSSFRow myRow = (HSSFRow) rowIter.next();
|
|
|
55 |
if(!firstRow){
|
|
|
56 |
firstRow = true;
|
|
|
57 |
continue;
|
|
|
58 |
}
|
|
|
59 |
Iterator<Cell> cellIter = myRow.cellIterator();
|
|
|
60 |
List<Cell> cells = new ArrayList<Cell>();
|
|
|
61 |
while(cellIter.hasNext()){
|
|
|
62 |
HSSFCell myCell = (HSSFCell) cellIter.next();
|
|
|
63 |
cells.add(myCell);
|
|
|
64 |
}
|
|
|
65 |
rows.add(cells);
|
|
|
66 |
}
|
|
|
67 |
}catch (Exception e){
|
|
|
68 |
e.printStackTrace();
|
|
|
69 |
}
|
|
|
70 |
return rows;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
public ByteArrayOutputStream mergeCourierDetailsReports(Map<Long, String> warehouseIdFileNames, long providerId, boolean isCod){
|
|
|
75 |
ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
|
|
|
76 |
|
|
|
77 |
Workbook wb = new HSSFWorkbook();
|
|
|
78 |
CreationHelper createHelper = wb.getCreationHelper();
|
|
|
79 |
Sheet sheet = wb.createSheet("new sheet");
|
|
|
80 |
|
|
|
81 |
CellStyle dateCellStyle = wb.createCellStyle();
|
|
|
82 |
dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("d/m/yyyy"));
|
|
|
83 |
|
|
|
84 |
CellStyle weightStyle = wb.createCellStyle();
|
|
|
85 |
weightStyle.setDataFormat(createHelper.createDataFormat().getFormat("0.000"));
|
|
|
86 |
|
|
|
87 |
// Create the header row and put all the titles in it. Rows are 0 based.
|
|
|
88 |
Row headerRow = sheet.createRow((short)0);
|
|
|
89 |
headerRow.createCell(0).setCellValue("Sl No");
|
|
|
90 |
headerRow.createCell(1).setCellValue("AWB No");
|
|
|
91 |
headerRow.createCell(2).setCellValue("AWB Date");
|
| 13452 |
manish.sha |
92 |
headerRow.createCell(3).setCellValue("Saholic Order Id");
|
| 4209 |
rajveer |
93 |
headerRow.createCell(4).setCellValue("Name");
|
|
|
94 |
headerRow.createCell(5).setCellValue("Address 1");
|
|
|
95 |
headerRow.createCell(6).setCellValue("Address 2");
|
|
|
96 |
headerRow.createCell(7).setCellValue("City");
|
|
|
97 |
headerRow.createCell(8).setCellValue("State");
|
|
|
98 |
headerRow.createCell(9).setCellValue("Pin Code");
|
|
|
99 |
headerRow.createCell(10).setCellValue("Telephone No 1");
|
|
|
100 |
headerRow.createCell(11).setCellValue("Telephone No 2");
|
|
|
101 |
headerRow.createCell(12).setCellValue("Paymode");
|
|
|
102 |
headerRow.createCell(13).setCellValue("Amount to be Collected");
|
|
|
103 |
headerRow.createCell(14).setCellValue("Shipment Value");
|
|
|
104 |
headerRow.createCell(15).setCellValue("Item ID");
|
|
|
105 |
headerRow.createCell(16).setCellValue("Packet Weight(in Kg)");
|
|
|
106 |
headerRow.createCell(17).setCellValue("Product Name");
|
|
|
107 |
headerRow.createCell(18).setCellValue("Pickup Location");
|
|
|
108 |
headerRow.createCell(19).setCellValue("Customer A/C Code");
|
|
|
109 |
int serialNo = 0;
|
|
|
110 |
|
|
|
111 |
for(long warehouseId: warehouseIdFileNames.keySet()){
|
|
|
112 |
List<List<Cell>> rows = getCourierDetailsReportRows(warehouseIdFileNames.get(warehouseId));
|
|
|
113 |
|
|
|
114 |
for(List<Cell> cells: rows){
|
|
|
115 |
serialNo++;
|
|
|
116 |
Row contentRow = sheet.createRow((short)serialNo);
|
|
|
117 |
contentRow.createCell(0).setCellValue(serialNo);
|
|
|
118 |
contentRow.createCell(1).setCellValue(cells.get(1).getStringCellValue());
|
|
|
119 |
|
|
|
120 |
Cell awbDateCell = contentRow.createCell(2);
|
|
|
121 |
awbDateCell.setCellValue(cells.get(2).getDateCellValue());
|
| 13452 |
manish.sha |
122 |
awbDateCell.setCellStyle(dateCellStyle);
|
|
|
123 |
if(cells.get(3).getCellType()==Cell.CELL_TYPE_NUMERIC){
|
|
|
124 |
contentRow.createCell(3).setCellValue(cells.get(3).getNumericCellValue());
|
|
|
125 |
}
|
|
|
126 |
if(cells.get(3).getCellType()==Cell.CELL_TYPE_STRING){
|
|
|
127 |
contentRow.createCell(3).setCellValue(cells.get(3).getStringCellValue());
|
|
|
128 |
}
|
| 4209 |
rajveer |
129 |
contentRow.createCell(4).setCellValue(cells.get(4).getStringCellValue());
|
|
|
130 |
contentRow.createCell(5).setCellValue(cells.get(5).getStringCellValue());
|
|
|
131 |
contentRow.createCell(6).setCellValue(cells.get(6).getStringCellValue());
|
|
|
132 |
contentRow.createCell(7).setCellValue(cells.get(7).getStringCellValue());
|
|
|
133 |
contentRow.createCell(8).setCellValue(cells.get(8).getStringCellValue());
|
|
|
134 |
contentRow.createCell(9).setCellValue(cells.get(9).getStringCellValue());
|
|
|
135 |
contentRow.createCell(10).setCellValue(cells.get(10).getStringCellValue());
|
|
|
136 |
contentRow.createCell(11).setCellValue("-");
|
|
|
137 |
contentRow.createCell(12).setCellValue(cells.get(12).getStringCellValue());
|
|
|
138 |
contentRow.createCell(13).setCellValue(cells.get(13).getNumericCellValue());
|
|
|
139 |
contentRow.createCell(14).setCellValue(cells.get(14).getNumericCellValue());
|
| 13452 |
manish.sha |
140 |
Cell weightCell = contentRow.createCell(15);
|
|
|
141 |
weightCell.setCellValue(cells.get(15).getNumericCellValue());
|
| 4209 |
rajveer |
142 |
weightCell.setCellStyle(weightStyle);
|
| 13453 |
manish.sha |
143 |
if(cells.get(16).getCellType()==Cell.CELL_TYPE_NUMERIC){
|
|
|
144 |
contentRow.createCell(16).setCellValue(cells.get(16).getNumericCellValue());
|
|
|
145 |
}
|
|
|
146 |
if(cells.get(16).getCellType()==Cell.CELL_TYPE_STRING){
|
|
|
147 |
contentRow.createCell(16).setCellValue(cells.get(16).getStringCellValue());
|
|
|
148 |
}
|
| 4209 |
rajveer |
149 |
contentRow.createCell(17).setCellValue(cells.get(17).getStringCellValue());
|
|
|
150 |
contentRow.createCell(18).setCellValue(cells.get(18).getStringCellValue());
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
// Write the workbook to the output stream
|
|
|
154 |
try {
|
|
|
155 |
wb.write(baosXLS);
|
|
|
156 |
baosXLS.close();
|
|
|
157 |
} catch (IOException e) {
|
|
|
158 |
logger.error("Exception while creating the Courier Details report", e);
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
return baosXLS;
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* @param args
|
|
|
168 |
*/
|
|
|
169 |
public static void main(String[] args) {
|
|
|
170 |
System.out.println("Hey There");
|
|
|
171 |
CourierDetailsReportMerger merger = new CourierDetailsReportMerger();
|
|
|
172 |
try {
|
|
|
173 |
FileOutputStream f = new FileOutputStream("/home/rajveer/Desktop/temp/courier-details1.xls");
|
|
|
174 |
Map<Long, String> warehouseIdFileNames = new HashMap<Long, String>();
|
|
|
175 |
|
|
|
176 |
|
|
|
177 |
String fName;
|
|
|
178 |
fName = "/home/rajveer/Desktop/temp/courier-details-prepaid-1-1-2011-12-5.xls";
|
|
|
179 |
warehouseIdFileNames.put(1L, fName);
|
|
|
180 |
fName = "/home/rajveer/Desktop/temp/courier-details-prepaid-2-1-2011-12-5.xls";
|
|
|
181 |
warehouseIdFileNames.put(2L, fName);
|
|
|
182 |
fName = "/home/rajveer/Desktop/temp/courier-details-prepaid-5-1-2011-12-5.xls";
|
|
|
183 |
warehouseIdFileNames.put(5L, fName);
|
|
|
184 |
ByteArrayOutputStream binXLS = merger.mergeCourierDetailsReports(warehouseIdFileNames, 1, true);
|
|
|
185 |
binXLS.writeTo(f);
|
|
|
186 |
f.close();
|
|
|
187 |
} catch (FileNotFoundException e) {
|
|
|
188 |
logger.error("Error while creating the Courier Details report", e);
|
|
|
189 |
} catch (IOException e) {
|
|
|
190 |
logger.error("IO error while writing the Courier Details report", e);
|
|
|
191 |
}
|
|
|
192 |
System.out.println("Successfully generated the detailed courier report");
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
}
|