利用POI將Excel轉化成XML檔案
一、導包
<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>
二、程式碼
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");
}
}