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

Question

Question

Generate excel file via workflow

asked on April 20, 2022

I have a monthly report that is done via workflow.  An email is sent with a table in the body of the email (using a multi-value token).  Is there a way to have workflow attach an excel file instead or in addition to the body of the email?  

0 0

Replies

replied on April 20, 2022 Show version history

Matthew's suggestion is one way to go, and it is probably the easiest to manage/implement.

I have a method I use for sending Excel reports from Laserfiche via email for some of my processes, but it is much more involved and has a lot of moving parts.

Anyway, the way I accomplish this is as follows.

  1. Create a template for the Excel file with column headers and no other data
  2. Store it on the workflow server somewhere it can be accessed by the workflow service
  3. Create a copy with Template or something like that appended to the end (i.e. FileName and FileName - TEMPLATE)
  4. Create an ODBC connection on the server and have it pointed at the main file, not the template. Make sure read only is not checked (you'll need the Excel ODBC drivers installed)
  5. Add the ODBC entry for that file as a data source in Workflow

 

Once all of that is done, I do the following in the workflow:

  1. First, run a Script activity to overwrite the main file with the Template copy to wipe any data from previous instances (you could also do this at the end if you prefer)
  2. Use Insert Data in a loop using the ODBC data source to populate the spreadsheet
  3. Use an SDK Script activity to write the populated file to the Electronic File of an entry in the repository
  4. Attach that entry in the email activity.

 

You could create a new entry in the workflow and delete it when finished, reuse the same placeholder entry every time it runs, or save a separate copy each time; it all depends on preferences.

The Script code is as follows:

Overwrite file with template (i.e., reset the data)

string filePath = @"FILE PATH\FILE NAME - TEMPLATE.xlsx";
string copyPath = @"FILE PATH\FILE NAME.xlsx";
bool result = File.Exists(filePath);

if(result){
    File.Copy(filePath,copyPath,true);
}

SetTokenValue("File Found",result);

Attach file to entry (i.e., add eDoc)

string filePath = @"FILE PATH\FILE NAME.xlsx";
bool success = false;

if(File.Exists(filePath)){
    // Get and lock source document
    DocumentInfo doc = (DocumentInfo)this.BoundEntryInfo;
    doc.Lock(LockType.Exclusive);

    // Try to attach file
    try{
        DocumentImporter docImporter = new DocumentImporter();
        docImporter.OcrImages = false;
        docImporter.OverwritePages = true;
        docImporter.ExtractTextFromEdoc = false;
        docImporter.Document = doc;

        // Import spreadsheet
        docImporter.ImportEdoc("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",filePath);

        // Update document extension and save changes
        doc.Extension = ".xlsx";

        // Save changes
        doc.Save();

        success = true;
    }
    // Ensure document is always unlocked
    finally{
        // Release document
        doc.Unlock();
        doc.Dispose();

        // Set result token
        SetTokenValue("File Attached",success);
    }
}

The basic workflow would be something along the lines of this

 

Another important note: ODBC to Excel only allows one connection at a time, so you'll get an error if multiple workflows try to insert data to an Excel file at the same time.

3 0
replied on April 20, 2022

I've done this with text files and CSV files.  Using a Create Entry activity I create an entry in the repository, and then using an SDK Script activity I add the text to that entry.  Then it's pretty easy to include that entry as an attachment on the Email activity.  So it's doable.

If you wanted an actual Excel document instead of a text file like a txt or csv format, that could be trickier since the Excel file formats are more complex than basic text formats.

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

Sign in to reply to this post.