PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/com/cf/tkconnect/data/ReadExcel.java

https://github.com/zypacsinc/src
Java | 268 lines | 235 code | 28 blank | 5 comment | 36 complexity | c84de12df4ef6494223eef23968afe1c MD5 | raw file
  1. package com.cf.tkconnect.data;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.Iterator;
  8. import java.util.List;
  9. import java.util.Map;
  10. import javax.servlet.http.HttpServletResponse;
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  12. import org.apache.poi.ss.usermodel.Cell;
  13. import org.apache.poi.ss.usermodel.CreationHelper;
  14. import org.apache.poi.ss.usermodel.IndexedColors;
  15. import org.apache.poi.ss.usermodel.Row;
  16. import org.apache.poi.ss.usermodel.Workbook;
  17. import org.apache.poi.ss.util.CellReference;
  18. import org.apache.poi.xssf.usermodel.XSSFCell;
  19. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  20. import org.apache.poi.xssf.usermodel.XSSFColor;
  21. import org.apache.poi.xssf.usermodel.XSSFFont;
  22. import org.apache.poi.xssf.usermodel.XSSFRow;
  23. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  24. import org.apache.poi.xssf.usermodel.XSSFSheet;
  25. import com.cf.tkconnect.data.form.BPAttributeData;
  26. import com.cf.tkconnect.data.process.ProcessBPXMLTemplate;
  27. import com.cf.tkconnect.log.Log;
  28. import com.cf.tkconnect.log.LogSource;
  29. import com.cf.tkconnect.util.WSUtil;
  30. public class ReadExcel {
  31. static Log logger = LogSource.getInstance(ReadExcel.class);
  32. long fileid;
  33. String filename;
  34. File file;
  35. List<String> upperdenames = new ArrayList<String>();
  36. List<String> lidenames = new ArrayList<String>();
  37. List<String[]> upperdata = new ArrayList<String[]>();
  38. List<String[]> lidata = new ArrayList<String[]>();
  39. String prefix;
  40. ProcessBPXMLTemplate bptemp;
  41. Map<String,Object> studiomap;
  42. String stringtype ="json";
  43. BPAttributeData bpattr;
  44. boolean hasError = false;
  45. String errorStr = "";
  46. public ReadExcel(String filename, File file, long fileid){
  47. this.filename = filename;
  48. this.file = file;
  49. this.fileid = fileid;
  50. }
  51. public ReadExcel(long fileid){
  52. this.fileid = fileid;
  53. stringtype = "xml";
  54. }
  55. public void process() throws Exception{
  56. int beginIndex = this.filename.indexOf("unifier_");
  57. int endIndex = this.filename.indexOf("_bp");
  58. if(beginIndex < 0 || endIndex <= 0 || endIndex <= beginIndex ){
  59. hasError = true;
  60. errorStr = "File name "+filename+" is not correct.";
  61. return;
  62. }
  63. this.prefix = this.filename.substring(beginIndex+8, endIndex);
  64. // now get the
  65. TKUnifierMetaData ud = new TKUnifierMetaData();
  66. this.studiomap = ud.getStudioInfo(this.prefix);
  67. if(this.studiomap.containsKey("studio_name")){
  68. bpattr = new BPAttributeData(this.prefix,"");
  69. bptemp = bpattr.getBPDesignDetails();
  70. }else {
  71. hasError = true;
  72. errorStr = "File name "+filename+" has been modified incorrectly.";
  73. return;
  74. }
  75. try{
  76. readWB();
  77. }catch (Exception e){
  78. logger.error(e, e);
  79. //return "{\"errors\":\"File name "+filename+" has errors "+e.getMessage()+" \"}";
  80. hasError = true;
  81. errorStr = "File name "+filename+" has errors "+e.getMessage();
  82. }
  83. }
  84. public ProcessBPXMLTemplate getBPTemp(){
  85. return this.bptemp;
  86. }
  87. public Map<String,Object> getStudioMap(){
  88. return this.studiomap;
  89. }
  90. public boolean hasError(){
  91. return this.hasError;
  92. }
  93. public String getError(){
  94. return this.errorStr;
  95. }
  96. public List<String[]> getUpperData(){
  97. return this.upperdata;
  98. }
  99. public List<String[]> getLiData(){
  100. return this.lidata;
  101. }
  102. public String getJsonData() throws Exception{
  103. if(hasError)
  104. return "{\"errors\":\""+errorStr+"\"}";
  105. StringBuilder buf = new StringBuilder("{");
  106. buf.append("\"prefix\":\""+this.prefix+"\",");
  107. buf.append("\"fileid\":\""+this.fileid+"\",");
  108. buf.append("\"file_name\":\""+WSUtil.jsfilter2(this.filename)+"\",");
  109. int count = 0;
  110. buf.append("\"upper_de\":{");
  111. for(String de : upperdenames){
  112. if(count > 0)
  113. buf.append(",");
  114. buf.append("\""+de+"\":\"\"");
  115. count++;
  116. }
  117. buf.append("},");
  118. buf.append("\"upper_data\":[");
  119. count = 0;
  120. for(String[] ud :upperdata){
  121. if(count > 0)
  122. buf.append(",");
  123. buf.append("{");
  124. for(int i = 0; i < ud.length; i++){
  125. String de = upperdenames.get(i);
  126. if(i > 0)
  127. buf.append(",");
  128. buf.append("\"").append(de).append("\":\"").append(WSUtil.jsfilter2(ud[i])).append("\"");
  129. }
  130. buf.append("}");
  131. count++;
  132. }
  133. buf.append("]");
  134. if(lidenames != null & lidenames.size() > 0){
  135. count = 0;
  136. buf.append(",").append("\"lineitem_de\":{");;
  137. for(String de : lidenames){
  138. if(count > 0)
  139. buf.append(",");
  140. buf.append("\""+de+"\":\"\"");
  141. count++;
  142. }
  143. buf.append("}");
  144. buf.append(",").append("\"lineitem_data\":[");;
  145. count = 0;
  146. for(String[] ud :lidata){
  147. if(count > 0)
  148. buf.append(",");
  149. buf.append("{");
  150. for(int i = 0; i < ud.length; i++){
  151. String de = lidenames.get(i);
  152. if(i > 0)
  153. buf.append(",");
  154. buf.append("\"").append(de).append("\":\"").append(WSUtil.jsfilter2(ud[i])).append("\"");
  155. }
  156. buf.append("}");
  157. count++;
  158. }
  159. buf.append("]");
  160. }
  161. if(bpattr != null){
  162. buf.append(",").append("\"bp_info\":").append(bpattr.getBPJsonData());
  163. }
  164. buf.append("}");
  165. return buf.toString();
  166. }
  167. private void readWB() throws Exception{
  168. java.io.InputStream inp = new FileInputStream(file);
  169. XSSFWorkbook wb = new XSSFWorkbook(inp) ;
  170. XSSFSheet sheet1 = wb.getSheetAt(0);
  171. readRows( sheet1,"upper");
  172. if(wb.getNumberOfSheets() > 2){
  173. XSSFSheet sheet2 = wb.getSheetAt(1);
  174. readRows( sheet2,"lineitems");
  175. }
  176. }//readwb
  177. private void readRows(XSSFSheet sheet, String type) throws Exception{
  178. Iterator<Row> it = sheet.iterator() ;
  179. int count = 0;
  180. while (it.hasNext() ) {
  181. XSSFRow row = (XSSFRow)it.next();
  182. if(count == 0)
  183. readHeaderRow(row,type);
  184. else
  185. readDataRow(row,type);
  186. count++;
  187. logger.debug("readRows count :"+count+" upperdata :"+upperdata.size()+" type:"+type);
  188. }
  189. }
  190. private void readHeaderRow(XSSFRow row, String type) throws Exception{
  191. Iterator<Cell> cellit = row.cellIterator();
  192. while (cellit.hasNext() ) {
  193. XSSFCell cell = (XSSFCell) cellit.next();
  194. String value = cell.getStringCellValue() ;
  195. if(value == null )
  196. return;
  197. if(value.length() < 2)
  198. continue;
  199. int startindex = value.indexOf("(");
  200. int endindex = value.indexOf(")");
  201. String dename = value.substring(startindex+1, endindex);
  202. if(logger.isDebugEnabled())
  203. logger.debug("readHeaderRow ::"+value+" :::: "+dename);
  204. if("upper".equals(type))
  205. upperdenames.add(dename);
  206. else
  207. lidenames.add(dename);
  208. }
  209. }
  210. private void readDataRow(XSSFRow row,String type) throws Exception{
  211. // Iterator<Cell> cellit = row.cellIterator();
  212. String[] strdata = new String[upperdenames.size()];
  213. if(type.equals("lineitems"))
  214. strdata = new String[lidenames.size()];
  215. // int count = 0;
  216. for(int count = 0; count < strdata.length; count++){
  217. // while (cellit.hasNext() ) {
  218. XSSFCell cell = row.getCell(count);
  219. if(cell == null){
  220. strdata[count] = "";
  221. continue;
  222. }
  223. switch(cell.getCellType()) {
  224. case Cell.CELL_TYPE_BOOLEAN:
  225. strdata[count] = cell.getBooleanCellValue()+"";
  226. break;
  227. case Cell.CELL_TYPE_NUMERIC:
  228. strdata[count] = cell.getNumericCellValue()+"";
  229. break;
  230. case Cell.CELL_TYPE_STRING:
  231. strdata[count] = cell.getStringCellValue();
  232. break;
  233. }
  234. }
  235. if(type.equals("lineitems"))
  236. lidata.add(strdata);
  237. else
  238. upperdata.add(strdata);
  239. }
  240. }