简体   繁体   中英

Adding a table to an existing excel file with spreadsheetgear

I use Spreadsheetgear to export the results of custom SQL queries as excel files.

Now I want to improve this system: The user will be able to upload an excel template file into the database (currently as varbinary). For example, it could have one worksheet with calculations, then when exporting data into that template it'll fill a different worksheet with the datatable from the query.

Can spreadsheetgear do this? If so, how does it work - mainly how can I load an existing excel file as a Spreadsheetgear workbook/workbookset? I could not find anything in their documentation (though I am still looking).

Edit: Solved. I create the workbook manually, load the template from the database as a byte[], then open said template with the OpenFromMemory function:


// Create workbookSet
SpreadsheetGear.IWorkbookSet workbookSet = SpreadsheetGear.Factory.GetWorkbookSet();
// Create a new empty workbook in the workbookSet.
SpreadsheetGear.IWorkbook workbook = workbookSet.Workbooks.Add();

if(TemplateID != -1) // If this case requires a template
{
    // Get template from SQL database (.xlsx stored as varbinary(max))
    byte[] template = GetTemplateByID(VorlagenID);
    workbook = workbookSet.Workbooks.OpenFromMemory(template);
}

// Create export worksheet
SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets[0];
worksheet.Name = "Export";
[...]

Templates always use the Worksheet[1] in my case, but it should be easy to create a Worksheet[1] for the export.

Yes it is possible to set up pre-constructed template files using SpreadsheetGear. We use this extensively using .NET / C# / MSSSQL. The method allows you to create quite sophisticated templates and then simply add the required data. This of course includes any calculations you build into the template.

Method 1 - Store the template on a webserver, extract and write the created user spreadsheet to a folder on the web server. Return the filename to allow extraction by code or by the user from the server.

        public static String SaveTemplateSpreadsheetToServer()
        {
            // Open the workbook.
            var templatename = HostingEnvironment.MapPath("~/Files/MyTemplate.xlsx");

            var workbook = Factory.GetWorkbook(templatename );

            // Read and write to the spreadsheet

            // Save a copy to disk and return filename       
            var filename = "The_exported_file.xlsx";
            var filePath = HostingEnvironment.MapPath("~/FilesTemp/" + filename);
            workbook.SaveAs(filePath, FileFormat.OpenXMLWorkbook);

             // close workbook
             workbook.Close();

            // Return the filename 
            return fileName;
        }

Method 2: Store the template on a webserver, extract and save modified spreadsheet as a byte array. Download directly an attachment

        public static byte[] SaveTemplateSpreadsheetToServer()
        {
            // Open the workbook.
            var templatename = HostingEnvironment.MapPath("~/Files/MyTemplate.xlsx");

            var workbook = Factory.GetWorkbook(templatename );

            // Read and write to the spreadsheet

            // Save as byte array and send to user
            var byteArray = workbook.SaveToMemory(FileFormat.OpenXMLWorkbook);

            // close workbook
            workbook.Close();

            // Return the byte array
            return byteArray;
        }

We have done some work with binary template files saved in a database but find it more convenient to work with physical template files on a web server. It is easier to manage changes to the template.

My only caution is to avoid working with very big templates that have lots of "junk" in them (eg images). The process becomes affected by the time it takes to load the file into memory prior to the read / write / export activity. Less than 1MB is ideal and less than 2MB is manageable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM