在从XML文件中读取类似的标记时,我在java中以字符串的形式获取数据。对于每个数据,数据都是管道分隔和双正斜杠(//)分隔。在可用标签中共享三个字符串,这些字符串需要以excel格式转换为分隔的选项卡。
Information - Desktop | Telecom//Apple Mac//iPhone//Working with iPhone
Infrastructure - Desktop | Telecommunication//Apple iPod//iPad
Information - Desktop | Telecom//Apple Mac//iTunes 10//Settings//Troubleshooting

请建议我如何实现循环,以得到像这样的。
发布于 2017-03-09 10:42:54
我做了这样的事。我能够以CSV格式输出数据,也许您希望使用API将其写入Excel文件。
public class StringToExcelProblem {
static String INPUT_DATA =
"Information - Desktop | Telecom//Apple Mac//iPhone//Working with iPhone\n"
+ "Infrastructure - Desktop | Telecommunication//Apple iPod//iPad\n"
+ "Information - Desktop | Telecom//Apple Mac//iTunes 10//Settings//Troubleshooting\n";
public static void main(String[] args) {
StringTokenizer st = new StringTokenizer(INPUT_DATA, "\n");
List<Block> allBlocks = new ArrayList<>();
while(st.hasMoreTokens()) {
String blockData = st.nextToken();
String[] st2 = blockData.split(" \\| ");
String parent = st2[0];
String labels = st2[1];
StringTokenizer st3 = new StringTokenizer(labels, "//");
int index = 0;
Block block = null;
while(st3.hasMoreTokens()) {
String label = st3.nextToken();
if(index == 0){
block = new Block(parent, label);
index ++;
} else {
block.add(label);
}
parent = label;
}
allBlocks.add(block);
}
for(Block b: allBlocks) {
for(RowObj r: b.rows){
System.out.println(r);
}
System.out.println();
}
}
}
class RowObj {
String label;
String value;
String parentId;
String active;
String parentTable;
@Override
public String toString() {
return label + "," + value + "," + parentId + "," + active + "," + parentTable;
}
}
class Block {
List<RowObj> rows;
public Block(String parent, String name) {
RowObj row = new RowObj();
row.label = name;
row.value = name.toLowerCase();
row.parentId = "Knowledge Base: " + parent;
row.active = "TRUE";
row.parentTable = "k_knowledge_base";
rows = new ArrayList<>();
rows.add(row);
}
public void add(String name) {
RowObj parent = rows.get(rows.size()-1);
RowObj row = new RowObj();
row.label = name;
row.value = name.toLowerCase();
row.parentId = "Knowledge Category: " + parent.label;
row.active = "TRUE";
row.parentTable = "k_category";
rows.add(row);
}
@Override
public String toString() {
return rows.toString();
}
}我能够生成以下输出。这和你的截图一样。唯一的区别是,这是一种CSV格式。您只需按需要设置格式即可。
Telecom,telecom,Knowledge Base: Information - Desktop,TRUE,k_knowledge_base
Apple Mac,apple mac,Knowledge Category: Telecom,TRUE,k_category
iPhone,iphone,Knowledge Category: Apple Mac,TRUE,k_category
Working with iPhone,working with iphone,Knowledge Category: iPhone,TRUE,k_category
Telecommunication,telecommunication,Knowledge Base: Infrastructure - Desktop,TRUE,k_knowledge_base
Apple iPod,apple ipod,Knowledge Category: Telecommunication,TRUE,k_category
iPad,ipad,Knowledge Category: Apple iPod,TRUE,k_category
Telecom,telecom,Knowledge Base: Information - Desktop,TRUE,k_knowledge_base
Apple Mac,apple mac,Knowledge Category: Telecom,TRUE,k_category
iTunes 10,itunes 10,Knowledge Category: Apple Mac,TRUE,k_category
Settings,settings,Knowledge Category: iTunes 10,TRUE,k_category
Troubleshooting,troubleshooting,Knowledge Category: Settings,TRUE,k_category希望这能有所帮助!
发布于 2017-03-09 10:22:20
采取这三个迭代步骤,以便将多分隔的、非标准的xml数据导入到MS中。
步骤1:导入数据
打开Excel→Select B2→Data→From Text图标(Get External Data组)→从“文本导入向导”窗口中选择.\yourFile.xml→Selectdelimited,请从“文本导入向导”窗口中选择next→,只选择other→Type |(用于分隔符)→击中finish命中<代码>D13
步骤2:导出余数
突出显示column C→Copy Selection→Open Notepad (notepad.exe)→Paste contents→Save记事本文件.\yourFile2.xml中的所有内容
步骤3:导入余数
Activate Excel→选择Column C→Delete列C→选择cell C2→Data→From Text图标(Get External Data组)→从“文本导入向导”窗口中选择.\yourFile.xml→Selectdelimited从next→Select other→Type / (用于分隔符)选择分隔符,将连续分隔符作为一个“命中代码”>D36D36命中<>D37。
在执行这三个步骤之后,您应该按照上面描述的方式将XML数据分开(使用这是内置的MS功能)。
如果你有任何问题,请告诉我。
https://stackoverflow.com/questions/42691545
复制相似问题