我想从“FolderPath”列下的Excel工作表中读取并获取文件夹路径,并在名为“Documentum”的父文件夹下创建文件夹结构列表。我附上了样本excel数据,如下所示。我可以通过硬编码要创建的文件夹路径从下面的代码创建一个文件夹。对于Eg,在这种情况下,它是: / documentum。
FOLDERPATH
/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);
}
}
谢谢和问候 DEB
答案 0 :(得分:0)
您可以使用Apache POI库。如果您正在运行自己的脚本,则需要将其添加到类路径中;如果您正在执行documentum作业(由documentum JMS执行),则需要将其添加到java_methods目录中
答案 1 :(得分:0)
我将在这里发布我的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;
您可以从我的示例中提取我如何使用它:
我用过:
代码: rowStart = 1;意味着我逐行从数据行开始处理。 lastColumn = 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;
}
}
}