把domino 的文件生成 doc 或者 html 檔案的實現

jarge發表於2010-05-21

import lotus.domino.*;
import java.util.*;
import java.io.PrintWriter;
import com.gdcn.admin.*;
import com.gdcn.rmi.*;
import java.rmi.Naming;
import com.gdcn.domino.util.Log;
import java.io.*;
import com.gdcn.domino.*;
import com.gdcn.*;
import java.lang.*;
import org.jdom.Element;


public class JavaAgent extends AgentBase {
     HashMap paramsMap;
 private PrintWriter pw;
 private PubliceFunction pFunc = new PubliceFunction();
 private static DominoInt dominoInt = null;
 private Session session = null;

 private String tmpPath_forWord = null; //臨時目錄
 private AgentContext agentContext;
 private Database   gz_dict_config = null;
 private Vector youxiang= new Vector(); //律師郵箱
 private Document tmpDoc=null;
 
 public void NotesMain() {

  try {
   session = getSession();
   agentContext = session.getAgentContext();
   pw = getAgentOutput();
   dominoInt = pFunc.getRmi(session);
   
   Document doc = agentContext.getDocumentContext();
    Database db = agentContext.getCurrentDatabase();
    //取業務配置庫
               String lstrPath = db.getFilePath().toString()
       .substring(
         0,
         (db.getFilePath().length() - db
         .getFileName().length()));
   gz_dict_config= session.getDatabase(db.getServer(), lstrPath
                     + "gz_dict_config.nsf");
               View law=null;
               Document ywDoc = null;
           if(doc.getItemValueString("lxr")!=null && !doc.getItemValueString("lxr").equals("")){
                   law=gz_dict_config.getView("vw_ShowLSConfigformail"); 
                   ywDoc= law.getDocumentByKey(doc.getItemValueString("lxr"));
                   youxiang=ywDoc.getItemValue("youxiang");
               } 
              for(int i=0;i            String platform. = session.getPlatform();
                   String tempRtfDir=getDbPathWithSepChar(db); //臨時文件路徑    
       String tmpAttName = "test1.doc";
       tmpPath_forWord = session.getEnvironmentString("Directory", true);
       if (platform.equals("UNIX") || platform.equals("LINUX")) {
       tmpPath_forWord += "/domino/html/download/";
       } else {
       tmpPath_forWord += "";
   }

   String tempRtfFile = tmpPath_forWord+tmpAttName;

               tmpDoc = db.createDocument(); 
               Document RcfvTable =db.createDocument();  
   RcfvTable.replaceItemValue("form","RcfvTable");
   RcfvTable.replaceItemValue("user",agentContext.getEffectiveUserName());
   RcfvTable.replaceItemValue("zcdh",doc.getFirstItem("bhText").getText());
   RcfvTable.replaceItemValue("fadanren",doc.getItemValueString("InitalAuthor_SYS_1"));
               RcfvTable.replaceItemValue("jbtime_1", doc.getFirstItem("jbtime").getText());
            RcfvTable.replaceItemValue("riqi",doc.getItemValueString("InitalCreated").substring(0,10));
             
               RcfvTable.replaceItemValue("projectname_1", doc.getFirstItem("projectname").getText());
               RcfvTable.replaceItemValue("name_1", doc.getFirstItem("name").getText());
               RcfvTable.replaceItemValue("lxr_1", doc.getFirstItem("lxr").getText());
    //         RcfvTable.replaceItemValue("zcdnr_1", doc.getFirstItem("zcdnr").getText());
               Vector xinxi=doc.getItemValue("zcdnr");
                     String neir=""; 
                     for(int k=0;k                       neir+=xinxi.get(k)+"
";                      
                      
               }
               RcfvTable.replaceItemValue("zcdnr_1", neir);
               RcfvTable.replaceItemValue("yqfksj_1", doc.getFirstItem("yqfksj").getText()+"  "+doc.getItemValueString("qjtime1"));
   RcfvTable.replaceItemValue("hdnr_1", "");
               RcfvTable.replaceItemValue("cljg", "");
               RichTextItem tmpItem;
               if(tmpDoc.hasItem("rtf_DocAtt")) tmpDoc.removeItem("rtf_DocAtt");
               tmpItem=tmpDoc.createRichTextItem("rtf_DocAtt");
               RcfvTable.renderToRTItem(tmpItem);
               //文件正文內容   
               String tmp = tmpItem.getText();
      
   FileOutputStream fOut=new FileOutputStream(tempRtfFile);
   fOut.write(tmp.getBytes());
   if(tmpDoc.hasItem("rtf_DocAtt")) tmpDoc.removeItem("rtf_DocAtt");
   tmpItem=tmpDoc.createRichTextItem("rtf_DocAtt"); 
   tmpItem.××Object(××dedObject.××_ATTACHMENT,"",tempRtfFile,"test1.doc");
               File tmpFile = new File(tempRtfFile); 
   tmpFile.delete();
               RcfvTable.remove(true); 
               String direct = session.getEnvironmentString("Directory", true);
               String tmpPath = "";
   
    if (platform.toLowerCase().indexOf("windows") > -1) {
    tmpPath = direct + "";    
    } else {
    tmpPath = direct + "/domino/html/temp/";    
    }  
                    File fileTemp = new File(tmpPath);
        if(!fileTemp.isDirectory()){
         fileTemp.mkdir();  
   
                     }   
                  //      for(int i=0;i         tmpDoc.replaceItemValue("form", "Memo");
         //設定當前登陸使用者為發件人
         tmpDoc.replaceItemValue("Principal",doc.getItemValueString("currentUserNameCommon"));      
         //郵件標題
         tmpDoc.replaceItemValue("Subject", "test1--"+doc.getItemValueString("bhText"));
         //郵件內容
             RichTextItem wbody = tmpDoc.createRichTextItem("wBody");         
                  //         wbody.appendText("");                    
                             MIMEEntity body = tmpDoc.createMIMEEntity();     
         Stream stream = session.createStream();
         stream.writeText(wbody.getText());
         body.setContentFromText(stream, "text/html;charset=UTF-8",MIMEEntity.ENC_NONE);
                             wbody.remove();
                               //========== 把附件加到郵件上 ======
                   //1、取附件名
                   String attformular = "@AttachmentNames";
                   Vector ret = session.evaluate(attformular,doc);
                   if(!ret.get(0).toString().equals("")){ //如果存在附件
                            //2、拆除附件
                            for (int j = 0; j < ret.size(); j++) {
                                 String attName_down = ret.get(j).toString();
                                   try {
                             ××dedObject obj_down = doc.getAttachment(attName_down);
                             if (obj_down != null) {
                                   obj_down.extractFile(tmpPath + attName_down);
                                   obj_down.recycle();
                                   obj_down = null;
                             }
                                   } catch (Exception e) {
                           e.printStackTrace();
                         }
                            }
                            //3、上傳附件
                            RichTextItem attFile =tmpDoc.createRichTextItem("Body");
                            for (int k = 0; k < ret.size(); k++) {
                                    String attName_up = ret.get(k).toString();
                                    File upAttFile = new File(tmpPath + attName_up);
                                    if (upAttFile.exists()) {
                                             attFile.××Object(××dedObject.××_ATTACHMENT, null,
                                 tmpPath + attName_up, attName_up);
                                             try {
                                                  upAttFile.delete(); // 刪除臨時文件
                                             } catch (Exception delErr) {
                                                     delErr.printStackTrace();
                                                     }                                          
                                           } else {
                                         throw new RuntimeException("NotesMain()出錯:伺服器檔案["+tmpPath + attName_up+"]不存在");
                                    }
                            }
                            attFile.recycle();
                            attFile = null;
                  } 
                     
          tmpDoc.send(false,youxiang.get(i).toString());   
                         }          
        //      tmpDoc.send(false,youxiang);                                    
                              doc.save(true,true);
                  pw.println("
        
                  ywDoc.recycle();
              law.recycle();
                            
   
   } catch(Exception e) {
   e.printStackTrace();
  }finally{
   if(dominoInt != null && (dominoInt instanceof com.gdcn.domino.pool.DominoIntProxy))
    ((com.gdcn.domino.pool.DominoIntProxy)dominoInt).release();
   try {
    session.recycle();
    } catch (Exception e) {
    e.printStackTrace();
   }

  }
 }
     /**
  * 功能:取當前資料庫目錄(包括分隔符號),與平臺不相關
  */
 public String getDbPathWithSepChar(Database db) throws Exception {
  String path = "";
  String dbPath = db.getFilePath();
  String filename = db.getFileName();
  path = dbPath.substring(0, dbPath.length() - filename.length());
  return path;
 }  

}

有時生成的doc文件格式不對,需要在表單設定裡,把所有內容內嵌為HTML,然後用和把它們包起來

同理可以生成 html 檔案

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10247040/viewspace-663313/,如需轉載,請註明出處,否則將追究法律責任。

相關文章