导出xml,excel文件,导入excel文件
来源:互联网 发布:哪些数据库是免费的 编辑:程序博客网 时间:2024/05/16 09:38
/** 导出自定义的xml文件**/ Element root=new Element("CHECKREPORT");//创建根节点 Document Doc=new Document(root);//将根节点添加到文档中 //GlobalConfig节点 Element elements1=new Element("GlobalConfig"); elements1.addContent(new Element("TaskTargetName").setText(organizeName)); elements1.addContent(new Element("TaskTargetDepartment").setText("")); root.addContent(elements1); Format format=Format.getPrettyFormat();//格式化xml文件,缩进效果 XMLOutputter XMLOut=new XMLOutputter(format); HttpServletResponse response = getResponse(".xml",organizeName+"检查系统数据结果报告"); try { OutputStream os = response.getOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLOut.output(Doc, baos); os.write(baos.toByteArray()); baos.flush(); baos.close(); os.close(); } catch (IOException e) { e.printStackTrace(); }///////导出excel文件//导出excel组件public void exportXLS() { List<List<? extends Object>> splitList = getSplitList();//List<? extends Object> 泛型是Object 或者继承Object的类
HttpServletResponse response = getResponse(".xls"); try { OutputStream os = response.getOutputStream(); int i = 1; wb = new HSSFWorkbook(); for (List<? extends Object> smallList : splitList) { this.createExcel(smallList, i++); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); wb.write(baos); os.write(baos.toByteArray()); baos.flush(); baos.close(); os.close(); } catch (IOException e) { e.printStackTrace(); } }public void exportZIP() { List<List<? extends Object>> splitList = getSplitList(); HttpServletResponse response = getResponse(".zip"); try { OutputStream os = response.getOutputStream(); ZipArchiveOutputStream zout = new ZipArchiveOutputStream(os); if (splitList.isEmpty()) { zout.close(); os.close(); return; } zout.setEncoding("GBK"); int i = 1; try { for (List<? extends Object> smallList : splitList) { wb = new HSSFWorkbook(); this.createExcel(smallList, i); ByteArrayOutputStream baos = new ByteArrayOutputStream(); wb.write(baos); baos.flush(); ZipArchiveEntry entry = new ZipArchiveEntry(sheetName + (i++) + ".xls"); zout.putArchiveEntry(entry); zout.write(baos.toByteArray()); baos.close(); } } catch (IOException e) { e.printStackTrace(); } finally { zout.closeArchiveEntry(); zout.close(); os.close(); } } catch (IOException e) { e.printStackTrace(); } }//导出excel方法public void exportExcelFun(){ String[] cellNames = null; String[] orderedAttrs = null; String downLoadFileName = ""; //导出文件名 groupInput.setPageSize(100000); CustomResultSet crs = new CustomResultSet(); crs = checkConfigDaoImpl.listDept(groupInput); // 表的列名 cellNames = new String[]{ "部门ID" , "部门名称" , "部门父节点ID" }; orderedAttrs = new String[]{ "groupId", "groupName", "parentId"}; downLoadFileName = "部门列表"; // 调用导出接口 List list = crs.getList(); ExcelFacts excel = new ExcelFacts.Builder(cellNames, orderedAttrs) .sheetName(downLoadFileName).dataList(list) .build(); // 如果记录条数大于10000,我们要循环去追加 int totalCount = crs.getTotalCount(); int pageCount = (totalCount + 10000 - 1) / 10000;// 需要循环的次数 for (int i = 1; i < pageCount; i++) {// 循环取 追加的list,下标从1开始,前10000条第一次已经追加过。 groupInput.setPageIndex(i); crs = checkConfigDaoImpl.listDept(groupInput); list = crs.getList(); excel.addData(list); } excel.exportXLS();}//////导入excel文件 String sta=""; String type=""; int count=0; // 统计 成功个数 int countf=0; // 统计 格式 个数 int counts=0; // 统计 失败 个数 String content = "<div id='importNum'>" ; List<GroupInput> daoAll=new ArrayList<GroupInput>();//将导入成功的数据存放在集合中 int allrow=0; Workbook workbook = null; try { InputStream is=new FileInputStream(file); String fileType=groupInput.getFileType(); if(fileType.equals("xls")){ workbook = new HSSFWorkbook(is);//支持excel2003解析 }else if(fileType.equals("xlsx")){ workbook=new XSSFWorkbook(is);//支持excel2007解析 } } catch (Exception e) { e.printStackTrace(); } int sheetNum = workbook.getNumberOfSheets();//获得总共的表单数; for (int i = 0; i < sheetNum; i++) { Sheet sheet = workbook.getSheetAt(i);//获得第一个表单 int sheetRows =sheet.getPhysicalNumberOfRows();//获得当前表单的总行数 if(sheetRows<=1){ content = "<div id='importNum'>您导入的文件格式有误请查看</div>"; } else{ //处理异常 try { for (int j = 1; j < sheetRows; j++) {//遍历每一行数据;由于第一行是标题不需要入库 allrow++; Row row = sheet.getRow(j);//获得对象 if(null!=row){ int sheetCells = row.getPhysicalNumberOfCells();//每一行的列数 int groupId =0; String name = ""; int parentId=0; try{ if(sheetCells>2){ //备注存在 groupId = Integer.parseInt(row.getCell(0).getStringCellValue()); name = row.getCell(1).getStringCellValue(); parentId = Integer.parseInt(row.getCell(2).getStringCellValue()); } } catch (Exception e) { countf ++; } GroupInput gp = new GroupInput(); gp.setGroupId(groupId); gp.setName(name); gp.setParentId(parentId); daoAll.add(gp); } } } catch (Exception e) { countf++; content = "<div id='importNum'>您导入的文件格式有误请查看</div>"; } } } if(countf==0){ if(daoAll.size()<=0){ content = "<div id='importNum'>您导入的文件格式有误请查看</div>"; } /* else if(daoAll.size()>50){ content = "<div id='importNum'>导入的数量不可以大于50条</div>"; } */ else{ // 将导入成功 数据 取出 存入数据库 int cs=0;// 统计放回0 的个数--导入失败的个数 int cb=0;// 统计放回不为0 的个数--导入成功的个数 for (GroupInput all:daoAll) { if(all!=null){ try { /*int isExist=checkConfigDaoImpl.existsCheckUsbDiskManageDao(all); if(isExist==-1){*/ int flag= checkConfigDaoImpl.importDept(all); if(flag==0){ cs++; }else{ cb++; } /*}else{ cs++; }*/ } catch (Exception e) { cs++; } }else{ cs++; } } if(cb==0){ content = "<div id='importNum'>导入失败"+cs+"条</div>"; type="导入失败"; sta="失败"; }else{ if(cs==0){ content = "<div id='importNum'>导入成功"+cb+"条</div>"; type="导入成功"+cb+"条数据"; sta="成功"; }else{ content = "<div id='importNum'>导入成功"+cb+"条 ,导入失败"+cs+"条</div>"; type="导入成功"+cb+"条数据--导入失败"+cs+"条数据"; sta="失败"; } } } }else{ content = "<div id='importNum'>您导入的文件格式有误请查看</div>"; } //获取部门列表的json字段 String json=""; json=getDeptListCommon(groupInput); content=content+"<div id='deptImportjson'>"+json+"</div>"; getResponse().setContentType("text/html");// 服务器告诉浏览器响应内容以text/html的方式打开 getResponse().getWriter().write(content);// js获得后台返回的值
0 0
- 导出xml,excel文件,导入excel文件
- 导入导出Excel文件
- 导入导出Excel文件
- 导入导出excel文件
- 导入导出Excel文件
- datatable导入导出excel文件
- 关于导入 导出Excel文件
- excel文件的导入导出
- PHP导出文件,导入Excel
- Excel文件导入导出(1)
- java Excel文件导入导出
- Excel文件导入和导出
- struts2导入&导出Excel文件
- php导出导入excel 文件
- sql导入导出 excel文件
- PHPExcel导入导出Excel文件
- mediawiki 导入导出Excel文件
- phpexcel导出、导入excel文件
- C++IO之文件操作
- 8.JavaWeb基础 DAO
- (java 功能篇)之 java时间工具类
- Crittercism: 在MongoDB上实现每天数十亿次请求
- Trace、Debug和TraceSource的使用以及日志设计
- 导出xml,excel文件,导入excel文件
- android知识点:duplicateParentState属性
- android生命周期详解
- 导入一个Android 项目后,注释中文乱码
- Oracle错误表
- 方面的饱和度舒服了看电视费
- 获取最新Google可用IP方法(for hosts)
- 协议和分类
- Hibernate Helloworld教程