一、创建数据库连接
private static Connection conn = null;
private static String url = "jdbc:mysql://127.0.0.1:3306/pm?useUnicode=true&characterEncoding=utf8";
private static String userName = "root";
private static String password = "root";
//创建连接
public static Connection MysqlConnect() {
try {
Class.forName("com.mysql.jdbc.Driver");
// 驱动 Connection
try {
conn = DriverManager.getConnection(url, userName, password);
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("数据库连接失败!");
}
return conn;
}
二、生成word文档
public static String toWord(){
Connection mysqlConnect = MysqlConnect();
String RE_INITIATOR = "1";
String RE_INITIATOR_DEP = "1";
String RE_START_TIME = "1";
String RE_END_TIME = "1";
String RE_RELATED_DEP = "1";
String RE_IS_CROSS_DEP = "1";
String RE_DISCREPTION = "1";
try {
Statement sts = null;
String sql = "select * from pm_requirement where RE_ID = '1' ";
ResultSet resul = null;
try {
sts = (Statement) mysqlConnect.createStatement();
resul = sts.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("查询的结果如下:");
while(resul.next()){
RE_INITIATOR = resul.getString("RE_INITIATOR");
//RE_INITIATOR_DEP = resul.getString("RE_INITIATOR_DEP");
RE_START_TIME = resul.getString("RE_START_TIME");
RE_END_TIME = resul.getString("RE_END_TIME");
//RE_RELATED_DEP = resul.getString("RE_RELATED_DEP");
//RE_IS_CROSS_DEP = resul.getString("RE_IS_CROSS_DEP");
RE_DISCREPTION = resul.getString("RE_DISCREPTION");
}
} catch (SQLException e) {
e.printStackTrace();
}
Map<String,Object> dataMap = new HashMap<String, Object>();
String fileurl=null;
try {
//日期
dataMap.put("RE_START_TIME", new SimpleDateFormat("yyyy年MM月dd日").format(new SimpleDateFormat("yyyy-MM-dd").parse(RE_START_TIME)));
dataMap.put("RE_END_TIME", new SimpleDateFormat("yyyy年MM月dd日").format(new SimpleDateFormat("yyyy-MM-dd").parse(RE_END_TIME)));
dataMap.put("RE_INITIATOR", RE_INITIATOR);
dataMap.put("RE_INITIATOR_DEP", RE_INITIATOR_DEP);
//dataMap.put("RE_START_TIME", RE_START_TIME);
//dataMap.put("RE_END_TIME", RE_END_TIME);
dataMap.put("RE_RELATED_DEP", RE_RELATED_DEP);
dataMap.put("RE_IS_CROSS_DEP", RE_IS_CROSS_DEP);
dataMap.put("RE_DISCREPTION", RE_DISCREPTION);
//Configuration 用于读取ftl文件
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
//指定路径的第二种方式,我的路径是C:/a.ftl
configuration.setDirectoryForTemplateLoading(new File("D:/"));
//输出文档路径及名称
File outFile = new File("D:/业务需求单.doc");
//以utf-8的编码读取ftl文件
Template template = configuration.getTemplate("requirement.ftl", "utf-8");
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
template.process(dataMap, out);
out.close();
fileurl = "D:/业务需求单.doc";
} catch (Exception e) {
e.printStackTrace();
}
附:word模板制作
第一步:手动创建一个doc模板;
第二步:将world中需要动态生成的部分用${}替换
第三步:将world另存为xml文件
第四步:打开xml看一下${XXX}标签是否分开,如果分开了,将${}中间无用的代码删掉,重命名.xml 改为 .ftl
pom.xml引入freemarker.jar
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>