rever

群里的一个朋友有个需要,要让把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")

分类:

技术点:

相关文章:

  • 2021-04-15
  • 2022-01-15
  • 2021-05-24
  • 2021-12-05
  • 2022-01-04
  • 2021-11-05
猜你喜欢
  • 2021-11-30
  • 2021-08-12
  • 2022-12-23
  • 2022-12-23
  • 2021-05-25
  • 2022-12-23
  • 2021-07-19
相关资源
相似解决方案