cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to export a excle file to local path?

KelvinXYLiao
Explorer
0 Likes
518

Dear All,

The business user uploaded a CSV file to BO, and now I am required to use JAVA code to call SAP SDK to export it to the local directory of the server. My Java code has already connected to CMS and found the corresponding infoObject May I ask what should be done next? Which SDK should I use? Which method should be used to export files? Do everyone have any ideas? Thank you in advance.
My BO version is SAP BusinessObjects BI Platform 4.2 Support Pack 8 Patch 2
Version: 14.2.8.3503
Here is my code:

package com.chb.ods;

import com.crystaldecisions.sdk.exception.SDKException;
import com.crystaldecisions.sdk.framework.CrystalEnterprise;
import com.crystaldecisions.sdk.framework.IEnterpriseSession;
import com.crystaldecisions.sdk.framework.ISessionMgr;
import com.crystaldecisions.sdk.occa.infostore.*;
import com.crystaldecisions.sdk.plugin.desktop.excel.IExcel;
import com.crystaldecisions.sdk.plugin.desktop.report.IReport;

import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class FileUploadApp {

private static final Logger logger = Logger.getLogger(FileUploadApp.class.getName());
public static void main(String[] args) {
try {
// 配置日志文件
FileHandler fileHandler = new FileHandler("D:\\JAVADoc\\Code\\FileUploaddemo5\\downloadFileFromBO.log", true);
fileHandler.setFormatter(new SimpleFormatter());
logger.addHandler(fileHandler);
logger.setLevel(Level.ALL);

// 重定向System.outSystem.err到日志文件
// PrintStream printStream = new PrintStream(new FileOutputStream("D:\\JAVADoc\\Code\\FileUploaddemo2\\downloadFileFromBO.log", true));
// System.setOut(printStream);
// System.setErr(printStream);

// 1. 登录到CMS
//System.out.println("logon start!");
logger.info("logon start!");
ISessionMgr sessionMgr = CrystalEnterprise.getSessionMgr();
IEnterpriseSession enterpriseSession = sessionMgr.logon("Administrator", "Abcd1234", "ODSBOUAT01:6400", "secEnterprise");
logger.info("logon completed!");

// 2. 获取InfoStore服务
IInfoStore infoStore = (IInfoStore) enterpriseSession.getService("InfoStore");
logger.info("getInfoStore completed!");

// 3. 查询报表对象
String query = "SELECT SI_ID, SI_NAME, SI_INSTANCE,SI_KIND FROM CI_INFOOBJECTS WHERE SI_NAME='cif093pf.381086.20240830.dat'";
IInfoObjects infoObjects = infoStore.query(query);
logger.info("query completed!");

// 4. 下载报表到本地
// byte[] reportContent = fileObject.getContent();
// String filePath = "/odsetl/APP/fileupload/MyReport.csv";
// try (OutputStream outputStream = new FileOutputStream(filePath)) {
// outputStream.write(reportContent);
// logger.info("Report downloaded successfully to: " + filePath);
// }
// IFiles[] files = fileObject.getFiles();
// if (files.length > 0) {
// InputStream inputStream = files[0].getLocale().getInputStream();
// String filePath = "/odsetl/APP/fileupload/MyReport.txt";
// try (OutputStream outputStream = new FileOutputStream(filePath)) {
// byte[] buffer = new byte[4096];
// int bytesRead;
// while ((bytesRead = inputStream.read(buffer)) != -1) {
// outputStream.write(buffer, 0, bytesRead);
// }
// logger.info("File downloaded successfully to: " + filePath);
// } finally {
// inputStream.close();
// }
// }
} else {
logger.info("Report not found.");
}
logger.info("douwnload completed!");

// 5. 注销
enterpriseSession.logoff();
} catch (SDKException e) {
logger.info("SDKException occurred: " + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
logger.info("Exception occurred: " + e.getMessage());
e.printStackTrace();
}
}
}

 

 

Accepted Solutions (0)

Answers (1)

Answers (1)

ayman_salem
Active Contributor

it is not clear why you need to export the CSV file again to the server's local directory!!??
If the user uploaded it, it will be located on the server in the input filestore directory.

KelvinXYLiao
Explorer
0 Likes
In our environment, the files uploaded by users are stored in a dedicated file server, and the path for storing the files is automatically generated by the server. This path is complex and follows no discernible pattern, making it very difficult to retrieve the files using a program. Therefore, we want to handle the file export ourselves and specify the path where the exported files are stored, so as to facilitate further processing of the files by our program. Thanks.