在B/S模式下使用java代理导入excel文件的实现方法及代码

用java代理获取附件输入流EmbeddedObject.getInputStream(),使用开源文件的Java Excel API 来读取excel

 

jxl.jar的下载地址http://www.andykhan.com/jexcelapi/download.html,lotus8最高支持2.68版本的

 

代码如下:

 

 

 

 

import lotus.domino.*;

 

import java.io.*;

import java.text.SimpleDateFormat;

import java.util.Date;

import jxl.*;

import jxl.read.biff.BiffException;

/**

* @Description TODO 

* @author zys

* @ProjectName Test 

* @FileName JavaAgent.java

* @date 2008-11-24

*/

public class JavaAgent extends AgentBase {

 

 

public void NotesMain() {

 

 

  try {

 

   Session session = getSession();

   AgentContext agentContext = session.getAgentContext();

   Document doc = agentContext.getDocumentContext();

   //String CurrUser=doc.getItemValueString("CurrUser");

   EmbeddedObject excelEmbeddedObject=null;

   String sType  = session.getPlatform();

               //取得平台类型

 

   // (Your code goes here) 

   String ServerPath = session.getEnvironmentString("Directory",true);

            PrintWriter pw=getAgentOutput()  ; 

   if (!ServerPath.equals(""))

   {

    ServerPath=getServerPath(ServerPath,sType);

    //boolean flag=false;

    //File excelFile=null;

    //excelFile=getFileByDoc(doc,ServerPath,CurrUser,pw);

 

    excelEmbeddedObject=this.getEmbeddedObjectByDoc(doc,pw);

    InputStream excelInputStream=null;

    if (excelEmbeddedObject!=null)

    excelInputStream=excelEmbeddedObject.getInputStream();

    //if (excelFile!=null)

    if(excelInputStream!=null)

    {

     try

     {

      //导入数据,在此方法中加入导入数据的处理

      //ImportDataFromExcel(excelFile,pw,doc);

      this.ImportDataFromExcel(excelInputStream,pw,doc,session);

      excelInputStream.close();

     }catch(Exception e) {

      if (excelEmbeddedObject!=null) excelEmbeddedObject.recycle();

      /*if(excelFile!=null)

      {

       flag=excelFile.delete();

       if(!flag)

       {

        System.out.println("附件删除失败");

       }

      }*/

      e.printStackTrace();

     }

     //flag=excelFile.delete();

     //if(!flag)

     //{

     // System.out.println("附件删除失败");

     //}

 

    }

    if (excelEmbeddedObject!=null) excelEmbeddedObject.recycle();

   }

   else

   {

    pw.println("导出失败,没找到服务器路径");

   }

//code end

     } catch(Exception e) {

 

   e.printStackTrace();

  }

}

 

/**

  * 

  * @Description TODO 

  * @param excelInputStream

  * @param pw

  * @param doc

  * @date 2008-12-9  15:17:00

  * @author zys

  * @return void

  */

public void ImportDataFromExcel(InputStream excelInputStream,PrintWriter pw,Document doc,Session s)

{

        int excelRows;//行数

        //int excelColumns;//列数

  Cell cell1;//单元格对象

  Workbook workbook;//Excel对象

  DateTime dt=null;

  try 

  {

 

   workbook = Workbook.getWorkbook(excelInputStream);

   Database db=s.getAgentContext().getCurrentDatabase();

   //Database flowdb=s.getDatabase(db.getServer(),doc.getItemValueString("appName") + "/mbgl.nsf");

   //Notes

   Document newdoc=null;

   Sheet sheet=workbook.getSheet(0);

   excelRows=sheet.getRows();

   //excelColumns=sheet.getColumns();

   System.out.println("excelRows="+excelRows);

   String CurrUser=doc.getItemValueString("UserName");

   Name notesName=s.createName(CurrUser);

   //增加你的代码

   //pw.println("<META HTTP-EQUIV='Pragma' CONTENT='no-cache'>");

   //pw.println("<META HTTP-EQUIV=/"Content-Type/" content=/"text/html; charset=gb2312/">");

   //pw.println("<LINK REL=stylesheet HREF=/"" + doc.getItemValueString("ResourcePath") + "css/global.css/" TYPE=/"text/css/">");

   //pw.println("<link href=/"" + doc.getItemValueString("ResourcePath") + "css/viewStyle.css/" rel=/"stylesheet/" type=/"text/css/">");

   String ErrorMsg="";

   //pw.println("<table width=95% align=center class=tableClass cellspan='5' cellpadding='2' border='1' id=outtable>");

   for(int i=1;i<excelRows;i++)

   {

    //pw.println("<tr>");

    cell1=sheet.getCell(0,i);//单元各对象

    if(!cell1.getContents().equals(""))

    {

     newdoc=db.createDocument();

     newdoc.replaceItemValue("form","frmWebFlow");

     //项目名称

     newdoc.replaceItemValue("docreater",notesName.getCanonical());

//System.out.println(doc.getItemValueString("currTime")+"11111111111");

     dt=s.createDateTime(doc.getItemValueString("currTime"));

     newdoc.replaceItemValue("docreatime",dt);

//newdoc.getFirstItem("docreatime").setDateTimeValue(dt); 

 

     newdoc.replaceItemValue("fldXmmc",cell1.getContents());

     cell1=sheet.getCell(1,i);//单元各对象 项目编码

     newdoc.replaceItemValue("fldXmbm",cell1.getContents());

     cell1=sheet.getCell(2,i);//单元各对象 审计机构名称

     newdoc.replaceItemValue("fldSjdw",cell1.getContents());

     cell1=sheet.getCell(3,i);//单元各对象 审计项目经理

     newdoc.replaceItemValue("fldProjectManager",cell1.getContents());

     cell1=sheet.getCell(4,i);//单元各对象 联系人

     newdoc.replaceItemValue("fldContact",cell1.getContents());     

     cell1=sheet.getCell(5,i);//单元各对象 联系电话

     newdoc.replaceItemValue("fldContactTel",cell1.getContents()); 

     cell1=sheet.getCell(6,i);//单元各对象 已完成工作小结

     newdoc.replaceItemValue("fldGznr",cell1.getContents()); 

     cell1=sheet.getCell(7,i);//单元各对象 将要进行的工作

     newdoc.replaceItemValue("fldWtlsqk",cell1.getContents()); 

     cell1=sheet.getCell(8,i);//单元各对象 审计发现问题

     newdoc.replaceItemValue("fldGzwczy",cell1.getContents());

 

     cell1=sheet.getCell(9,i);//单元各对象 工作中遇到的主要问题及建议解决方法

     newdoc.replaceItemValue("fldSjfx",cell1.getContents()); 

     cell1=sheet.getCell(10,i);//单元各对象 其它

     newdoc.replaceItemValue("fldBz",cell1.getContents()); 

     newdoc.replaceItemValue("subform",doc.getItemValueString("subform"));

newdoc.replaceItemValue("fldParentCode",doc.getItemValueString("fldParentCode"));

newdoc.replaceItemValue("viewname",doc.getItemValueString("viewname"));

newdoc.replaceItemValue("flowtype",doc.getItemValueString("flowtype"));

newdoc.replaceItemValue("flowid",doc.getItemValueString("flowid"));

     newdoc.computeWithForm(true,false);

     //newdoc.replaceItemValue("hfldFlowDefPath","hq/dep11/flowdef_11.nsf");

     newdoc.replaceItemValue("flownum",new Integer(0));

     newdoc.replaceItemValue("DocumentAuthors",notesName.getCanonical());

     newdoc.replaceItemValue("fldAuthor",notesName.getCanonical());

     newdoc.replaceItemValue("fldLastStepReader",notesName.getCanonical());

     newdoc.replaceItemValue("fldLastStepAuthor",notesName.getCanonical());

     newdoc.replaceItemValue("alldealer",notesName.getCanonical());

     newdoc.replaceItemValue("stat","正在起草");

 

 

 

     newdoc.save(true);

    }

    else

    {

     System.out.println("第"+i+"第一列为空,没有导入");

     ErrorMsg=ErrorMsg+"<tr><td class='tdContent'>第"+i+"行第一列为空,没有导入</td></tr>";

    }

    //for(int j=0;j<excelColumns;j++)

    //{

    // cell1=sheet.getCell(j,i);//单元各对象

    // pw.println("<td class='tdContent'>"+cell1.getContents()+"</td>");//单元格内容

    //}

    //pw.println("</tr>");

   }

   //pw.println("</table>");

   //增加你的代码

 

   workbook.close();

   pw.println("<META HTTP-EQUIV='Pragma' CONTENT='no-cache'>");

   pw.println("<META HTTP-EQUIV=/"Content-Type/" content=/"text/html; charset=gb2312/">");

   pw.println("<LINK REL=stylesheet HREF=/"" + doc.getItemValueString("ResourcePath") + "css/global.css/" TYPE=/"text/css/">");

   pw.println("<link href=/"" + doc.getItemValueString("ResourcePath") + "css/viewStyle.css/" rel=/"stylesheet/" type=/"text/css/">");

 

   pw.println("<table width=95% align=center class=tableClass cellspan='5' cellpadding='2' border='1' id=outtable>");

 

   pw.println("<tr><td class='tdContent'>导入完成</td></tr>");

   pw.println(ErrorMsg);

   pw.println("<tr><td align='center'><input type=button name='return' value='返  回' οnclick='javascript:history.back();'></td></tr>");

   pw.println("</table>");

  } catch (BiffException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  } catch (Exception e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

}

 

/**

  * 

  * @Description 在此方法中增加处理导入数据的代码

  * @param excelFile

  * @param pw

  * @param doc

  * @date 2008-11-24  17:31:06

  * @author zys

  * @return void

  */

public void ImportDataFromExcel(File excelFile,PrintWriter pw,Document doc)

{

        int excelRows;//行数

        int excelColumns;//列数

  Cell cell1;//单元格对象

  Workbook workbook;//Excel对象

  try 

  {

   workbook = Workbook.getWorkbook(excelFile);

 

   Sheet sheet=workbook.getSheet(0);

   excelRows=sheet.getRows();

   excelColumns=sheet.getColumns();

 

   //增加你的代码

   pw.println("<META HTTP-EQUIV='Pragma' CONTENT='no-cache'>");

   pw.println("<META HTTP-EQUIV=/"Content-Type/" content=/"text/html; charset=gb2312/">");

   pw.println("<LINK REL=stylesheet HREF=/"" + doc.getItemValueString("ResourcePath") + "css/global.css/" TYPE=/"text/css/">");

   pw.println("<link href=/"" + doc.getItemValueString("ResourcePath") + "css/viewStyle.css/" rel=/"stylesheet/" type=/"text/css/">");

 

   pw.println("<table width=95% align=center class=tableClass cellspan='5' cellpadding='2' border='1' id=outtable>");

   for(int i=0;i<excelRows;i++)

   {

    pw.println("<tr>");

    for(int j=0;j<excelColumns;j++)

    {

     cell1=sheet.getCell(j,i);//单元各对象

     pw.println("<td class='tdContent'>"+cell1.getContents()+"</td>");//单元格内容

    }

    pw.println("</tr>");

   }

   pw.println("</table>");

   //增加你的代码

 

   workbook.close();

 

  } catch (BiffException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  } catch (Exception e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

}

 

/**

  * 

  * @Description TODO 

  * @param doc

  * @param pw

  * @return

  * @date 2008-12-9  15:15:35

  * @author zys

  * @return InputStream

  */

public EmbeddedObject getEmbeddedObjectByDoc(Document doc,PrintWriter pw)

{

  EmbeddedObject excelEmbeddedObject=null;

        if (doc != null)

        {

         try 

         {

             if (doc.hasEmbedded()) 

 

    {

     Item it = doc.getFirstItem("$FILE");

 

     String AttachmentName= it.getValueString();

     EmbeddedObject obj = doc.getAttachment(AttachmentName);

     //System.out.println("it.getText()="+it.getText());

     if(isExcelFile(obj.getName()))

     {

      excelEmbeddedObject=obj;

      //if (obj != null) 

      //{

       //obj.extractFile(serverPath+currUser+AttachmentName);

       //excelFile=new File(serverPath+currUser+AttachmentName);

       //excelInputStream=obj.getInputStream();

 

      //}

 

     }

     else

     {

                  pw.println( "<script language='javascript'>");

                  pw.println( "alert('您上传的文件格式不是Excel文件,请重新导入.....');");

                  pw.println( "history.back();");

                  pw.println( "</script>");

 

     }

 

    }

    else

    {

     pw.println( "<script language='javascript'>");

     pw.println( "alert('您没有上传文件,请您重新选择导入.....');");

     pw.println( "history.back();");

     pw.println( "</script>");

    }

 

            } catch (NotesException e) {

     // TODO Auto-generated catch block

 

              e.printStackTrace();

    } 

        }

        return excelEmbeddedObject;

}

 

/**

  * 

  * @Description TODO 

  * @param doc

  * @param serverPath

  * @param currUser

  * @return

  * @date 2008-11-24  17:11:21

  * @author zys

  * @return File

  */

public File getFileByDoc(Document doc,String serverPath,String currUser,PrintWriter pw)

{

  File excelFile=null;

        if (doc != null)

        {

         try 

         {

             if (doc.hasEmbedded()) 

 

    {

     Item it = doc.getFirstItem("$FILE");

 

     String AttachmentName= it.getValueString();

     EmbeddedObject obj = doc.getAttachment(AttachmentName);

     //System.out.println("it.getText()="+it.getText());

     if(isExcelFile(obj.getName()))

     {

      if (obj != null) 

      {

       obj.extractFile(serverPath+currUser+AttachmentName);

       excelFile=new File(serverPath+currUser+AttachmentName);

 

      }

     }

     else

     {

                  pw.println( "<script language='javascript'>");

                  pw.println( "alert('您上传的文件格式不是Excel文件,请重新导入.....');");

                  pw.println( "history.back();");

                  pw.println( "</script>");

     }

     obj.recycle();

    }

    else

    {

     pw.println( "<script language='javascript'>");

     pw.println( "alert('您没有上传文件,请您重新选择导入.....');");

     pw.println( "history.back();");

     pw.println( "</script>");

    }

 

            } catch (NotesException e) {

     // TODO Auto-generated catch block

              e.printStackTrace();

    } 

        }

        return excelFile;

}

 

 

/**

  * 

  * @Description TODO 

  * @param serverPath

  * @param sType

  * @return

  * @date 2008-11-24  9:55:33

  * @author zys

  * @return String

  */

public String getServerPath(String serverPath,String sType)

{

  int serverPathLength=serverPath.length();

  String lastChar=serverPath.substring(serverPathLength-1,serverPathLength);

  if(sType.equals("UNIX")|| sType.equals("LINUX") || sType.equals("AIX/64"))

  {

   if (!lastChar.equals("/"))

   {

    return serverPath+ "/";

   }

  }

  else if(sType.equals("MACINTOSH"))

  {

   if (!lastChar.equals(":"))

   {

    return serverPath+ ":";

   }    

  }

  else  

  {

   if (!lastChar.equals("//"))

   {

    return serverPath+ "//";

   }

  }

  return serverPath;

}

/**

  * 

  * @Description TODO 

  * @param fileName

  * @return

  * @date 2008-11-24  11:04:38

  * @author zys

  * @return boolean

  */

public boolean isExcelFile(String fileName)

{

  boolean flag=true;

 

  if (fileName.lastIndexOf(".xls")<1)

  return false;

 

  int serverPathLength=fileName.length();

  String fileType=fileName.substring(serverPathLength-4,serverPathLength);

  //System.out.println(fileType);

  if (!fileType.equals(".xls"))

  {

   flag=false;

  }

  return flag;

}

 

 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值