v6之数据导出

1.效果图

 

2.实现

  2.1 前端

 

 

 2.2 后端

 

/** 
* 数据导入
* @param req
* @param rep
* @param errorHandler
* @param messageHandler
* @param viewHelper
* @return
* @throws IOException
*/
public String importdata(HttpServletRequest req, HttpServletResponse rep, IErrorHandler errorHandler, IMessageHandler messageHandler, ViewHelper viewHelper) throws IOException {
if (log.isDebugEnabled()) {
log.debug("TaskImportCmd.importdata---begin");
}
Map<String, Object> preMap = new HashMap<String, Object>();
Map<String, Object> viewin = (HashMap<String, Object>)viewHelper.getView();

// 首先判断导出的目录是否存在
String filepath = req.getSession().getServletContext().getRealPath("/jsp/com/v6/screen/pea/work/taskimport/temFile/");
File dir = new File(filepath);
if (!dir.isDirectory()) {
if (!dir.exists()) {
dir.mkdirs();
}
}

// 获取上传文件路径名称
UploadFile uploadFile = (UploadFile) viewin.get("uploadFile");
String fileName = "";
if (uploadFile != null) {
fileName = processFileName(uploadFile.getFileName());
// 判断文件是否存在
File file = new File(filepath + fileName);
if (file.exists()) {// 如果同名文件已经存在
// 删除已有文件
file.delete();
}

// 创建该文件
file.createNewFile();
FileOutputStream out = new FileOutputStream(file, true);
out.write(uploadFile.getFileData());
out.flush();
out.close();
Workbook hwk = null;
InputStream is = null;
try {
is = new FileInputStream(file);
try {
hwk = new HSSFWorkbook(is);
} catch (Exception ex) {
hwk = new XSSFWorkbook(is);
}
Sheet sh = hwk.getSheetAt(0);// 得到book第一个工作薄sheet

int rows = sh.getLastRowNum() + 1 - sh.getFirstRowNum(); // 总行数
if (log.isDebugEnabled()) {
log.debug("TaskImportCmd.importdata---rows="+rows);
}
List importList = new ArrayList();//Excel表格数据
for (int i = 1; i < rows; i++) {
Row row = sh.getRow(i);
if (row == null || row.getCell(0) == null || "".equals(row.getCell(0))
|| row.getCell(1) == null || "".equals(row.getCell(1))
|| row.getCell(2) == null || "".equals(row.getCell(2))) {
log.debug("第" + (i + 1) + "行信息填写不完整!");
throw new RuntimeException("#异常原因#导入失败:第" + (i + 1) + "行信息填写不完整!");
}

String taskName = cellIsNul(row.getCell(0));
Map<String, Object> ipColMap = new HashMap<String, Object>();
String taskTypeName = cellIsNul(row.getCell(1));
String taskType = "";
if("重点任务".equals(taskTypeName)){
taskType = "01";
}else if("专项任务".equals(taskTypeName)){
taskType = "02";
}else if("日常任务".equals(taskTypeName)){
taskType = "03";
}
String importDepts = cellIsNul(row.getCell(2));

String taskDesc = cellIsNul(row.getCell(3));
ipColMap.put("TASK_NAME", taskName);
ipColMap.put("TASK_TYPE", taskType);
ipColMap.put("IMPORT_DEPTS", importDepts);
ipColMap.put("TASK_DESC", taskDesc);
importList.add(ipColMap);
}
preMap.put("importList", importList);
getTaskImportService().importdata(preMap);
if (log.isDebugEnabled()) {
log.debug("TaskImportCmd.importdata---end");
}
req.setAttribute("OP_FLAG", "T");
req.setAttribute("OP_MSG", "任务导入成功!");
rep.flushBuffer();

} catch (Exception e) {
req.setAttribute("OP_FLAG", "F");

String info = e.toString();
String[] infos = info.split("#异常原因#");
if(infos.length>= 2){
if (log.isDebugEnabled()) {
log.debug("TaskImportCmd.importdata---catch info="+infos[1]);
}
req.setAttribute("OP_MSG", infos[1]);
}
if (log.isDebugEnabled()) {
log.debug("导入异常--", e);
}
} finally {
if (is != null)
is.close();
}
}
return null;
}
posted @ 2023-02-27 15:05  码农在广场  阅读(21)  评论(0编辑  收藏  举报