|
本帖最后由 67886387 于 2015-5-8 16:28 编辑
首先技术路线是参考网上案例(JAVA + FreeMarker + jacob_1.9),说复杂其实也不复杂,只是在配置XML的时候需要仔细点,所有用到jar和js都在最后的附件中,以自身项目为例做下面步骤:
1,业务流程是在grid列表下点击某条数据生成合同并通过windowDialog组件打开合同详情;
2,创建action,名称为:CreateDoc- private static Configuration configuration = null;
- private static WordToPDF wordToPDF = null;
- /**
- * 生成word
- * @param fid 主键
- * @param type 模板类型
- * @return
- */
- public static String CreateDoc(String fid, String type) {
- return getDoc(fid, type);
- }
- //生成模板
- public static String getDoc(String fid, String type) {
- configuration = new Configuration();
- configuration.setDefaultEncoding("utf-8");
- configuration.setClassicCompatible(true);
- // 要填入模本的数据文件
- Map<String, Object> dataMap = new HashMap<String, Object>();
- getData_XF(fid, dataMap);
- FileSystem fileSystem = FileSystemWrapper.instance();
- String real = fileSystem.getRealPath("/LandManagerSys/template");
- File file = new File(real);
- try {
- configuration.setDirectoryForTemplateLoading(file);
- } catch (IOException e2) {
- e2.printStackTrace();
- }
- Template t = null;
- try {
- // test.ftl为要装载的模板
- t = configuration.getTemplate(type + ".ftl");
- } catch (IOException e) {
- e.printStackTrace();
- }
- String ul = getHome() + "/model/UI/LandManagerSys/doc/" + fid + type + ".doc";
- System.out.println(ul);
- File outFile = new File(ul);
- if (outFile.exists()) {
- outFile.delete();
- }
- Writer out = null;
- try {
- out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- }
- try {
- t.process(dataMap, out);
- } catch (TemplateException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- out.flush();
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- wordToPDF = new WordToPDF();
- String pdfUrl = getHome() + "/model/UI/LandManagerSys/doc/" + fid + type + ".pdf";
- wordToPDF.wordToPDF(ul, pdfUrl);
- return outFile.getName();
- }
-
- /**
- *
- * @param fid
- * @param dataMap
- */
- public static void getData_XF(String fid, Map<String, Object> dataMap) {
- String oracle = "SELECT JZ_XX.*, TO_CHAR(FSQRCSNY, 'YYYY-MM-DD') AS SQRCSNY , TO_CHAR(FSLRQ, 'YYYY-MM-DD') AS SLRQ , TO_CHAR(FTJRQ, 'YYYY-MM-DD') AS TJRQ , TO_CHAR(FZCSQRQ, 'YYYY-MM-DD') AS ZCSQRQ , TO_CHAR(FCJRQ, 'YYYY-MM-DD') AS CJRQ FROM JZ_XX";
- String mssql = "SELECT JZ_XX.*, CONVERT(VARCHAR(10), FSQRCSNY, 23) AS SQRCSNY , CONVERT(VARCHAR(10), FSLRQ, 23) AS SLRQ , CONVERT(VARCHAR(10), FTJRQ, 23) AS TJRQ , CONVERT(VARCHAR(10), FZCSQRQ, 23) AS ZCSQRQ , CONVERT(VARCHAR(10), FCJRQ, 23) AS CJRQ, DateName(year,FZCSQRQ) as NIAN, DateName(month,FZCSQRQ) as YUE, DateName(day,FZCSQRQ) as RI FROM JZ_XX JZ_XX";
- String mysql = "SELECT JZ_XX.*, date_format(FSQRCSNY, '%Y-%m-%d') AS SQRCSNY, date_format(FSLRQ, '%Y-%m-%d') AS SLRQ, date_format(FTJRQ, '%Y-%m-%d') AS TJRQ, date_format(FZCSQRQ, '%Y-%m-%d') AS ZCSQRQ, date_format(FCJRQ, '%Y-%m-%d') AS CJRQ, date_format(now(), '%Y') AS NIAN, date_format(now(), '%m') AS YUE, date_format(now(), '%d') AS RI FROM JZ_XX JZ_XX";
- if (!"".equals(fid)) {
- oracle += " WHERE JZ_XX.FID='" + fid + "'";
- mssql += " WHERE JZ_XX.FID='" + fid + "'";
- mysql += " WHERE JZ_XX.FID='" + fid + "'";
- }
- System.out.println(mssql);
- HashMap<String, String> sqlMap = new HashMap<String, String>();
- sqlMap.put(SQL.DEFAULT_DB_NAME, mssql);
- sqlMap.put(DatabaseProduct.ORACLE.name(), oracle);
- sqlMap.put(DatabaseProduct.MSSQL.name(), mssql);
- sqlMap.put(DatabaseProduct.KINGBASE.name(), oracle);
- sqlMap.put(DatabaseProduct.SYBASE.name(), mssql);
- sqlMap.put(DatabaseProduct.DB2.name(), oracle);
- sqlMap.put(DatabaseProduct.MYSQL.name(), mysql);
- Table table = SQL.select(sqlMap, null, "/LandManagerSys/LandDAM/data");
- Iterator<Row> rows = table.iterator();
- Collection<String> list = table.getColumnNames();
- String[] strArray1 = (String[]) list.toArray(new String[0]);
- while (rows.hasNext()) {
- Row row = rows.next();
- for (int i = 0; i < table.getColumnCount(); i++) {
- dataMap.put(strArray1[i], row.getValue(strArray1[i]));
- }
- }
- }
-
- //获取服务端安装地址
- public static String getHome() {
- String home = System.getenv("JUSTEP_HOME");
- if (home == null) {
- File fl = new File(ContextHelper.getSessionContext().getSession().getServletContext().getRealPath("/WEB-INF/justep.xml"));
- if (fl.exists()) {
- try {
- SAXReader reader = new SAXReader();
- Document doc = reader.read(fl);
- Element e = doc.getRootElement().element("JUSTEP_HOME");
- if (e != null) {
- home = e.getText().trim();
- } else {
- home = fl.getParent() + "/../../..";
- }
- } catch (DocumentException e) {
- e.printStackTrace();
- throw new UnsupportedOperationException(fl.getPath() + "不是标准的XML!", e);
- }
- } else {
- throw new UnsupportedOperationException("没有找到" + fl.getPath());
- }
- }
- try {
- File f = new File(home);
- home = f.getCanonicalPath();
- } catch (IOException e) {
- throw new RuntimeException("JUSTEP_HOME: “" + home + "“不存在!");
- }
- return home;
- }
复制代码
|
|