lotus中Excel的导出

来源:互联网 发布:淘宝网防晒服 编辑:程序博客网 时间:2024/06/05 23:55

import java.io.PrintWriter;

import lotus.domino.AgentBase;
import lotus.domino.AgentContext;
import lotus.domino.Database;
import lotus.domino.DocumentCollection;
import lotus.domino.NotesException;
import lotus.domino.Session;
import lotus.domino.Document;
import java.util.Vector;

/**
 * 将视图数据导出到excel中
 *
 */
public class OutputToExcel extends AgentBase {

 private Session session = null;
 private AgentContext agentContext = null;
 private Database db = null;
 private PrintWriter pw = null;
 private StringBuilder sb = null;

 private static String[] FIELDS = { "Year", "Month", "BM", "GZYHM",
   "JBGZ", "XYGZ", "NGGZ", "BT", "GZHJ", "BK", "KK", "YFHJ", "SBTC", "ZFGJJ", "GHF", "DKS",
   "ZK", "BF", "DBHJ", "SFHJ"};
 private static String[] FIELDSNAME = { "年份", "月份", "部门", "用户名",
   "基本工资", "效益工资", "年功工资", "补贴" , "合计", "补款", "扣款", "应发合计", "社保统筹",
   "住房公积金", "工会费", "代扣税", "暂扣", "补发", "合计", "实发合计"};

 public void NotesMain() {
  try {
   session = getSession();
   agentContext = session.getAgentContext();
   db = agentContext.getCurrentDatabase();
   
   pw = getAgentOutput();
   sb = new StringBuilder();

   pw.println("Content-Type:application/vnd.ms-excel");
   printHead();
   printView();
   pw.print(sb.toString());

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

 private void printHead() {
  for (String fieldName : FIELDSNAME) {
   sb.append(fieldName);
   sb.append("\t");
  }
  sb.append("\n");
 }

 private void printView() throws NotesException {
  
  Document docNow = agentContext.getDocumentContext();//当前文档
  
  //搜索需要导出的工资
  String year = docNow.getItemValueString("Year");
  String month = docNow.getItemValueString("Month");
  String username = docNow.getItemValueString("YHM"); 
  String bm = docNow.getItemValueString("BM");
  
  //释放空间
  docNow.recycle();
  
  //搜索条件
  String sql = "Form='yggz'  & @Contains(GZVerifyStatus;'已确认')";
  if(!year.equals("")){    
   sql = sql + "& @Contains(Year;'" + year + "')";    
  }
  if(!month.equals("")){
   sql = sql + "& @Contains(Month;'" + month + "')"; 
  }
  if(!username.equals("")){
   sql = sql + "& @Contains(GZYHM;'" + username + "')"; 
  }
  if(!bm.equals("")){
   sql = sql + " & @Contains(BM;'" + bm + "')";
  }
  
  
  DocumentCollection docs = db.search(sql);
  int num = docs.getCount();
  
  
  //如果没有输入搜索条件,则搜索结果为空
  if(year.equals("") && month.equals("") && username.equals("") && bm.equals("")){
   num = 0;
  }

  
  Document doc = docs.getFirstDocument();
  Document temp = null;
  
  while (num != 0 && doc != null) {
     printDocInfo(doc);
     temp = docs.getNextDocument(doc);
     doc.recycle();
     doc = temp;
  }
 }

 private void printDocInfo(Document doc) throws NotesException {
  for (String field : FIELDS) {
   Vector fldvalue = doc.getItemValue(field);
   String fld = "";
   if (fldvalue.size() > 0) {
    fld = fldvalue.get(0).toString();
   }
   sb.append(fld);
   sb.append("\t");
  }
  sb.append("\n");
 }

 private void recycle() {
  try {
   if (pw != null) {
    pw.close();
   }
   if (db != null) {
    db.recycle();
   }
   if (agentContext != null) {
    agentContext.recycle();
   }
   if (session != null) {
    session.recycle();
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}