You are viewing limited content. For full access, please sign in.

Question

Question

Export data in Excel (XLS) format using the SDK Script

SDK
asked on December 18, 2016 Show version history

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();
            }
        }

 

0 0

Answer

SELECTED ANSWER
replied on December 19, 2016 Show version history

Yogesh,

Just to add to Robert's list, I was looking for something easier to work with as I had all kinds of issues with using Interop.Excel, having to have the same version on the Workflow Server, exporting issues, you name it...I stumbled upon a program called EPPlus (Version 4.1.0), it's free, it's much easier to use, and you don't even have to HAVE Excel on the server to use it.  It has saved me a lot of headaches:
http://epplus.codeplex.com/

Just add the .dll to your references in the SDK.

0 0
replied on December 22, 2016 Show version history

Thanks Rick. Have decided to go with EPPlus now that our customer has agreed for XLSX instead of XLS. Their original requirement was XLS. 

Once again, thanks to both Robert and yourself for posting. Much appreciated. 

0 0
replied on October 29, 2019

Im new in the Laserfiche, may i know how to implement this solution in my current workflow

 

0 0

Replies

replied on December 19, 2016

Workflow is running as a Windows service, and Excel is a desktop application, so Excel is probably failing because it needs to be run in an interactive session. Try using a different library to export the file, for example ClosedXML or OpenXML PowerTools (which is what Laserfiche Web Access uses to export Excel files)

0 0
replied on December 22, 2016 Show version history

Thanks Robert, your comment regarding interactive session makes sense. BTW, your Empower 2017 Advanced Topics in SDK Programming class seems very popular, its full and I'm on a waiting list - any chance you can schedule a bigger room :) lol

0 0
replied on January 8, 2024

Hello Robert - sorry to necro this old thread, but do you have a sample workflow with an example of the sdk using ClosedXML?

 

I am very new to using the SDK and I assume I am missing several complications , like getting the right packages/nugets installed before this can work. any and all advice is greatly appreciated.

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.