//上传位置(与操作excel无关,可不看)
public String getUploadPath() { File theWebFolder = XMPPServer.getInstance().getAdminFolder(); String path = theWebFolder.getAbsolutePath() + File.separator + "ucstar_plugins/ucyingjiyanlian/upload/"; return path; } public String getUploadMediaPath() { File theWebFolder = XMPPServer.getInstance().getAdminFolder(); String path = theWebFolder.getAbsolutePath() + File.separator + "ucstar_plugins/ucyingjiyanlian/upload/media/"; return path; }
// excel2003跟2007操作差不多、但是也有区别可以通过判断后缀名区别然后分别调用方法处理
//2003处理
public boolean exportExcelData(String _fileName, String _incidentUri) { boolean flag = false; if (JiveGlobals.isEmpty(_fileName) || JiveGlobals.isEmpty(_incidentUri)) { return flag; } try { String savePath = YingjiyanlianBaseManager.getInstance() .getUploadPath(); File saveFile = new File(savePath, _fileName); if (saveFile.exists()) { InputStream in = new BufferedInputStream(new FileInputStream( saveFile)); List<UcstarYingjiSend> fileto = new ArrayList<UcstarYingjiSend>(); POIFSFileSystem fs = new POIFSFileSystem(in); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFCell cell = null; // sheet循环 for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) { // 第一张表的值 HSSFSheet st = wb.getSheetAt(sheetIndex); // 获取第二行以下的信息 for (int rowIndex = 0; rowIndex <= st.getLastRowNum(); rowIndex++) { HSSFRow row = st.getRow(rowIndex); if (row == null) { continue; } UcstarYingjiSend poom = new UcstarYingjiSend(); poom.setIncidentUri(_incidentUri); for (short columnIndex = 0; columnIndex <= row .getLastCellNum(); columnIndex++) { String value = ""; cell = row.getCell(columnIndex); if (cell != null) { // 注意:一定要设成这个,否则可能会出现乱码 cell.setEncoding(HSSFCell.ENCODING_UTF_16); switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: value = cell.getStringCellValue(); break; // 公式类型转换 case HSSFCell.CELL_TYPE_FORMULA: value = new DecimalFormat("0").format(cell .getNumericCellValue()); break; case HSSFCell.CELL_TYPE_NUMERIC: if (HSSFDateUtil.isCellDateFormatted(cell)) { value = new java.text.SimpleDateFormat( "HH:mm").format(cell .getDateCellValue()); } else { value = new DecimalFormat("0") .format(cell .getNumericCellValue()); } break; } } if (rowIndex != 0 && rowIndex != 1) { if (columnIndex == 0) { if (JiveGlobals.isEmpty(value) || !value.matches("[0-9]+")) { continue; } poom.setTaskId(value); } if (columnIndex == 1) { if (JiveGlobals.isEmpty(value)) { continue; } // poom.setCreatetime(value); poom.setCreatetime("00:00"); } if (columnIndex == 2) { if ("否".equals(value)) { poom.setSameTime(0); } else if ("是".equals(value)) { poom.setSameTime(1); } } if (columnIndex == 3) { poom.setScriptstr(value); } if (columnIndex == 4) { poom.setCreator(value); } if (columnIndex == 5) { poom.setReceivers(value); } if (columnIndex == 6) { poom.setDetailTime(value); } if (columnIndex == 7) { poom.setDetail(value); } if (columnIndex == 8) { poom.setTitle(value); } if (columnIndex == 9) { poom.setMemo(value); } } } // 需要判断字段的值是否都为空,等 其他验证条件 if (!JiveGlobals.isEmpty(poom.getTaskId()) && !JiveGlobals.isEmpty(poom.getReceivers()) && !JiveGlobals.isEmpty(poom.getCreatetime())) { fileto.add(poom); } else { Log.console(">>演练脚本导入数据有误,行:" + rowIndex); } } } if (in != null) { try { in.close(); } catch (IOException ioe) { } } if (fileto.size() > 0 && UcstarYingjiSendServiceUtil.getFactory() .getUcstarYingjiSendService().saveScriptDatas( fileto, _incidentUri)) { flag = true; } else { Log.error(">>202读取演练脚本数据为空"); } } else { //文件不存在提示用户 Log.error(">>203演练脚本不存在"); } } catch (Exception e) { Log.error(">>应急演练Excel文件数据异常:" + e); } return flag; } }
//2007处理
// 2007版本 public boolean exportExcelDate2007(String _fileName, String _incidentUri) { boolean flag = false; if (JiveGlobals.isEmpty(_fileName) || JiveGlobals.isEmpty(_incidentUri)) { return flag; } try { String savePath = YingjiyanlianBaseManager.getInstance() .getUploadPath(); // String _fileNamm = _fileName +"x"; File saveFile = new File(savePath, _fileName); if (saveFile.exists()) { String sb250 = saveFile.toString(); // OPCPackage s1 = OPCPackage.open(in) // InputStream in = new BufferedInputStream(new // FileInputStream(saveFile)); List<UcstarYingjiSend> fileto = new ArrayList<UcstarYingjiSend>(); // XSSFWorkbook wb = new XSSFWorkbook(s1); XSSFWorkbook wb = new XSSFWorkbook(sb250); // wb = new XSSFWorkbook(); // sheet循环 for (int numSheet = 0; numSheet < wb.getNumberOfSheets(); numSheet++) { // 第一张表的值 XSSFSheet st = wb.getSheetAt(numSheet); // 获取第二行以下的信息 for (int rowNum = 0; rowNum <= st.getLastRowNum(); rowNum++) { XSSFRow row = st.getRow(rowNum); if (row == null) { continue; } UcstarYingjiSend poom = new UcstarYingjiSend(); poom.setIncidentUri(_incidentUri); for (short cellNum = 0; cellNum <= row.getLastCellNum(); cellNum++) { String value = ""; XSSFCell cell = row.getCell(cellNum); if (cell != null) { // 注意:一定要设成这个,否则可能会出现乱码 switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: value = cell.getStringCellValue(); break; // 公式类型转换 case Cell.CELL_TYPE_FORMULA: value = new DecimalFormat("0").format(cell .getNumericCellValue()); break; case Cell.CELL_TYPE_NUMERIC: /* * if (DateUtil.isD(cell)){ value = new * java.text.SimpleDateFormat("HH:mm").format(cell.getDateCellValue()); } * else { value = new * DecimalFormat("0").format(cell.getNumericCellValue()); } */ // String ss = // cell.getCellStyle().getDataFormatString(); value = new java.text.SimpleDateFormat( "HH:mm").format(cell .getDateCellValue()); break; } } if (rowNum != 0 && rowNum != 1) { if (cellNum == 0) { if (JiveGlobals.isEmpty(value) || !value.matches("[0-9]+")) { continue; } poom.setTaskId(value); } if (cellNum == 1) { if (JiveGlobals.isEmpty(value)) { continue; } // poom.setCreatetime(value); poom.setCreatetime("00:00"); } if (cellNum == 2) { if ("否".equals(value)) { poom.setSameTime(0); } else if ("是".equals(value)) { poom.setSameTime(1); } } if (cellNum == 3) { poom.setScriptstr(value); } if (cellNum == 4) { poom.setCreator(value); } if (cellNum == 5) { poom.setReceivers(value); } if (cellNum == 6) { poom.setDetailTime(value); } if (cellNum == 7) { poom.setDetail(value); } if (cellNum == 8) { poom.setTitle(value); } if (cellNum == 9) { poom.setMemo(value); } } } // 需要判断字段的值是否都为空,等 其他验证条件 if (!JiveGlobals.isEmpty(poom.getTaskId()) && !JiveGlobals.isEmpty(poom.getReceivers()) && !JiveGlobals.isEmpty(poom.getCreatetime())) { fileto.add(poom); } else { Log.console(">>演练脚本导入数据有误,行:" + rowNum); } } } /* * if(in != null){ try{ in.close(); }catch(IOException ioe){ } } */ if (fileto.size() > 0 && UcstarYingjiSendServiceUtil.getFactory().getUcstarYingjiSendService().saveScriptDatas(fileto, _incidentUri)) { flag = true; } else { Log.error(">>202读取演练脚本数据为空"); } } else { // 文件不存在提示用户 Log.error(">>203演练脚本不存在"); } } catch (Exception e) { Log.error(">>应急演练Excel文件数据异常:" + e); } return flag; }