救命!!java导出的word打不开

Suprit 2018-06-10 10:46:24
使用freemaker的java程序导出的word打不开,套用了几个模板,把里面的数据给替换了一下,可生产的word文件就是打不开!

以下是程序中导出word的相关代码
//导出pdf按钮
//导出word按钮
@FXML
private void handleWordExport() {
int selectedIndex = studentTable.getSelectionModel().getSelectedIndex();
Student selectedStudent = studentTable.getSelectionModel().getSelectedItem();
if(selectedIndex >= 0) {
//只有在选中一个学生的时候才会导出
//你只看这一点就行,这一点和下面对应
WordTest test = new WordTest(selectedStudent);
test.createWord();

}else {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("导出失败");
alert.setHeaderText("没有学生可以选择");
alert.setContentText("请选择一个学生");
alert.showAndWait();
}

}


//导出word类和方法
public class WordTest {

private Configuration configuration = null;
Student student;

public WordTest(Student selectedStudent){
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
configuration.setEncoding(Locale.getDefault(), "utf-8");
//创建一个Configuration实例 的后面给这个对象设置编码为utf-8:

this.student = selectedStudent;
}

public void createWord(){
Map<String,Object> dataMap=new HashMap<String,Object>();
getData(dataMap);
configuration.setClassForTemplateLoading(this.getClass(), "/Student/model");//模板文件所在路径
Template t = null;
try {
t = configuration.getTemplate("exportModel.ftl"); //获取模板文件
System.out.println("获取模板文件成功");
} catch (IOException e) {
e.printStackTrace();
}
File outFile = new File("E:/outFile/"+student.getName()+"的资料.doc"); //导出文件
System.out.println("导出位置为E:/outFile");
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}

try {
t.process(dataMap, out); //将填充数据填入模板文件并输出到目标文件

//关闭流
out.flush();
out.close();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("导出成功");
}

//填充数据
private void getData(Map<String, Object> dataMap) {
dataMap.put("Name", student.getName());
dataMap.put("Photo", student.getPhoto());
dataMap.put("Id", student.getid());
dataMap.put("Class", student.getClassIn());
dataMap.put("Chinese", student.getChinese());
dataMap.put("Math", student.getMath());
dataMap.put("English", student.getEnglish());
dataMap.put("Physics", student.getphysics());
dataMap.put("Chemistry", student.getchemistry());
dataMap.put("biology", student.getbiology());

}
}


以下是模板内容



以下是错误内容


以下是错误文件的xml部分核心代码(因为太大了无法上传,核心代码可以看到数据已经被替换)




跪求大神了!


...全文
2088 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wqqqianqian 2019-03-08
  • 打赏
  • 举报
回复
用pageoffice 生成word吧,pageoffice 是调用的客户端电脑的office 。生成的效果和office 的效果一样,不可能存在打不开的情况。
月球和六毛钱 2018-12-04
  • 打赏
  • 举报
回复
修改xml文件的过程中,如果误加了空格或换行,都会出现导出文档无法打开的情况
QWERT4745 2018-06-11
  • 打赏
  • 举报
回复
看下控制台生成word时有没有报错信息,是哪个变量的问题,再不行就是注释掉部分代码去排除
stacksoverflow 2018-06-11
  • 打赏
  • 举报
回复
编码的问题参照这里 https://blog.csdn.net/u013456370/article/details/71404108
stacksoverflow 2018-06-11
  • 打赏
  • 举报
回复
替换前后的文件用工具比较一下。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧