关于将xml文件格式的excel并和的工具类
来源:互联网 发布:nginx fastcgi java 编辑:程序博客网 时间:2024/06/05 18:08
将xml文件格式的excel并和的工具类
由于工作中的需求,需要将非标准的excel并和(用编辑器打开是xml形式的文件)要求将单独的文件并和显示成页签,并且保留原有的样式,页签名为文件名
我会接收一个List并最终返回一个File
ps:需要dom4j的jar包
代码块
代码块语法遵循标准markdown代码,例如:
public class ExcelMerge { /** * 将多个xls并和成一个 * @param list 传入的File集合 */ public static File listMergeFile(List<File> list){ //将第一个文件作为主文件并将里面的id按规则替换 File mainFile=list.get(0); Document mainDoc=coverXmlID(mainFile, 0); //将主文件Worksheet的name换成文件名 Element rootElement=mainDoc.getRootElement(); String mainFileName=mainFile.getName(); mainFileName=mainFileName.substring(0,mainFileName.indexOf(".")); Element rootWorksheet=rootElement.element("Worksheet"); Attribute rootWorksheetAttribute= (Attribute) rootWorksheet.attributes().get(0); rootWorksheetAttribute.setValue(mainFileName); //将集合的其它文件更换id并并和到主文件 for(int i=1;i<list.size();i++){ Document subDoc=coverXmlID(list.get(i),i); Element subRoot=subDoc.getRootElement(); mergeSubDoc(rootElement,subRoot,list.get(i)); } File file=docCoverFile(mainDoc,mainFile); return file; } /** * 将Document对象转换成File文件 * @param mainDoc 要转的document对象 * @param mainFile 目标文件对象 * @return */ private static File docCoverFile(Document mainDoc, File mainFile) { ByteArrayInputStream bis=null; FileOutputStream fos=null; try { bis=new ByteArrayInputStream(mainDoc.asXML().getBytes()); fos=new FileOutputStream(mainFile); byte[] buf=new byte[1024*1024]; int len=0; while((len=bis.read(buf))!=-1){ fos.write(buf,0,len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { bis.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } return mainFile; } /** * 子文件并和到主文件 * @param mainDoc * @param subDoc * @param file */ private static void mergeSubDoc(Element mainDoc, Element subDoc, File file) { /**1.把style样式复制进去*/ Element mainStylesNode=mainDoc.element("Styles"); List subStyleNode=subDoc.element("Styles").elements("Style"); for(int i=1;i<subStyleNode.size();i++){ Element styleNode= (Element) subStyleNode.get(i); mainStylesNode.add((Element) styleNode.clone()); } /**2.把worksheet复制进去*/ String fileName=file.getName();//获取文件名 fileName=fileName.substring(0,fileName.indexOf(".")); Element subWorksheet=subDoc.element("Worksheet"); Attribute subWordsheetAttribute= (Attribute) subWorksheet.attributes().get(0); subWordsheetAttribute.setValue(fileName); mainDoc.add((Element) subWorksheet.clone()); } /** * 转换ID值,按一定规则替换 * @param file * @param index * @return */ private static Document coverXmlID(File file,int index) { Document document=coverDocument(file); Element root = document.getRootElement(); List nodeList = root.elements(); //遍历根节点下的子节点 for(int i=0;i<nodeList.size();i++){ Element element = (Element) nodeList.get(i); //找到Styles节点 if("Styles".equalsIgnoreCase(element.getName())){ List styleNodes=element.elements(); //获取所有style节点 for(int j=0;j<styleNodes.size();j++){ Element styleNode= (Element) styleNodes.get(j); if("Style".equalsIgnoreCase(styleNode.getName())){ //获取属性值 List attrValue=styleNode.attributes(); Attribute attribute= (Attribute) attrValue.get(0); String newAttrValue="S"+index+"_"+attribute.getValue(); String oldvalue=attribute.getValue(); //替换style中的ss:id值 attribute.setValue(newAttrValue); //替换cell节点的styleid属性 replaceStyleID(root,oldvalue,newAttrValue); } } } } return document; } /** * 替换cell节点中的styleid * @param document * @param attrValue * @param newAttrValue */ private static void replaceStyleID(Element document, String attrValue, String newAttrValue) { //找到xml文件中的所有row节点 Element tablelement=document.element("Worksheet"); tablelement= (Element) tablelement.elements().get(0); List<Element> rowList=tablelement.elements("Row"); //遍历row节点中的cell节点 for(int i=0;i<rowList.size();i++){ Element rowNode=rowList.get(i); List<Element> cellNodeList=rowNode.elements("Cell"); for(int j=0;j<cellNodeList.size();j++){ Element cellNode=cellNodeList.get(j); Attribute cellAttr= (Attribute) cellNode.attributes().get(1);//获取ss:StyleID //如果和style中的id值相同,替换成新的 if(attrValue.equalsIgnoreCase(cellAttr.getValue())){ cellAttr.setValue(newAttrValue); } } } } /** * file转成dom * @param file * @return */ private static Document coverDocument(File file){ SAXReader reader=new SAXReader(); Document document=null; try { document=reader.read(file); } catch (Exception e) { e.printStackTrace(); } return document; }}
阅读全文
0 0
- 将xml文件格式的excel并和的工具类
- 关于将xml文件格式的excel并和的工具类
- Excel 2003的XML文件格式
- 关于xml文件格式的问题
- 使用POI和Dom4j将excel转换为xml格式的excel
- Repo的manifest XML文件格式
- sqlMapConfig.xml的头文件格式
- oencv-xml,yaml文件格式的写和读
- json文件格式的查询工具
- 关于excel导入的类和方法
- 关于使用SAP的BSP在后台导出excel(xml格式),并在web上弹出的方法
- Java读取以.xlsx结尾的excel文件,并写出每张表对应的c#类、java类、保存数据的xml文件、读取xml文件的工具类
- 递规法遍历xml树的属性和节点值,并将其批量放入数组
- 如何将mysql数据库的数据导出并转为.csv文件格式为UTF-8无BOM
- 上传excel文件并将里面的数据放入数据库
- 读取已经存在的excel文件并将其导出
- 关于sax解析XML文件的简单流程+工具类
- 之前写的一个XML工具类,包括将JSON对象转换为XML对象
- windbg 常用命令详解
- 菜鸟日常记载【1】
- Java正则表达式的使用
- 判断一个节点是否在一棵二叉树中&判断一颗二叉树是是否是另一颗树的子树
- Android实现附件预览
- 关于将xml文件格式的excel并和的工具类
- Swift 关键字
- 代码表示a++和++a
- Java设计模式之策略模式
- C++11 新特性之Lambda表达式
- HDU1520 Anniversary party 树形DP
- hdu1251-统计难题 字典树经典例题
- JVM学习笔记(4)-内存分配与回收策略
- Surround360 README文档——中文翻译