【java工具类】java做的一个xml转Excel工具,基于maven工程

说明:适合数据库导出为xml时转成Excel

本工具将上传至GitHub:https://github.com/xiaostudy/xiaostudyAPI3

doc4j的maven依赖

1 <!--xml解析的dom4j-->
2         <!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
3         <dependency>
4             <groupId>dom4j</groupId>
5             <artifactId>dom4j</artifactId>
6             <version>1.6.1</version>
7         </dependency>

主要代码

package com.xiaostudy.util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * XML工具类
 * @author xiaostudy
 * @date 2019.4.26
 * @version 1.0.0
 */
public class XmlUtil {

    public static void mainString[] args) {
        Boolean aBoolean = Xml2ExcelFile"C:\Users\Administrator\Desktop\test.xml", "C:\Users\Administrator\Desktop\test2.xlsx");
        System.out.printlnaBoolean);
    }

    /**
     * Xml转Excel文件
     * @param strXmlFilePath Xml文件路径
     * @param strExcleFilePath Excel文件存放路径
     * @return
     */
    public static Boolean Xml2ExcelFileString strXmlFilePath, String strExcleFilePath) {
        ifStringUtil.isTrimNullstrXmlFilePath) || StringUtil.isTrimNullstrExcleFilePath)) {
            return false;
        }

        List<List<String>> listList = readXmlstrXmlFilePath);
        ifnull == listList || listList.isEmpty)) {
            return false;
        }

        return ExcelUtil.createExcelFilestrExcleFilePath, listList);
    }

    /**
     * 读取Xml文件,以List<List<String>>形式返回
     * @param strFile
     * @return
     */
    public static List<List<String>> readXmlString strFile) {
        ifStringUtil.isTrimNullstrFile)) {
            return null;
        }

        SAXReader reader = new SAXReader);
        File file = new FilestrFile);
        if!file.exists) || !file.isFile)) {
            return null;
        }

        Document document = null;
        try {
            document = reader.readfile);
        } catch DocumentException e) {
            e.printStackTrace);
        }
        List<List<String>> listList = new ArrayList<>);

        if null != document) {
            Element root = document.getRootElement);
            List<Element> childElements = root.elements);
            List<String> titleList = new ArrayList<>);
            listList.addtitleList);
            boolean flag = true;
            for Element child : childElements) {
                List<String> list = new ArrayList<>);

                List<Element> elementList = child.elements);
                for Element ele : elementList) {
                    ifflag) {
                        titleList.addele.getName));
                    }
                    list.addele.getText));
                }
                flag = false;

                listList.addlist);

                System.out.println);
            }
        }

        return listList;
    }
}

依赖的工具类GitHub上有https://github.com/xiaostudy/xiaostudyAPI3

StringUtil.isTrimNull

1 public static boolean isTrimNullObject obj) {
2         if null != obj) {
3             return isNullobj.toString).trim));
4         } else {
5             return true;
6         }
7     }

Excel.createExcelFile

 1 public static <T> Boolean createExcelFileString fileName, List<List<T>> list) {
 2         logger.debug">>>>>" + CLASSNAME + ".createExcelFile)...");
 3         logger.debug"fileName: " + fileName);
 4         logger.debug"list: " + list);
 5 
 6         FileOutputStream fileOut = null;
 7         try {
 8             ifStringUtil.isTrimNullfileName)) {
 9                 return false;
10             }
11 
12             File file = new FilefileName);
13             iffile.exists)) {
14                 System.out.println"文件已存在!");
15                 logger.debug"文件已存在!");
16                 return false;
17             }
18 
19             Workbook workbook = null;
20             String excelType = null;
21             ifFileUtil.isFileNameEndsWithfileName, ".xls")) {
22                 excelType = "xls";
23                 workbook = new HSSFWorkbook);
24             } else ifFileUtil.isFileNameEndsWithfileName, ".xlsx")) {
25                 excelType = "xlsx";
26                 workbook = new XSSFWorkbook);
27             } else {
28                 return false;
29             }
30 
31             fileOut = new FileOutputStreamfileName);
32             workbook.writefileOut);
33             fileOut.close);
34 
35             FileInputStream is = new FileInputStreamfile);
36             if"xls".equalsexcelType)) {
37                 POIFSFileSystem fs = new POIFSFileSystemis);
38                 workbook = new HSSFWorkbookfs);
39             } else if"xlsx".equalsexcelType)) {
40                 workbook = new XSSFWorkbookis);
41             }
42 
43             Sheet sheet = workbook.createSheet);
44             sheet = setSheetValuesheet , list);
45 
46             fileOut = new FileOutputStreamfileName);
47             workbook.writefileOut);
48         } catch Exception e) {
49             e.printStackTrace);
50         } finally {
51             iffileOut != null) {
52                 try {
53                     fileOut.close);
54                 } catch IOException e) {
55                     e.printStackTrace);
56                 }
57             }
58             logger.debug"<<<<<" + CLASSNAME + ".createExcelFile).");
59             return true;
60         }
61     }

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注