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();


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章