Hello, one of our client's requirement is that we export data out in XLS format. I have the below unit test code where I am attempting to save an xls file to a temp location from a Workflow script. In reality, I will be iterating tokens but just for initial setup, I am using some hard coded values. When I run the Excel code in a Visual Studio console app (running on this same Workflow server), all is fine, the excel file is created without issues. However, when I embed this into a LF Script activity (once again, on the same server), I get the below exception.
19/12/2016 5:44:59 PM Excel Test Exception from HRESULT: 0x800A03EC
This happens during the SaveAs line, because when I comment this line out, the workflow runs without any exceptions or warnings.
I tried to create a text file to the same location, and this works, so folder security isn't the issue here. Once again, all my tests have been on the same server (box). I have Office 2013 (32bit) installed on this machine.
What really gets me is that this exact same bit of code works fine inside a Console App, but not from a workflow SDK script. I am happy to try any alternate methods of producing an XLS file if anyone has any other suggestions. Thanks.
using Excel = Microsoft.Office.Interop.Excel;
:
:
protected override void Execute()
{
// Write your code here.
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
//add data
xlWorkSheet.Cells[1, 1] = "Test 1";
xlWorkSheet.Cells[1, 2] = "Test 2";
xlWorkSheet.Cells[1, 3] = "Test 3";
xlWorkSheet.Cells[1, 4] = "Test 4";
xlWorkBook.SaveAs(@"c:\temp\test.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
private static void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
}
finally
{
GC.Collect();
}
}