Subversion Repositories SmartDukaan

Rev

Rev 13452 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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");
92
	    headerRow.createCell(3).setCellValue("Order No");
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());
122
			    awbDateCell.setCellStyle(dateCellStyle);		    
123
			    contentRow.createCell(3).setCellValue(cells.get(3).getNumericCellValue());
124
			    contentRow.createCell(4).setCellValue(cells.get(4).getStringCellValue());
125
			    contentRow.createCell(5).setCellValue(cells.get(5).getStringCellValue());
126
			    contentRow.createCell(6).setCellValue(cells.get(6).getStringCellValue());
127
			    contentRow.createCell(7).setCellValue(cells.get(7).getStringCellValue());
128
			    contentRow.createCell(8).setCellValue(cells.get(8).getStringCellValue());
129
			    contentRow.createCell(9).setCellValue(cells.get(9).getStringCellValue());
130
			    contentRow.createCell(10).setCellValue(cells.get(10).getStringCellValue());
131
			    contentRow.createCell(11).setCellValue("-");
132
			    contentRow.createCell(12).setCellValue(cells.get(12).getStringCellValue());
133
			    contentRow.createCell(13).setCellValue(cells.get(13).getNumericCellValue());
134
			    contentRow.createCell(14).setCellValue(cells.get(14).getNumericCellValue());
135
			    contentRow.createCell(15).setCellValue(cells.get(15).getNumericCellValue());
136
			    Cell weightCell = contentRow.createCell(16);
137
			    weightCell.setCellValue(cells.get(16).getNumericCellValue());
138
			    weightCell.setCellStyle(weightStyle);
139
			    contentRow.createCell(17).setCellValue(cells.get(17).getStringCellValue());
140
			    contentRow.createCell(18).setCellValue(cells.get(18).getStringCellValue());
141
			    contentRow.createCell(19).setCellValue(cells.get(19).getStringCellValue());
142
		    }
143
	    }	    
144
		// Write the workbook to the output stream
145
		try {
146
			wb.write(baosXLS);
147
			baosXLS.close();
148
		} catch (IOException e) {
149
			logger.error("Exception while creating the Courier Details report", e);
150
		}
151
 
152
		return baosXLS;
153
	}
154
 
155
 
156
 
157
	/**
158
	 * @param args
159
	 */
160
	public static void main(String[] args) {
161
		System.out.println("Hey There");
162
		CourierDetailsReportMerger merger = new CourierDetailsReportMerger();
163
		try {
164
			FileOutputStream f = new FileOutputStream("/home/rajveer/Desktop/temp/courier-details1.xls");
165
			Map<Long, String> warehouseIdFileNames = new HashMap<Long, String>();
166
 
167
 
168
			String fName;
169
			fName = "/home/rajveer/Desktop/temp/courier-details-prepaid-1-1-2011-12-5.xls";
170
			warehouseIdFileNames.put(1L, fName);
171
			fName = "/home/rajveer/Desktop/temp/courier-details-prepaid-2-1-2011-12-5.xls";
172
			warehouseIdFileNames.put(2L, fName);
173
			fName = "/home/rajveer/Desktop/temp/courier-details-prepaid-5-1-2011-12-5.xls";
174
			warehouseIdFileNames.put(5L, fName);
175
			ByteArrayOutputStream binXLS = merger.mergeCourierDetailsReports(warehouseIdFileNames, 1, true);
176
			binXLS.writeTo(f);
177
			f.close();
178
		} catch (FileNotFoundException e) {
179
			logger.error("Error while creating the Courier Details report", e);
180
		} catch (IOException e) {
181
			logger.error("IO error while writing the Courier Details report", e);
182
		}
183
		System.out.println("Successfully generated the detailed courier report");
184
	}
185
 
186
}