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

时间:2023-03-09 01:12:39
【java工具类】java做的一个xml转Excel工具,基于maven工程

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

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

doc4j的maven依赖

 <!--xml解析的dom4j-->
<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</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 main(String[] args) {
Boolean aBoolean = Xml2ExcelFile("C:\\Users\\Administrator\\Desktop\\test.xml", "C:\\Users\\Administrator\\Desktop\\test2.xlsx");
System.out.println(aBoolean);
} /**
* Xml转Excel文件
* @param strXmlFilePath Xml文件路径
* @param strExcleFilePath Excel文件存放路径
* @return
*/
public static Boolean Xml2ExcelFile(String strXmlFilePath, String strExcleFilePath) {
if(StringUtil.isTrimNull(strXmlFilePath) || StringUtil.isTrimNull(strExcleFilePath)) {
return false;
} List<List<String>> listList = readXml(strXmlFilePath);
if(null == listList || listList.isEmpty()) {
return false;
} return ExcelUtil.createExcelFile(strExcleFilePath, listList);
} /**
* 读取Xml文件,以List<List<String>>形式返回
* @param strFile
* @return
*/
public static List<List<String>> readXml(String strFile) {
if(StringUtil.isTrimNull(strFile)) {
return null;
} SAXReader reader = new SAXReader();
File file = new File(strFile);
if(!file.exists() || !file.isFile()) {
return null;
} Document document = null;
try {
document = reader.read(file);
} 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.add(titleList);
boolean flag = true;
for (Element child : childElements) {
List<String> list = new ArrayList<>(); List<Element> elementList = child.elements();
for (Element ele : elementList) {
if(flag) {
titleList.add(ele.getName());
}
list.add(ele.getText());
}
flag = false; listList.add(list); System.out.println();
}
} return listList;
}
}

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

StringUtil.isTrimNull

 public static boolean isTrimNull(Object obj) {
if (null != obj) {
return isNull(obj.toString().trim());
} else {
return true;
}
}

Excel.createExcelFile

 public static <T> Boolean createExcelFile(String fileName, List<List<T>> list) {
logger.debug(">>>>>" + CLASSNAME + ".createExcelFile()...");
logger.debug("fileName: " + fileName);
logger.debug("list: " + list); FileOutputStream fileOut = null;
try {
if(StringUtil.isTrimNull(fileName)) {
return false;
} File file = new File(fileName);
if(file.exists()) {
System.out.println("文件已存在!");
logger.debug("文件已存在!");
return false;
} Workbook workbook = null;
String excelType = null;
if(FileUtil.isFileNameEndsWith(fileName, ".xls")) {
excelType = "xls";
workbook = new HSSFWorkbook();
} else if(FileUtil.isFileNameEndsWith(fileName, ".xlsx")) {
excelType = "xlsx";
workbook = new XSSFWorkbook();
} else {
return false;
} fileOut = new FileOutputStream(fileName);
workbook.write(fileOut);
fileOut.close(); FileInputStream is = new FileInputStream(file);
if("xls".equals(excelType)) {
POIFSFileSystem fs = new POIFSFileSystem(is);
workbook = new HSSFWorkbook(fs);
} else if("xlsx".equals(excelType)) {
workbook = new XSSFWorkbook(is);
} Sheet sheet = workbook.createSheet();
sheet = setSheetValue(sheet , list); fileOut = new FileOutputStream(fileName);
workbook.write(fileOut);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(fileOut != null) {
try {
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
logger.debug("<<<<<" + CLASSNAME + ".createExcelFile().");
return true;
}
}