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?
Question
Question
Replies
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.
- Create a template for the Excel file with column headers and no other data
- Store it on the workflow server somewhere it can be accessed by the workflow service
- Create a copy with Template or something like that appended to the end (i.e. FileName and FileName - TEMPLATE)
- 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)
- 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:
- 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)
- Use Insert Data in a loop using the ODBC data source to populate the spreadsheet
- Use an SDK Script activity to write the populated file to the Electronic File of an entry in the repository
- 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.
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.