【问题标题】:Read folder paths from excel and create the folders in documentum using java从 excel 中读取文件夹路径并使用 java 在 documentum 中创建文件夹
【发布时间】:2014-05-05 09:25:58
【问题描述】:

我想从 "FolderPath" 列下的 Excel 工作表中读取和获取我的文件夹路径,并在名为 "Documentum" 的父文件夹下创建一个文件夹结构列表>。我附上了如下示例 excel 数据。我可以通过硬编码要创建的文件夹路径,从以下代码创建一个文件夹。例如,在这种情况下它是:/documentum.

文件夹路径

/documentum/folder1

/documentum/folder2

/documentum/folder3

/documentum/folder8/folder9

**

请建议,如何在下面的代码中添加此功能。我尝试使用 excel POI,但运气不佳,因为我在 POI 中工作不多,对以下代码的任何示例修改或建议都会有所帮助。

package com.documentum;

import com.documentum.fc.common.DfException;
import com.documentum.fc.common.DfId;
import com.documentum.fc.common.IDfId;
import com.documentum.fc.common.IDfLoginInfo;

import com.documentum.fc.client.IDfClient;
import com.documentum.fc.client.IDfFolder;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.client.IDfSessionManager;

import java.util.StringTokenizer;

import com.documentum.com.DfClientX;
import com.documentum.com.IDfClientX;

public class AutoFolderStructure
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        String username = "user";
        String password = "docu";
        String repoName = "documentum";
        String folderPath = "/documentum"; //folder path to be created

        IDfSessionManager sessMgr = null;
        IDfSession sess = null;
        try
        {
            sessMgr = createSessionManager();
            addIdentity(sessMgr,username,password,repoName);
            sess = sessMgr.getSession(repoName);
            IDfId newId = createFolder(sess,folderPath,null,null,null);
            System.out.println("Created folder: " + newId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            if((sessMgr != null) && (sess != null))
            {
                sessMgr.release(sess);
            }
        }

    }



    public static IDfId createFolder(
       IDfSession sess,
       String path,
       String pathSep,
       String type,
       String basePath)
       throws DfException
    {
       boolean cabinetStillToBeProcessed = true;

       if (pathSep == null || (pathSep.length() == 0))
       {
          pathSep = "/";
       }

       if ((type == null) || (type.length() == 0))
       {
          type = "dm_folder";
       }

       IDfId currentId = null;

       StringBuffer bufFldrPath = new StringBuffer(48);
       if ((basePath != null) && (basePath.length() > 1))
       {
          currentId = getIdByPath(sess, basePath);
          if (!currentId.isNull())
          {
             //base path actually exists.
             bufFldrPath.append(basePath);
             cabinetStillToBeProcessed = false; //cabinet already processed due to base path.

             int basePathLen = basePath.length();            
             if(basePathLen < path.length())
             {
                path = path.substring(basePath.length());
             }            
          }

       }

       StringTokenizer tokFldrNames = new StringTokenizer(path, pathSep);

       if (cabinetStillToBeProcessed)
       {
          //Execution will come here only if basePath was not specified or
          //if specified basePath was not valid.
          String cabinetName = tokFldrNames.nextToken();
          StringBuffer cabQual = new StringBuffer(32);
          cabQual.append("dm_cabinet where object_name='").append(
             cabinetName).append(
             "'");

          currentId = sess.getIdByQualification(cabQual.toString());
          if (currentId.isNull())
          {
             //need to create cabinet.
             IDfFolder cab = (IDfFolder) sess.newObject("dm_cabinet");
             cab.setObjectName(cabinetName);
             cab.save();
             currentId = cab.getObjectId();            
          }
          bufFldrPath.append(pathSep).append(cabinetName);
       }
       //By this point the bufFldrPath will either have the cabinet path
       //or it will have the basePath in it. 

       //now create all folders beyond the cabinet or basePath.
       while(tokFldrNames.hasMoreTokens())
       {         
          String parentPath = bufFldrPath.toString();

          String fldrName = tokFldrNames.nextToken();
          bufFldrPath.append(pathSep).append(fldrName);
          //by this point the buffer should contain the new expected path

          currentId = getIdByPath(sess,bufFldrPath.toString());
          if(currentId.isNull())
          {
             //looks like the new folder in the path does not exist.
             IDfFolder newFldr = (IDfFolder) sess.newObject(type);
             newFldr.setObjectName(fldrName);
             newFldr.link(parentPath);
             newFldr.save();
             currentId = newFldr.getObjectId();            
          }
          //by this point currentId should point to next folder in path                           
       }//while(all folder names)

       return currentId;
    }


    public static IDfId getIdByPath(IDfSession sess, String path)
       throws DfException
    {       

       int pathSepIndex = path.lastIndexOf('/');
       if (pathSepIndex == -1)
       {
          return new DfId("000");
       }

       StringBuffer bufQual = new StringBuffer(32);
       if (pathSepIndex == 0)
       {
          //its a cabinet path 
          bufQual.append(" dm_cabinet where object_name='");
          bufQual.append(path.substring(1));
          bufQual.append("'");
       }
       else
       {
          bufQual.append(" dm_sysobject where FOLDER('");
          bufQual.append(path.substring(0, pathSepIndex));
          bufQual.append("') ");
          bufQual.append(" and object_name='");
          bufQual.append(path.substring(pathSepIndex + 1));
          bufQual.append("'");
       }

       String strQual = bufQual.toString();
       IDfId id = sess.getIdByQualification(strQual);
       return id;
    }

    private static IDfSessionManager createSessionManager() throws DfException
    {
        IDfClientX clientX = new DfClientX();
        IDfClient localClient = clientX.getLocalClient();
        IDfSessionManager sessMgr = localClient.newSessionManager();
        return sessMgr;
    }


    private static void addIdentity(IDfSessionManager sm, String username,
            String password, String repoName) throws DfException
    {
        IDfClientX clientX = new DfClientX();

        IDfLoginInfo li = clientX.getLoginInfo();
        li.setUser(username);
        li.setPassword(password);

        // check if session manager already has an identity.
        // if yes, remove it.
        if (sm.hasIdentity(repoName))
        {
            sm.clearIdentity(repoName);
        }

        sm.setIdentity(repoName, li);
    }

}

感谢和问候 德布

【问题讨论】:

  • 听起来您在问如何从 Excel 中提取数据,但您已经知道如何执行 Documentum 部分(从路径创建文件夹)。那是对的吗?在这种情况下,将其标记为 Excel 或 Java 问题而不是 Documentum 可能会更好。
  • 是的,你是对的,我想在这里了解如何从 excel 中读取路径,迭代 excel 表中的列以获取文件夹路径并在 documentum 中创建文件夹。目前我可以在代码中创建硬编码的文件夹“/documentum”。任何关于在documentum中创建文件夹的excel读写建议都会有所帮助。

标签: java excel jakarta-ee documentum documentum6.5


【解决方案1】:

您可以使用 Apache POI 库。如果您正在运行自己的脚本,则需要将其添加到类路径中;如果您正在执行 documentum 作业(由 documentum JMS 执行),则需要将其添加到 java_methods 目录

【讨论】:

    【解决方案2】:

    我将在这里发布我使用 POI 的代码:

    首先:

    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    

    您可以从我的示例中提取我是如何使用它的:

    我用过:

    列名 1 |列名 2 |列名 3 |列名 4

    这里的数据

    代码: 行开始 = 1;意味着我从数据行开始逐行处理。 最后一列 = 4;表示我有 4 列。

    修复你发现的代码。

    // Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook;
            try
            {
                workbook = new XSSFWorkbook(file);
    
                // Get first/desired sheet from the workbook
                XSSFSheet sheet = workbook.getSheetAt(0);
    
                int rowStart = 1;
                int rowEnd = sheet.getLastRowNum();
    
                for (int rowNum = rowStart; rowNum < rowEnd; rowNum++)
                {
                    Row r = sheet.getRow(rowNum);
    
                    int lastColumn = 4;
                    int inputtype = 0;
                    table_no = "";
                    dql = "";
                    description = "";
                    table_code = "";
                    code = "";
    
                    for (int cn = 0; cn < lastColumn; cn++)
                    {
                        Cell cell = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
    
                        if (cell == null)
                        {
                            switch (inputtype)
                            {
                            case 0:
                                inputtype++;
                                code = "";
                                break;
                            case 1:
                                inputtype++;
                                table_no = "";
                                break;
    
                            case 2:
                                inputtype++;
                                table_code = "";
                                break;
                            case 3:
                                inputtype++;
                                description = "";
                                break;
    
                            default:
                                inputtype = 0;
                                break;
                            }
                        }
                        else
                        {
                            switch (inputtype)
                            {
                            case 0:
                                inputtype++;
                                if (cell.getCellType() == Cell.CELL_TYPE_STRING)
                                {
                                    code = cell.getStringCellValue();
                                }
                                else
                                {
                                    tmp = cell.getNumericCellValue();
                                    code = tmp.intValue() + "";
                                }
                                break;
    
                            case 1:
                                inputtype++;
                                if (cell.getCellType() == Cell.CELL_TYPE_STRING)
                                {
                                    table_no = cell.getStringCellValue();
                                }
                                else
                                {
                                    tmp = cell.getNumericCellValue();
                                    table_no = tmp.intValue() + "";
                                }
                                break;
    
                            case 2:
                                inputtype++;
                                if (cell.getCellType() == Cell.CELL_TYPE_STRING)
                                {
                                    table_code = cell.getStringCellValue().trim();
                                }
                                else
                                {
                                    tmp = cell.getNumericCellValue();
    
                                    table_code = tmp.intValue() + "";
                                }
                                break;
                            case 3:
                                inputtype++;
                                if (cell.getCellType() == Cell.CELL_TYPE_STRING)
                                {
                                    description = cell.getStringCellValue();
                                }
                                else
                                {
                                    tmp = cell.getNumericCellValue();
    
                                    description = tmp.intValue() + "";
                                }
                                break;
                            default:
                                inputtype = 0;
                                break;
                            }
                        }
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-25
      • 2023-02-15
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      相关资源
      最近更新 更多