使用net.sourceforge.rtf.RTFTemplate生成损坏的输出.doc;

时间:2017-06-19 13:14:32

标签: java velocity

  

我正在使用RTFTemplate API使用 .rtf 模板创建报告(。doc)

  • 我的 .rtf 模板文件除动态单元格外还有多个文本字段。这些文本字段应该在生成doc文件后手动填充。

  • 很少有文本字段属于“常规文本”类型格式,其他文本字段具有“数字类型”格式。

  • 如果我将Number textfields设置数字格式保持为#,## 0.00 ,请参阅:Corrupt Number Format,然后输出文件已损坏。
  • 但如果我将数字格式更改为 0.00 ,则表示输出正确无误。

以下是我的java代码:

public class TestEncore {
    private static Map<String, RTFDocument> transformedDocumentMap = new HashMap<String, RTFDocument>();

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        try {
            String baseDirectory = "./";
            String outDirectory =      baseDirectory+"out"+File.separator;
            String rtfSource =         baseDirectory+"out2"+File.separator + "model.rtf";
            String rtfTarget =         outDirectory + "/sortie2.rtf";
            // Create out Directory
            File out = new File(outDirectory);
            out.mkdirs();

            HashMap<String,Object> valueMap=new HashMap<String,Object> ();
            valueMap.put("date", Calendar.getInstance().getTime());
            valueMap.put("name", "ABC DEF XYZ");
            valueMap.put("age", 24);

            /**
             * Execute transformed RTFDocument
             * and put it the instance of transformed RTFDocument
             * into the Map (into cache)
             */
            executeTransformedDocument(rtfSource,valueMap);

            // 1. Get default RTFtemplateBuilder
            RTFTemplateBuilder builder = RTFTemplateBuilder.newRTFTemplateBuilder();            

            // 2. Get RTFtemplate with default Implementation of template engine (Velocity) 
            RTFTemplate rtfTemplate = builder.newRTFTemplate();   

            // 3. Get the transformed document
            // and NOT the rtf source
            RTFDocument transformedDocument =  (RTFDocument)transformedDocumentMap.get(rtfSource); 
            rtfTemplate.setTransformedDocument(transformedDocument);

            VelocityTemplateEngineImpl templateEngine = new VelocityTemplateEngineImpl();
            VelocityEngine velocityEngine = new VelocityEngine();
            velocityEngine.setProperty("input.encoding ", "UTF-8"); 
            velocityEngine.setProperty("output.encoding", "UTF-8"); 
            velocityEngine.setProperty ("response.encoding", "UTF-8");
            templateEngine.setVelocityEngine(velocityEngine);
            rtfTemplate.setTemplateEngine(templateEngine);  


            OutputStreamWriter fileout = new OutputStreamWriter(new FileOutputStream(rtfTarget), "UTF-8"); 
            rtfTemplate.merge(fileout);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void executeTransformedDocument(String rtfSourceFile,Map<String,Object> templateValues) throws Exception {
        // 1. Get default RTFtemplateBuilder
        RTFTemplateBuilder builder = RTFTemplateBuilder.newRTFTemplateBuilder();            

        // 2. Get RTFtemplate with default Implementation of template engine (Velocity) 
        RTFTemplate rtfTemplate = builder.newRTFTemplate();   

        // 3 Load XML fields available and set it to the RTFTemplate
            if(templateValues!=null&& !templateValues.isEmpty()){
                Set<Entry<String,Object>> entrySet=templateValues.entrySet();
                Iterator<Entry<String, Object>> iterator=entrySet.iterator();
                while(iterator.hasNext()){
                    Entry<String, Object> entry=iterator.next();
                    rtfTemplate.put(entry.getKey(), entry.getValue());
                }
            }
        // 3. Set the RTF model source 
        rtfTemplate.setTemplate(new File(rtfSourceFile));  

        // 4. Transform to get transformed RTFDocument
        RTFDocument transformedDocument = rtfTemplate.transform();

        // 5. Cache the transformed document into map
        transformedDocumentMap.put(rtfSourceFile, transformedDocument);
    }
    }
  • 有没有人遇到过这样的问题?

  • 它可以与MSOffice版本相关吗?我正在使用 MSOffice2013

0 个答案:

没有答案