小工具:word表格文字转化成insert语句
群里的一个朋友有个需要,要让把word里的表格数据插入到数据库里面。
我的思路是,把格式化的数据转成insert语句,然后去执行就可以了。
要求的insert语句格式是:'insert into xxx values("字段1","字段2","字段3");"
public class WordsProcessor {
public static void main(String[] args) {
String strings="Col1 Col2 Col3 Col4 Col5\r\n" +
"abc def ghi jkl mno\r\n" +
"123 456 789 012 345\r\n";
String[] words=strings.split("\r\n");
Stream<String> stream1 = Arrays.stream(words);
stream1.forEach(s->{
String[] temp=s.split("\\s+");
String result="insert into XXX values(";
for(int i=0;i<temp.length;i++) {
result+="\""+temp[i]+"\"";
if(i!=temp.length-1) {
result+=",";
}
}
result+=")";
System.out.println(result);
});
}
}
输出:
insert into XXX values("Col1","Col2","Col3","Col4","Col5")
insert into XXX values("abc","def","ghi","jkl","mno")
insert into XXX values("123","456","789","012","345")