下面的代码来自一个 winforms 应用程序,它在按钮事件上打开业务对象 6.5 的实例,刷新报表,然后将报表中的数据转储到 csv 文件中,然后退出业务对象实例。
我第一次运行代码时它工作得很好,但是如果我再次运行它,我会遇到异常
boApp.LoginAs(userName, Password, false, null);
抛出的异常是“无效对象”。
我假设这是因为 boApp 没有被重新初始化,问题是我缺乏关于静态类的知识。
调用方法是这样的:
BO_Control.RefreshBusinessObjects(boReportsFolder, boExportsFolder, boReportName, exportFileName, startDate, endDate);
这是 BO_Control 类
static class BO_Control
{
static busobj.Application boApp = new busobj.Application();
static busobj.Document testDoc;
public static void RefreshBusinessObjects(string reportFolder, string exportFolder ,string boReportName, string exportFileName, string startDate, string endDate)
{
DateTime BoStart = DateTime.Now;
boApp.LoginAs(userName, Password, false, null);
boApp.Interactive = false;
boApp.Visible = false;
GetData(reportFolder, boReportName, startDate, endDate);
ExportData(exportFolder, exportFileName);
Console.WriteLine("BO_Export took {0} seconds.", DateTime.Now.Subtract(BoStart));
boApp.Quit();
}
static busobj.Document GetData(string reportFolder, string reportName, string startDate, string endDate)
{
Console.WriteLine(reportFolder + reportName);
testDoc = (busobj.Document)boApp.Documents.Open(reportFolder + reportName, true, false, null, null);
//Report Start Date
testDoc.Variables[1].Value = startDate;
//Report End Date
testDoc.Variables[2].Value = endDate;
//Area. Needs to be a semi-colon delimited string
testDoc.Variables[3].Value = "L;B;H;";
testDoc.Refresh();
return testDoc;
}
static void ExportData(string exportFolder, string exportFileName)
{
testDoc.Reports.get_Item(1).ExportAsText(exportFolder + exportFileName);
//2 = DoNotSaveChanges
testDoc.Close(2);
}
}