一、导包
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
不用Maven的,可以到这里下载所需的jar包:https://download.csdn.net/download/chenchuwai/10786910
二、代码
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class parseExcel {
/*
* 新建一个Maven的java工程 。在https://mvnrepository.com/中输入poi、poi-ooxml、commons-io、dom4j,再
* 挑一个多人用的版本,将其Maven依赖拷贝到自己工程的pom.xml中,完成引用相关的jar包
*/
//将excel的数据转换成xml格式
public static void generateXml(final String excelPath, final String xmlPath) throws Exception {
// 格式化输出
final OutputFormat format = OutputFormat.createPrettyPrint();
// 指定XML编码
format.setEncoding("UTF-8");
// 用于指定显示和编码方式
final XMLWriter output = new XMLWriter(new FileWriter(xmlPath), format);
// 定义一个XML文档对象
final Document document = DocumentHelper.createDocument();
// 获取根节点
Element root = document.getRootElement();
// 获取excel文件
final File tempFile = new File(excelPath.trim());
// 获取带后缀的文件名
final String fileName = tempFile.getName();
// 获取后缀,例如.xlsx
final String prefix = fileName.substring(fileName.lastIndexOf("."));
// 获取后面部分的长度
final int num = prefix.length();
// 获取去掉了后缀的文件名
final String fileOtherName = fileName.substring(0, fileName.length() - num);
// 创建根节点
if (root == null) {
root = document.addElement(fileOtherName);
root.addAttribute("position", fileName);
}
// 利用工厂读取excel可以不用考虑后缀是.xls还是.xlsx
final Workbook wb = WorkbookFactory.create(new File(excelPath));
// 获取sheet页的数量
final int sheetNum = wb.getNumberOfSheets();
// 循环读取每一个页sheet的内容start
for (int i = 0; i < sheetNum; i++) {
// 读取某一页sheet
final Sheet sheet = wb.getSheetAt(i);
// 标记是否接下来的是否为FieldIdLabel(数据行是否为属性名)
boolean isFieldIdLabel = false;
// 标记是否接下来的是否为FieldValue(数据行是否为值)
boolean isFieldValue = false;
// 每一行具有数据值的列数量
int coloumNum = 0;
// 定义一个集合存放FieldIdLabel
final List<String> fields = new ArrayList<String>();
// 获取每一页sheet底下Tab的名字
final String sheetName = sheet.getSheetName();
// 定义prePosition,拼接存储位置
final String prePosition = new String(fileName + "," + sheetName);
// 添加一级节点
final Element firstElm = root.addElement("sheet");
firstElm.addAttribute("id", sheetName);
firstElm.addAttribute("position", prePosition.toString());
// 定义二级节点
Element secondElm = null;
// 定义三级节点
Element thirdElm = null;
// 循环读取每一行的内容start
for (final Row row : sheet) {
// 获取每一行具有可读数据值的列数量
coloumNum = row.getPhysicalNumberOfCells();
// 行数
final String rowNum = String.valueOf(row.getRowNum() + 1);
// 定义四级节点
Element fourthElm = null;
// 标志是否接下来row的FieldValue是数据行
boolean isNextRow = true;
// 循环读取每一列的值start
for (final Cell cell : row) {
// 将单元格的内容转换成字符串
final String cellStr = cellValueToString(cell);
// 单元格的列索引
final int cellIndex = cell.getColumnIndex();
// 各种不同的情况start
if (cellStr.startsWith("##")) {
final String cellElm = cellStr.substring(2);
// 添加二级节点
secondElm = firstElm.addElement(cellElm);
secondElm.addAttribute("position", prePosition + "," + rowNum);
} else if (cellStr.startsWith("#begin")) {
// 添加三级节点
thirdElm = secondElm.addElement("elements");
final String[] arrayStr = cellStr.split(":");
if (arrayStr.length == 1) {
thirdElm.addAttribute("id", "default");
} else {
thirdElm.addAttribute("id", arrayStr[1]);
}
isFieldIdLabel = true;
} else if (isFieldIdLabel) {
if (!cellStr.isEmpty()) {
if (coloumNum != 0) {
fields.add(cellStr);
coloumNum -= 1;
}
} else {// 如果为空
if (coloumNum != 0) {
coloumNum -= 1;
}
}
if (coloumNum == 0) {
isFieldIdLabel = false;
isFieldValue = true;
}
} else if (cellStr.startsWith("#end")) {
isFieldValue = false;
fields.clear();
} else if (isFieldValue) {
// 循环读取每一行数据
if (isNextRow) {
// 添加四级节点
fourthElm = thirdElm.addElement("element");
fourthElm.addAttribute("position", prePosition + "," + rowNum);
// 添加五级节点
final Element fifthElm = fourthElm.addElement(fields.get(cellIndex));
fifthElm.setText(cellStr);
isNextRow = false;
} else {
// 继续添加五级节点
if (cellIndex < fields.size()) {
final Element fifthElm = fourthElm.addElement(fields.get(cellIndex));
fifthElm.setText(cellStr);
}
}
}
}
}
}
System.out.println("Excel success into XML file!");
output.write(document);
output.flush();
output.close();
}
// 将单元格的内容全部转换成字符串
private static String cellValueToString(final Cell cell) {
String str = "";
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
str = cell.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
str = cell.getDateCellValue().toString();
} else {
str = String.valueOf(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
str = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
str = cell.getCellFormula();
break;
default:
str = cell.getRichStringCellValue().getString();
break;
}
return str;
}
//主函数
public static void main(final String[] args) throws Exception {
// 将Excel文件转换成XML文件
generateXml("D://TestPOI//system.xlsx", "D://TestPOI//system.xml");
}
}