这个很简单,我粗略写了个例子。
每天把需要转换的文件,放在一个文件夹下,直接批量处理。同时也打印到控制台中。
如果觉得我的方法可以,我可以将源码发给你。
public class FileTool {
private static String mode =
"<Interaction>\n"
+ " <TransactionDate>#1#</TransactionDate>\n"
+ " <EmailAddress>#2#</EmailAddress>\n"
+ " <UserName>#3#</UserName>\n"
+ " <Locale>en_US</Locale>\n"
+ " <DeploymentZone>main_site</DeploymentZone>\n"
+ " <Products>\n"
+ " <Product>\n"
+ " <ExternalId>#4#</ExternalId>\n"
+ " <Name>#5#</Name>\n"
+ " <Price>#6#</Price>\n"
+ " </Product>\n"
+ " </Products>\n"
+ "</Interaction>";
public static void main(String[] args) {
List<File> files = FileUtil.loopFiles("E:\\Users\\LIN\\Desktop\\file\\Excel\\");
if (CollUtil.isEmpty(files)) {
return;
}
for (File file : files) {
ExcelReader reader = ExcelUtil.getReader(file);
List<Map<String, Object>> readAll = reader.readAll();
String testStr = mode;
testStr =
testStr
.replaceAll("#1#", readAll.get(0).get("TransactionDate").toString())
.replaceAll("#2#", readAll.get(0).get("EmailAddress").toString())
.replaceAll("#3#", readAll.get(0).get("UserName").toString())
.replaceAll("#4#", readAll.get(0).get("ExternalId").toString())
.replaceAll("#5#", readAll.get(0).get("Name").toString())
.replaceAll("#6#", readAll.get(0).get("Price").toString());
System.out.println(testStr);
System.out.println("==========================");
File newFlie =
FileUtil.appendUtf8String(
testStr, file.getPath().replaceAll("Excel", "XML").replaceAll("xlsx", "xml"));
}
}
}