1 package com.genersoft.cbms.ysbz.ExcelDr.cmd; 2 3 import com.genersoft.cbms.ysbz.ExcelDr.dao.ExcelDrDao; 4 import com.genersoft.cbms.ysbz.ExcelDr.dao.IExcelDrDao; 5 import com.genersoft.cbms.ysbz.ExcelDr.domain.IExcelDrDomain; 6 import com.genersoft.cbms.ysbz.ExcelDr.entity.ExcelDr; 7 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 8 import org.apache.poi.ss.usermodel.Cell; 9 import org.apache.poi.ss.usermodel.Row; 10 import org.apache.poi.ss.usermodel.Sheet; 11 import org.apache.poi.ss.usermodel.Workbook; 12 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 13 import org.loushang.bsp.security.context.GetBspInfo; 14 import org.loushang.bsp.share.organization.OrganFactory; 15 import org.loushang.next.dao.DaoFactory; 16 import org.loushang.next.data.DataSet; 17 import org.loushang.next.data.ParameterSet; 18 import org.loushang.next.data.Record; 19 import org.loushang.next.upload.UploadFile; 20 import org.loushang.next.web.cmd.BaseAjaxCommand; 21 import org.loushang.sca.ScaComponentFactory; 22 23 import java.io.*; 24 import java.net.ServerSocket; 25 import java.util.*; 26 27 /** 28 * Created by lyx on 2016/10/18. 29 */ 30 public class ExcelDrCommand extends BaseAjaxCommand { 31 private static IExcelDrDomain excelDomain = ScaComponentFactory.getService(IExcelDrDomain.class, "excelDomain/excelDomain"); 32 IExcelDrDao exceldao = (IExcelDrDao) DaoFactory.getDao(ExcelDrDao.class.getName()); 33 34 String dynm; 35 36 /** 37 * excel導入 38 */ 39 public void importExcel() { 40 //用來存插到bzsj表里的數據 41 //List<ExcelDr> itemList = new ArrayList<ExcelDr>(); 42 ExcelDr item = new ExcelDr(); 43 44 //獲取用戶名稱 45 String organ_id = GetBspInfo.getBspInfo().getCorporationOrganId();//組織ID 46 String organ_name; 47 if (organ_id == null) { 48 organ_name = "%"; 49 } else { 50 organ_name = OrganFactory.getIOrganProvider().getOrganByOrganId(organ_id).getOrganName();//組織名稱 51 } 52 item.setFcsjUser(organ_id); 53 54 String slnm = (String) getParameter("slnm"); 55 item.setFcsjSlnm(slnm); 56 //前台選中的組織的內碼編號和名稱 57 String zznm = (String) getParameter("zznm"); 58 String zzbh = (String) getParameter("zzbh"); 59 String zzmc = (String) getParameter("zzmc"); 60 //是否選中按組織導入 61 Boolean ifcheck = (Boolean) getParameter("ifcheck"); 62 //前台選中的目標類型的內碼編號和名稱 63 String faMblx = (String) getParameter("faMblx"); 64 String mblxmc = (String) getParameter("mblxmc"); 65 String mblxbh = (String) getParameter("mblxbh"); 66 //獲取前台選中了哪些報表 67 String[] bbbhs = (String[]) getParameter("bbbhs"); 68 69 //不按組織導入時,獲取前台選中了哪些組織 70 String[] zzbhs = (String[]) getParameter("zzbhs"); 71 72 //解析excel 73 Record[] records = (Record[]) getParameter("records"); 74 if (records == null || records.length < 1) 75 return; 76 Record record = records[0]; 77 UploadFile file = (UploadFile) record.get("file"); 78 InputStream inputStream = null; 79 try { 80 inputStream = file.getInputStream(); 81 } catch (FileNotFoundException e) { 82 e.printStackTrace(); 83 } catch (IOException e) { 84 e.printStackTrace(); 85 } 86 // 得到工作表 87 if (inputStream == null) { 88 return; 89 } 90 //文件名 91 String url = file.getFileName(); 92 //文件的后綴名 93 String suffix = url.substring(url.lastIndexOf(".")); 94 // HSSFWorkbook book = null; 95 Workbook book=null; 96 /* try { 97 //如果是xlsx格式,則這一步會報錯 98 book = new HSSFWorkbook(inputStream); 99 } catch (IOException e) { 100 e.printStackTrace(); 101 }*/ 102 try { 103 if(".xls".equals(suffix)){ 104 //支持07版本以前的excel 105 book= new HSSFWorkbook(inputStream); 106 }else if(".xlsx".equals(suffix)){ 107 //支持07版本以后的excel 108 book = new XSSFWorkbook(inputStream); 109 }else{ 110 System.out.println("不支持的文件類型!"); 111 return; 112 } 113 } catch (IOException e) { 114 e.printStackTrace(); 115 } 116 if (book == null) { 117 return; 118 } 119 //得到一共有幾個sheet 120 int sheetnum = book.getNumberOfSheets(); 121 122 //sheet頁循環 123 for (int i = 0; i < sheetnum; i++) { 124 boolean ifbbexist = false; 125 boolean ifzzexist = false; 126 boolean ifbbzzexist = false; 127 Sheet sheet = null; 128 sheet = book.getSheetAt(i); 129 if (sheet != null) { 130 //獲取sheet頁的名稱 131 String sheetName = sheet.getSheetName(); 132 //獲取報表中的最后一行的行號,則總行數等於它加1 133 int allrow = sheet.getLastRowNum() + 1; 134 135 String sheetzzbh = ""; 136 String[] sheetnmmc = new String[2]; 137 if (ifcheck) { 138 //如果是安組織導入,將得到的sheet頁名稱分開,以便獲取報表編號 139 String[] sheetNames = sheetName.split("\\."); 140 String bbbh = sheetNames[0]; 141 for (int n = 0; n < bbbhs.length; n++) { 142 if (bbbh.equals(bbbhs[n])) { 143 ifbbexist = true; 144 } 145 } 146 if (ifbbexist) { 147 dynm = getDynm(bbbh); 148 item.setFcsjZznm(zznm); 149 item.setFcsjZzbh(zzbh); 150 item.setFcsjZzmc(zzmc); 151 } 152 } else { 153 //將得到的sheet頁名稱分開,以便獲取報表編號 154 String[] sheetNames = sheetName.split("\\."); 155 String bbbh = sheetNames[0]; 156 //將得到的sheet頁名稱分開,以便獲取組織編號 157 sheetzzbh = sheetNames[1].substring(sheetNames[1].indexOf("(") + 1, sheetNames[1].indexOf(")")); 158 //判斷此sheet頁的報表編號是否是選中的 159 for (int b = 0; b < bbbhs.length; b++) { 160 if (bbbh.equals(bbbhs[b])) { 161 ifbbexist = true; 162 } 163 } 164 //判斷此sheet頁的組織編號是否是選中的 165 for (int z = 0; z < zzbhs.length; z++) { 166 if (sheetzzbh.equals(zzbhs[z])) { 167 ifzzexist = true; 168 } 169 } 170 if (ifbbexist && ifzzexist) { 171 ifbbzzexist = true; 172 dynm = getDynm(bbbh); 173 sheetnmmc = getZznmmc(sheetzzbh); 174 item.setFcsjZznm(sheetnmmc[0]); 175 item.setFcsjZzbh(sheetzzbh); 176 item.setFcsjZzmc(sheetnmmc[1]); 177 } 178 } 179 //1.(按組織導入)如果sheet頁中的報表編號跟前台選中的編號相等,才能導入 180 //2.(不按組織導入)sheet頁中的報表編號和組織編號跟前台選中的編號相等,才能導入 181 if ((ifbbexist && ifcheck) || ifbbzzexist) { 182 //獲取該報表是否是兩欄表頭的表格 183 List<String> zhcs = getIfzh(dynm); 184 String ifzh = zhcs.get(0); 185 String kzhs = zhcs.get(1); 186 int kzh = Integer.parseInt(kzhs); 187 //獲取該報表數據庫里一共有幾列 188 int tableCol = getTableCol(dynm); 189 //獲取內容行從哪一行開始 190 int rownum = getRownum(dynm); 191 //內容行的行號(從1開始) 192 String mxnm = getMxnm(dynm); 193 DataSet dsc = getSjl(dynm); 194 DataSet dsr = getXxl(dynm); 195 int xxls = getXxls(dynm); 196 int hzbbStat = getStat(dynm, item); 197 //編制行里的行記錄 198 List<HashMap<String, Object>> recordsList = getRecList(dynm,mxnm,item); 199 200 int footerRow = getFooternum(dynm); 201 202 //兩欄表 203 if(ifzh.equals("1")){ 204 int rowXh = 1; 205 for (int j = rownum; j < rownum+kzh-1; j++) { 206 int firstLan =1; 207 //cell單元格的值 208 String value = ""; 209 Map<Integer, String> dataMap = new HashMap<Integer, String>(); 210 int col = 0; 211 // 得到j的那一行 212 Row rowi = sheet.getRow(j); 213 // 得到該行的所有列 214 Iterator<Cell> cellTitle = rowi.cellIterator(); 215 // 循環標題所有的列 216 while (cellTitle.hasNext()) { 217 if(0<firstLan && firstLan<=tableCol){ 218 Cell cell = (Cell) cellTitle.next(); 219 int type = cell.getCellType(); 220 switch (type) { 221 case 0: 222 Double dValue = (Double) cell.getNumericCellValue(); 223 value = dValue.toString(); 224 break; 225 case 1: 226 value = cell.getStringCellValue(); 227 break; 228 case 3: 229 value = ""; 230 break; 231 } 232 dataMap.put(col, value); 233 col++; 234 }else{ 235 break; 236 } 237 firstLan++; 238 } 239 Date date = new Date(); 240 item.setCreatedtime(date); 241 item.setLastmodifiedtime(date); 242 exceldao.importExcel(dynm, dataMap, item, rowXh,mxnm,recordsList,xxls,hzbbStat,dsc,dsr); 243 rowXh++; 244 } 245 for (int j = rownum; j <= rownum+recordsList.size()-kzh; j++) { 246 int secontLan =1; 247 //cell單元格的值 248 String value = ""; 249 Map<Integer, String> dataMap = new HashMap<Integer, String>(); 250 int col = 0; 251 // 得到j的那一行 252 Row rowi = sheet.getRow(j); 253 // 得到該行的所有列 254 Iterator<Cell> cellTitle = rowi.cellIterator(); 255 // 循環標題所有的列 256 while (cellTitle.hasNext()) { 257 Cell cell = (Cell) cellTitle.next(); 258 if(tableCol<secontLan && secontLan<=tableCol*2){ 259 int type = cell.getCellType(); 260 switch (type) { 261 case 0: 262 Double dValue = (Double) cell.getNumericCellValue(); 263 value = dValue.toString(); 264 break; 265 case 1: 266 value = cell.getStringCellValue(); 267 break; 268 case 3: 269 value = ""; 270 break; 271 } 272 dataMap.put(col, value); 273 col++; 274 } 275 if(secontLan>tableCol*2){ 276 break; 277 } 278 secontLan++; 279 } 280 Date date = new Date(); 281 item.setCreatedtime(date); 282 item.setLastmodifiedtime(date); 283 exceldao.importExcel(dynm, dataMap, item, rowXh,mxnm,recordsList,xxls,hzbbStat,dsc,dsr); 284 rowXh++; 285 } 286 }else{ 287 int rowXh = 1; 288 //正常表 289 for (int j = rownum; j < rownum+recordsList.size(); j++) { 290 //cell單元格的值 291 String value = ""; 292 Map<Integer, String> dataMap = new HashMap<Integer, String>(); 293 int col = 0; 294 // 得到j的那一行 295 Row rowi = sheet.getRow(j); 296 // 得到該行的所有列 297 Iterator<Cell> cellTitle = rowi.cellIterator(); 298 // 循環標題所有的列 299 while (cellTitle.hasNext()) { 300 Cell cell = (Cell) cellTitle.next(); 301 /* cell.getCellType()返回的類型: 302 int CELL_TYPE_NUMERIC = 0; 303 int CELL_TYPE_STRING = 1; 304 int CELL_TYPE_FORMULA = 2; 305 int CELL_TYPE_BLANK = 3; 306 int CELL_TYPE_BOOLEAN = 4; 307 int CELL_TYPE_ERROR = 5;*/ 308 int type = cell.getCellType(); 309 switch (type) { 310 case 0: 311 Double dValue = (Double) cell.getNumericCellValue(); 312 value = dValue.toString(); 313 break; 314 case 1: 315 value = cell.getStringCellValue(); 316 break; 317 case 3: 318 value = ""; 319 break; 320 } 321 dataMap.put(col, value); 322 col++; 323 } 324 Date date = new Date(); 325 item.setCreatedtime(date); 326 item.setLastmodifiedtime(date); 327 exceldao.importExcel(dynm, dataMap, item, rowXh,mxnm,recordsList,xxls,hzbbStat,dsc,dsr); 328 /*exceldao.importExcel(dynm,value,item);*/ 329 rowXh++; 330 } 331 } 332 } 333 } 334 } 335 } 336 337 //獲取報表的對應內碼 338 public String getDynm(String bbbh) { 339 String txnm = (String) getParameter("txnm"); 340 return exceldao.getDynm(txnm, bbbh); 341 } 342 public String getMxnm(String dynm) { 343 return exceldao.getMxnm(dynm); 344 } 345 346 //獲取報表是否兩欄表頭 347 public List<String> getIfzh(String dynm){ 348 return exceldao.getIfzh(dynm); 349 } 350 //獲取報表一共有幾列 351 public int getTableCol(String dynm){ 352 return exceldao.getTableCol(dynm); 353 } 354 355 //根據sheet的組織編號獲取組織的內碼和名稱 356 public String[] getZznmmc(String sheetzzbh) { 357 String txnm = (String) getParameter("sheetzzbh"); 358 return exceldao.getZznmmc(sheetzzbh); 359 } 360 361 //確定報表從第幾行開始才是數據行(內容行) 362 public int getRownum(String dynm) { 363 return exceldao.getRownum(dynm); 364 } 365 public DataSet getSjl(String dynm) { 366 return exceldao.getSjl(dynm); 367 } 368 public DataSet getXxl(String dynm) { 369 return exceldao.getXxl(dynm); 370 } 371 public int getXxls(String dynm) { 372 return exceldao.getXxls(dynm); 373 } 374 public int getStat(String dynm,ExcelDr item) { 375 return exceldao.getStat(dynm,item); 376 } 377 378 //確定報表從第幾行開始才是數據行(內容行) 379 public int getFooternum(String dynm) { 380 return exceldao.getFooternum(dynm); 381 } 382 public List<HashMap<String, Object>> getRecList(String dynm,String mxnm,ExcelDr item) { 383 return exceldao.getRecList(dynm,mxnm,item); 384 } 385 386 /** 387 * @param 388 * @return void 返回類型 389 * @throws 390 * @Title: getCS 391 * @Description: 獲取當前用戶所屬組織的信息 392 */ 393 public void getCS() { 394 395 String organ_id = GetBspInfo.getBspInfo().getCorporationOrganId();//組織ID 396 397 if (organ_id == null) { 398 setReturn("organ_id", "%"); 399 } else { 400 String organ_name = OrganFactory.getIOrganProvider().getOrganByOrganId(organ_id).getOrganName();//組織姓名 401 setReturn("organ_id", organ_id); 402 } 403 } 404 405 public void getParentOrgans() { 406 String organId = (String) getParameter("organId"); 407 List<String> parentOrgans = new ArrayList<String>(); 408 parentOrgans = exceldao.getParentOrgans(organId); 409 setReturn("organList", parentOrgans); 410 } 411 public void getFaslnm(){ 412 ParameterSet pset = getParameterSet(); 413 String faslnm= exceldao.getFaslnm(pset); 414 setReturn("faslnm", faslnm); 415 } 416 }
這些代碼,必須要引入相應jar包才可以。