import org.xmind.core.*;
import org.xmind.core.io.ByteArrayStorage;
import java.io.*;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class XmindToCSV {
private Map<String,String> cases=new LinkedHashMap<String, String>( );
/** CSV文件列分隔符 */
private static final String CSV_COLUMN_SEPARATOR = ",";
/** CSV文件行分隔符 */
private static final String CSV_ROW_SEPARATOR = "
";
/**
* @param dataList 数据集合数据
*/
public ByteArrayOutputStream doExport(Map<String,String> dataList) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
//表头部数据
String[] colNames={"用例标题","步骤","预期","用例类型","适用阶段"};
try {
StringBuffer buf = new StringBuffer();
// 完成数据csv文件的封装
// 输出列头
for (int i = 0; i < colNames.length; i++) {
buf.append(colNames[i]).append(CSV_COLUMN_SEPARATOR);
}
buf.append(CSV_ROW_SEPARATOR);
if (null != dataList) { // 输出数据
for(Map.Entry entry:dataList.entrySet ()){
//用例标题
String key=entry.getKey ().toString ();
buf.append ( """ ).append(key).append ( """ ).append(CSV_COLUMN_SEPARATOR);
//步骤
buf.append ( """ ).append(key).append ( """ ).append(CSV_COLUMN_SEPARATOR);
//预期
String value=entry.getValue ().toString ();
buf.append(""").append (value).append ( """ ).append(CSV_COLUMN_SEPARATOR);
//用例类型
buf.append("功能测试" ).append(CSV_COLUMN_SEPARATOR);
//适用阶段
buf.append("系统测试阶段" ).append(CSV_COLUMN_SEPARATOR);
buf.append(CSV_ROW_SEPARATOR);
}
}
// 写出响应
os.write(buf.toString().getBytes("GBK"));
os.flush();
os.close();
return os;
} catch (Exception e) {
e.printStackTrace();
}
return os;
}
public void xmindToCsv(String filepath){
IWorkbookBuilder builder = Core.getWorkbookBuilder();// 初始化builder
IWorkbook workbook = null;
try {
workbook = builder.loadFromFile(new File(filepath),new ByteArrayStorage(),null);// 打开XMind文件
} catch (Exception e) {
}
ISheet defSheet = workbook.getPrimarySheet();// 获取主Sheet
ITopic rootTopic = defSheet.getRootTopic(); // 获取根Topic
cases=getNodes(rootTopic);
System.out.println ( "用例个数:"+cases.size () );
// for(Map.Entry<String,String> entry:cases.entrySet ()){
// String key=entry.getKey ();
//// System.out.println("name="+entry.getKey ());
// String value=entry.getValue ();
// System.out.println("name="+key+","+"value="+value);
// }
}
//递归遍历节点
public Map<String,String> getNodes(ITopic node) {
//递归遍历当前节点所有的子节点
List<ITopic> listElement = node.getAllChildren ();
//遍历所有一级子节点
int num=1;
for (ITopic e : listElement) {
List <ITopic> ee = e.getAllChildren ();
//找到最后一个节点
if (ee.size () == 0) {
List <ITopic> path=e.getPath ().toTopicList ();
StringBuffer sb=new StringBuffer();
for(ITopic t:path) {
sb.append ( t.getTitleText () );
sb.append ( "-" );
}
// String key=(sb.substring (sb.indexOf ( "-" )+1, sb.lastIndexOf ( "-" ) ));
String key=(sb.substring (0, sb.lastIndexOf ( "-" ) ));
// key=key.substring ( 0,key.lastIndexOf ( "-" )+1 )+num;
String value=e.getTitleText ();
cases.put (key,value );
}
num++;
//递归遍历
this.getNodes ( e );
}
return cases;
}
public Map <String, String> getCases() {
return cases;
}
public void setCases(Map <String, String> cases) {
this.cases = cases;
}
public static void main(String[]args) throws IOException {
XmindToCSV test=new XmindToCSV();
String outfile="转换为csv的文件路径";
test.xmindToCsv ( "xmind文件路径");
Map<String,String> cases=test.getCases ();//获取xmind用例
ByteArrayOutputStream bos =test.doExport(cases);//导出禅道规范用例
FileOutputStream fos=null;
try {
fos=new FileOutputStream(outfile);
fos.write(bos.toByteArray());
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
}finally {
bos.close ();
fos.close ();
}
}
}