excel处理

 

  1 package com.xxx.xxx.xxx.excel;
  2 
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.io.FileOutputStream;
  6 import java.io.IOException;
  7 import java.io.InputStream;
  8 import java.io.OutputStreamWriter;
  9 import java.io.StringWriter;
 10 import java.util.ArrayList;
 11 import java.util.Iterator;
 12 import java.util.List;
 13 
 14 import org.dom4j.Attribute;
 15 import org.dom4j.Document;
 16 import org.dom4j.DocumentHelper;
 17 import org.dom4j.Element;
 18 import org.dom4j.io.OutputFormat;
 19 import org.dom4j.io.SAXReader;
 20 import org.dom4j.io.XMLWriter;
 21 
 22 public class XMLHandler {
 23 
 24     /**
 25      * xml 编码格式
 26      */
 27     private static final String ENCODING = "UTF-8";
 28 
 29     /**
 30      * SAXReader
 31      */
 32     private SAXReader saxReader;
 33 
 34     /**
 35      * Document
 36      */
 37     private Document document;
 38 
 39     /**
 40      * 根节点
 41      */
 42     private Element root;
 43 
 44     /**
 45      * 记录取null的标签列表
 46      */
 47     private List<String> nullValues = new ArrayList<String>();
 48     
 49     
 50     
 51 
 52     /**
 53      * 构造函数
 54      * 直接使用saxReader.read(File file); 出现 The encoding "GBK" is not supported
 55      * @param inputXml xml文件
 56      */
 57     public XMLHandler(File inputXml) {
 58         String xml = file2String(inputXml);
 59         try {
 60             if (inputXml != null) {
 61                 document = DocumentHelper.parseText(xml);
 62                 root = document.getRootElement();
 63             }
 64         } catch (Exception e) {
 65             throw new RuntimeException("can't parse xml, please check the xml format", e);
 66         }
 67     }
 68 
 69     /**
 70      * 构造函数
 71      * @param inputXml xml文件
 72      * @param encoding xml 编码
 73      */
 74     public XMLHandler(File inputXml, String encoding) {
 75         saxReader = new SAXReader();
 76         try {
 77             if (inputXml != null) {
 78                 saxReader.setEncoding(encoding);
 79                 document = saxReader.read(inputXml);
 80                 root = document.getRootElement();
 81             }
 82         } catch (Exception e) {
 83             throw new RuntimeException("can't parse xml, please check the xml format", e);
 84         }
 85 
 86     }
 87 
 88     /**
 89      * 构造函数
 90      * 直接使用saxReader.read(String str);会出现no protocol:的异常
 91      * 使用DocumentHelper.parseText(String str);解决这一问题
 92      * @param inputXml xml字符串
 93      */
 94     public XMLHandler(String inputXml) {
 95         saxReader = new SAXReader();
 96         try {
 97             if (inputXml != null) {
 98                 document = DocumentHelper.parseText(inputXml);
 99                 root = document.getRootElement();
100             }
101         } catch (Exception e) {
102             throw new RuntimeException("can't parse xml, please check the xml format", e);
103         }
104     }
105 
106     /**
107      * 将xml的document对象转换成xml字符串
108      * @return xml字符串
109      */
110     public String doc2Xml() {
111         StringWriter writer = new StringWriter();
112         try {
113             OutputFormat format = OutputFormat.createPrettyPrint();
114             format.setEncoding(ENCODING);
115             XMLWriter output = new XMLWriter(writer, format);
116             output.write(document);
117             output.close();
118         } catch (Exception e) {
119             e.printStackTrace();
120         }
121         return writer.toString();
122     }
123 
124     /**
125      * 将xml的document对象转换成xml字符串
126      * @param encoding 字符串编码
127      * @return xml字符串
128      */
129     public String doc2Xml(String encoding) {
130         StringWriter writer = new StringWriter();
131         try {
132             OutputFormat format = OutputFormat.createPrettyPrint();
133             format.setEncoding(encoding);
134             XMLWriter output = new XMLWriter(writer, format);
135             output.write(document);
136             output.close();
137         } catch (Exception e) {
138             e.printStackTrace();
139         }
140         return writer.toString();
141     }
142 
143     /**
144      * 获取root节点
145      * @return root节点
146      */
147     public Element getRootElement() {
148         return root;
149     }
150 
151     /**
152      * 获取单个xpath路径的Text值
153      * "//article/date"
154      * @param xpath xpath路径
155      * @return Text值
156      */
157 //    @SuppressWarnings("unchecked")
158     public String getElementText(String xpath) {
159         List<?> list = document.selectNodes(xpath);
160         Iterator<?> iter = list.iterator();
161         String value = null;
162         if (iter.hasNext()) {
163             Element element = (Element) iter.next();
164             value = element.getText().trim();
165         }
166 
167         if (value == null) {
168             if (!nullValues.contains(xpath)) {
169                 nullValues.add(xpath);
170             }
171             value = "";
172         }
173         return value;
174     }
175 
176     /**
177      * 设置单个xpath路径的Text值
178      * "//article/date"
179      * "//article/date/book[n]" 取出第N个元素
180      * @param xpath xpath路径
181      * @param value Text值
182      */
183 //    @SuppressWarnings("unchecked")
184     public void setElementText(String xpath, String value) {
185         List<?> list = document.selectNodes(xpath);
186         Iterator<?> iter = list.iterator();
187         if (iter.hasNext()) {
188             Element element = (Element) iter.next();
189             element.setText(value);
190         }
191     }
192 
193     /**
194      * 获取单个xpath路径的Attribute值
195      * "//article/@date"
196      * @param xpath xpath路径
197      * @return Attribute值
198      */
199 //    @SuppressWarnings("unchecked")
200     public String getElementAttribute(String xpath) {
201         List<?> list = document.selectNodes(xpath);
202         Iterator<?> iter = list.iterator();
203         String value = null;
204         if (iter.hasNext()) {
205             Attribute attribute = (Attribute) iter.next();
206             value = attribute.getValue().trim();
207         }
208         if (value == null) {
209             if (!nullValues.contains(xpath)) {
210                 nullValues.add(xpath);
211             }
212             value = "";
213         }
214         return value;
215     }
216 
217     /**
218      * 设置单个xpath路径的Attribute值
219      * "//article/@date"
220      * "//article/date/book[n]" 取出第N个元素
221      * @param xpath xpath路径
222      * @param value Attribute值
223      */
224 //    @SuppressWarnings("unchecked")
225     public void setElementAttribute(String xpath, String value) {
226         List<?> list = document.selectNodes(xpath);
227         Iterator<?> iter = list.iterator();
228         if (iter.hasNext()) {
229             Attribute attribute = (Attribute) iter.next();
230             attribute.setValue(value);
231         }
232     }
233 
234     /**
235      * 获取xpath下的Element列表
236      * @param xpath xpath路径
237      * @return Element列表
238      */
239 //    @SuppressWarnings("unchecked")
240     public List<?> getListElement(String xpath) {
241         return document.selectNodes(xpath);
242     }
243 
244     /**
245      * 获取元素Element的xpath下的Element列表
246      * @param element 当前元素Element
247      * @param xpath xpath路径
248      * @return Element列表
249      */
250 //    @SuppressWarnings("unchecked")
251     public List<?> getListElement(Element element, String xpath) {
252         return element.selectNodes(xpath);
253     }
254 
255     /**
256      * 获取xpath下的Element的Attribute值列表
257      * @param xpath xpath路径
258      * @return Attribute值列表
259      */
260 //    @SuppressWarnings("unchecked")
261     public List<String> getListElementAttribute(String xpath) {
262         List<String> value = new ArrayList<String>();
263         List<?> list = document.selectNodes(xpath);
264         Iterator<?> iter = list.iterator();
265         while (iter.hasNext()) {
266             Attribute attribute = (Attribute) iter.next();
267             value.add(attribute.getValue().trim());
268         }
269         return value;
270     }
271 
272     /**
273      *  获取元素Element的xpath下的Element的Attribute值列表
274      * @param element 当前元素Element
275      * @param xpath xpath路径
276      * @return Attribute值列表
277      */
278 //    @SuppressWarnings("unchecked")
279     public List<String> getListElementAttribute(Element element, String xpath) {
280         List<String> value = new ArrayList<String>();
281         List<?> list = element.selectNodes(xpath);
282         Iterator<?> iter = list.iterator();
283         while (iter.hasNext()) {
284             Attribute attribute = (Attribute) iter.next();
285             value.add(attribute.getValue().trim());
286         }
287         return value;
288     }
289 
290     /**
291      * 获取xpath下的Element的Text值列表
292      * @param xpath xpath路径
293      * @return Text值列表
294      */
295 //    @SuppressWarnings("unchecked")
296     public List<String> getListElementText(String xpath) {
297         List<String> value = new ArrayList<String>();
298         List<?> list = document.selectNodes(xpath);
299         Iterator<?> iter = list.iterator();
300         while (iter.hasNext()) {
301             Element ele = (Element) iter.next();
302             value.add(ele.getText().trim());
303         }
304         return value;
305     }
306 
307     /**
308      *  获取元素Element的xpath下的Element的Text值列表
309      * @param element 当前元素Element
310      * @param xpath xpath路径
311      * @return Text值列表
312      */
313 //    @SuppressWarnings("unchecked")
314     public List<String> getListElementText(Element element, String xpath) {
315         List<String> value = new ArrayList<String>();
316         List<?> list = element.selectNodes(xpath);
317         Iterator<?> iter = list.iterator();
318         while (iter.hasNext()) {
319             Element ele = (Element) iter.next();
320             value.add(ele.getText().trim());
321         }
322         return value;
323     }
324 
325     /**
326      * 获取xpath下的单个Element
327      * @param xpath xpath路径
328      * @return 单个Element
329      */
330 //    @SuppressWarnings("unchecked")
331     public Element getElement(String xpath) {
332         List<?> list = document.selectNodes(xpath);
333         Iterator<?> iter = list.iterator();
334         if (iter.hasNext()) {
335             return (Element) iter.next();
336         }
337         return null;
338     }
339 
340     /**
341      * 获取元素Element的xpath下的单个Element
342      * @param element 当前元素Element
343      * @param xpath xpath路径
344      * @return 单个Element
345      */
346 //    @SuppressWarnings("unchecked")
347     public Element getElement(Element element, String xpath) {
348         List<?> list = element.selectNodes(xpath);
349         Iterator<?> iter = list.iterator();
350         if (iter.hasNext()) {
351             return (Element) iter.next();
352         }
353         return null;
354     }
355 
356     /**
357      *  获取元素Element的xpath下的Element的Attribute值
358      * @param element 当前元素Element
359      * @param xpath xpath路径
360      * @return Attribute值
361      */
362 //    @SuppressWarnings("unchecked")
363     public String getElementAttribute(Element element, String xpath) {
364         List<?> list = element.selectNodes(xpath);
365         Iterator<?> iter = list.iterator();
366         String value = null;
367         if (iter.hasNext()) {
368             Attribute attribute = (Attribute) iter.next();
369             value = attribute.getValue().trim();
370         }
371         if (value == null) {
372             String path = element.getUniquePath() + "/" + xpath;
373             if (!nullValues.contains(path)) {
374                 nullValues.add(path);
375             }
376             value = "";
377         }
378         return value;
379     }
380 
381     /**
382      * 设置元素Element的xpath下的Element的Attribute值
383      * @param element 当前元素Element
384      * @param xpath xpath路径
385      * @param value Attribute值
386      */
387 //    @SuppressWarnings("unchecked")
388     public void setElementAttribute(Element element, String xpath, String value) {
389         List<?> list = element.selectNodes(xpath);
390         Iterator<?> iter = list.iterator();
391         if (iter.hasNext()) {
392             Attribute attribute = (Attribute) iter.next();
393             attribute.setValue(value);
394         }
395     }
396 
397     /**
398      *  获取元素Element的xpath下的Element的Text值
399      * @param element 当前元素Element
400      * @param xpath xpath路径
401      * @return Text值
402      */
403 //    @SuppressWarnings("unchecked")
404     public String getElementText(Element element, String xpath) {
405         if (element == null) {
406             return "";
407         }
408         List<?> list = element.selectNodes(xpath);
409         Iterator<?> iter = list.iterator();
410         String value = null;
411         if (iter.hasNext()) {
412             Element ele = (Element) iter.next();
413             value = ele.getText().trim();
414         }
415         if (value == null) {
416             String path = element.getUniquePath() + "/" + xpath;
417             if (!nullValues.contains(path)) {
418                 nullValues.add(path);
419             }
420             value = "";
421         }
422         return value;
423     }
424 
425 
426     /**
427      * 设置元素Element的xpath下的Element的Text值
428      * @param element 当前元素Element
429      * @param xpath xpath路径
430      * @param value Text值
431      */
432 //    @SuppressWarnings("unchecked")
433     public void setElementText(Element element, String xpath, String value) {
434         List<?> list = element.selectNodes(xpath);
435         Iterator<?> iter = list.iterator();
436         if (iter.hasNext()) {
437             Element ele = (Element) iter.next();
438             ele.setText(value);
439         }
440     }
441 
442     /**
443      * 读取File的内容
444      * @param file 文件
445      * @return 内容
446      */
447     public static String file2String(File file) {
448         try {
449             InputStream fs = new FileInputStream(file);
450             byte[] b = new byte[(int) file.length()];
451             fs.read(b);
452             fs.close();
453             return new String(b);
454         } catch (Exception e) {
455             e.printStackTrace();
456         }
457         return null;
458     }
459 
460     /**
461      * 读取xml字符串,获取Document对象
462      * @param xml xml字符串
463      * @return Document对象
464      */
465     public static Document xml2Doc(String xml) {
466         try {
467             return DocumentHelper.parseText(xml);
468         } catch (Exception e) {
469             e.printStackTrace();
470         }
471         return null;
472     }
473 
474     /**
475      * @return List
476      */
477     public List<String> getNullValues() {
478         return nullValues;
479     }
480 
481     public void writeXmlFile(String file,String code) {
482         try {
483             OutputFormat format = OutputFormat.createPrettyPrint();
484             //format.setEncoding("UTF-8");    // 指定XML编码        
485             //XMLWriter writer = new XMLWriter(new FileWriter(file),format);
486             XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"),format); 
487             writer.write(document);
488             writer.close();
489         } catch (IOException e) {
490         
491             e.printStackTrace();
492         }        
493     }
494 }

 

posted @ 2018-01-09 09:56  hongqingfu  阅读(240)  评论(0编辑  收藏  举报