导出xml,excel文件,导入excel文件
2014-08-12 17:03
141 查看
/** 导出自定义的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获得后台返回的值
相关文章推荐
- Excel文件导入导出实战(3)-xml模板文件的上传下载
- DataTable 对xml、excel、csv 对文件的导入和导出功能
- 上传excel文件实现批量导入数据及导出数据为excel文件
- VC++ 下使用 xml 文件导入导出数据
- Asp.Net下导出/导入规则的Excel(.xls)文件
- asp.net导出/导入规则的Excel(.xls)文件
- XML文件导入到DATASET并且增加数据后导出为XML
- SQL 数据的导入导出,对远程(MSsql,OracleAccess,)数据库的操作以及读取Excel,txt文件中的数据
- 解决 在Oracle表数据和xml文件间相互导入导出的PL/SQL 过程
- Asp.Net下导出/导入规则的Excel(.xls)文件
- [导入]ASP.NET 2.0中将 GridView 导出到 Excel 文件中
- Excel文件的导出与导入
- 用toad导入和导出excel(.xls)文件(表的数据)
- GridView导出为Excel后,导出的.xls文件无法作为源文件导入的问题
- php数据导入导出之excel(csv文件)
- php数据导入导出之excel(csv文件)
- 导出查询中的数据到Excel,包含字段名,文件为真正的Excel文件
- 导出表中的数据到Excel,包含字段名,文件为真正的Excel文件
- php数据导入导出之excel(csv文件)
- JFileChooser实现文件导出导入到Excel