Generates an Excel binary or OOXML file and saves it as an attachment to a SharePoint list item.

public static void Save(this ExcelTemplate template, Microsoft.SharePoint.SPListItem listItem, string fileName)
Public Shared Sub Save(ByVal template As ExcelTemplate, ByVal listItem As Microsoft.SharePoint.SPListItem, ByVal fileName As String)

The current ExcelTemplate object which is being saved.

SharePoint List Item to which the file is attached

Name of the output file. ExcelWriter will save the file to the list item using this name.

Save will throw this exception if null (C#) or Nothing (VB.NET) is passed to the method.

ExcelWriter allows you to save in both the Excel 97-03 BIFF8 format (.xls) or the new Office Open XML (.xlsx) format. The template file must be of the expected output format. Hence, if you wish to output .xls files, you must start with a .xls template, and if you wish to output .xlsx files, you must start with a .xlsx template. ExcelWriter does not support the creation or editing of .xlsx files with the ExcelApplication object.

You can call Save more than once for a single instance of ExcelTemplate. This allows you to save more than one copy of a generated file, and/or both save the file on the server and stream it to the client.

This is an extension method for the ExcelTemplate object to be used for saving spreadsheets to SharePoint List Items. To use this method, you must add a reference to SoftArtisans.OfficeWriter.ExcelWriter.SharePointIntegration.dll

//--- Retrieve current AllowUnsafeUpdates value
bool currentAllowUnsafeUpdates = list.ParentWeb.AllowUnsafeUpdates;

//--- You must allow unsafe updates in order for the file to be saved in the
//--- List from a web application
list.ParentWeb.AllowUnsafeUpdates = true;
xlt.Save(listItem, "populated.xlsx");

//--- Update the list
listItem.Update();

//--- Return the AllowUnsafeUpdates value to its original state
list.ParentWeb.AllowUnsafeUpdates = currentAllowUnsafeUpdates;
        
'--- Retrieve current AllowUnsafeUpdates value
Dim currentAllowUnsafeUpdates As Boolean = list.ParentWeb.AllowUnsafeUpdates

'--- You must allow unsafe updates in order for the file to be saved in the
'--- List from a web application
list.ParentWeb.AllowUnsafeUpdates = True
xlt.Save(listItem, "populated.xlsx")

'--- Update the list
listItem.Update()

'--- Return the AllowUnsafeUpdates value to its original state
list.ParentWeb.AllowUnsafeUpdates = currentAllowUnsafeUpdates