里面用到的 MyXMindUtils工具类 和 XMindNodeModel 看: https://blog.csdn.net/lgz0921/article/details/115111569?spm=1001.2014.3001.5501
pom.xml:
<!--Excel处理包-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
utils :
public class MyExcelUtils {
public static List<List<XMindNodeModel>> xMindNodeModelLists = new ArrayList<>();
public static XSSFWorkbook wb = new XSSFWorkbook();
public static XSSFSheet sheet = wb.createSheet("1");
/**
* 转换接口
*/
public static void xMindToExcel(File file) {
//获得二维数组xMindNodeModelLists
getXMindNodeModelList(MyXMindUtils.readXMind(file), new LinkedList<>());
writerExcel(xMindNodeModelLists, file.getName().replace(".xmind",".xlsx"));
}
/**
* 输出Excel
* */
public static void writerExcel(List<List<XMindNodeModel>> xMindNodeModelLists, String fileName) {
int maxLen = xMindNodeModelLists.stream().mapToInt(List::size).max().getAsInt();
Map<String, Boolean> map = new HashMap<>();
try {
XSSFCellStyle style = getXSSFCellStyle();
for (int i = 0; i < xMindNodeModelLists.size(); i++) {
Row row = sheet.createRow(i);
List<XMindNodeModel> list = xMindNodeModelLists.get(i);
for (int j = 0; j < maxLen; j++) {
Cell cell = row.createCell(j);
if (j < list.size()) {
String content = list.get(j).getContent();
cell.setCellValue(content);
if (map.containsKey(content)) {
cell.setCellValue("");
} else {
map.put(content, true);
}
} else {
cell.setCellValue("");
}
cell.setCellStyle(style);
}
}
FileOutputStream outputStream = new FileOutputStream(new File("C:\\Users\\WEIMOB\\Desktop\\" + fileName));
wb.write(outputStream);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 设置字体
* */
public static XSSFFont getXSSFFont(){
//设置字体
XSSFFont font = wb.createFont();
font.setFontName("微软雅黑");
font.setFontHeightInPoints((short) 9);// 设置字体大小
//加粗
//font.setBold(true);
return font;
}
/**
* 设置单元格样式
* */
public static XSSFCellStyle getXSSFCellStyle(){
XSSFCellStyle style = wb.createCellStyle();
//下边框
style.setBorderBottom(BorderStyle.THIN);
//左边框
style.setBorderLeft(BorderStyle.THIN);
//上边框
style.setBorderTop(BorderStyle.THIN);
//右边框
style.setBorderRight(BorderStyle.THIN);
//自动换行
style.setWrapText(true);
//水平居中
style.setAlignment(HorizontalAlignment.CENTER);
//上下居中
style.setVerticalAlignment(VerticalAlignment.CENTER);
//设置字体
style.setFont(getXSSFFont());
return style;
}
/**
* 得到xMindNodeModelLists
*/
public static void getXMindNodeModelList(Object data, List<XMindNodeModel> xMindNodeModels) {
XMindNodeModel xMindNodeModel = new XMindNodeModel();
JSONObject jsonb = JSON.parseObject(String.valueOf(data));
JSONArray jsonArray = null;
for (Object obj : jsonb.keySet()) {
String key = String.valueOf(obj);
String value = String.valueOf(jsonb.get(obj));
if ("children".equals(key)) {
jsonArray = JSON.parseArray(value);
} else {
fillXMindNodeModel(xMindNodeModel, key, value);
}
}
if (xMindNodeModel.getContent() != null) {
xMindNodeModel.setContent(xMindNodeModel.getContent().replaceAll("\r", " "));
}
xMindNodeModels.add(xMindNodeModel);
if (jsonArray == null || jsonArray.size() == 0) {
xMindNodeModelLists.add(xMindNodeModels);
} else {
for (Object o : jsonArray) {
List<XMindNodeModel> xMindNodeModelsClone = depCopy(xMindNodeModels);
getXMindNodeModelList(o, xMindNodeModelsClone);
}
xMindNodeModels.remove(xMindNodeModels.size() - 1);
}
}
/**
* 填充节点
*/
public static void fillXMindNodeModel(XMindNodeModel xMindNodeModel, String key, String value) {
if (key.equals(XMindNodeModel.ColumnEnum.content.name())) {
xMindNodeModel.setContent(value);
} else if (key.equals(XMindNodeModel.ColumnEnum.level.name())) {
xMindNodeModel.setLevel(Integer.valueOf(value));
}
}
/***
* 对集合进行深拷贝 注意需要对泛型类进行序列化(实现Serializable)
*
* @param srcList
* @param <T>
* @return
*/
public static <T> List<T> depCopy(List<T> srcList) {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
try {
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(srcList);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream inStream = new ObjectInputStream(byteIn);
List<T> destList = (List<T>) inStream.readObject();
return destList;
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
使用方式:
@SpringBootTest
public class ExcelTests {
@Test
public void xMindToExcel() {
File file = new File("C:\\Users\\Desktop\\haha.xmind");
MyExcelUtils.xMindToExcel(file);
}
}