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();
本文介绍了一种使用Java操作Excel的方法,特别是在需要处理复杂样式和多个Sheet表的情况下,通过将Excel转换为XML格式来实现更灵活的操作。文章解决了在转换过程中遇到的文件编码问题,并给出了具体的代码示例。
3021

被折叠的 条评论
为什么被折叠?



