JAVA操作Execl时生成的XML文件的编码问题

     Java操作Execl在比较复杂的情况下如:需要有精致的格式,且有多个Sheet表格的时候并没有太完美的解决方案,将Execl文件另存为XML格式,然后像操作文本文件一样操作Execl是一种不错的解决方案!

    但具体操作的时候遇见使用JAVA编辑后保存的文件内容与Execl转存的内容一模一样,但是就是用execl打不开的情况,execl转存的xml在eclipse中查看时UTF-8格式,JAVA生成的XML文件使用普通的文本编辑器打开无乱码,但是放在eclipse中出现乱码现象,应该是JAVA生成的XML文件的编码问题!

一开始使用FileWriter无法控制保存文件的格式,改为使用OutputStreamWriter,问题解决

Configuration config = FreeMarkerUtil.getConfiguration(TestSaveAs.class, "template");
			
			Template template = config.getTemplate("test.xml");
			
			File file = new File("c:\\test.xml");
			
			Map root = new HashMap();
			
			Writer writer = new FileWriter(file);
			template.process(root, writer);
			
			writer.flush();
			writer.close();


 

最终代码:

 

			Configuration config = FreeMarkerUtil.getConfiguration(TestSaveAs.class, "template");
			
			Template template = config.getTemplate("test.xml");
			
			File file = new File("c:\\test.xml");
			
			Map root = new HashMap();
			
			Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); 
			template.process(root, writer);
			
			writer.flush();
			writer.close();


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值