How to avoid file corruption while opening the generated file using XlsIO in .NET MVC web application ?
XlsIO generated files will be downloaded in .xls format in some browsers by default, even when we add the extension in the file name or set the version for the file. Because of this the downloaded file gets corrupted. To overcome this, set ExcelHttpContentType while saving the file in XlsIO.
Below code snippet demonstrates on how to set ExcelHttpContentType while saving .xlsx format file using XlsIO.
C#
// Step 1 : Instantiate the spreadsheet creation engine. ExcelEngine excelEngine = new ExcelEngine(); // Step 2 : Instantiate the Excel application object. IApplication application = excelEngine.Excel; // Creating the workbook with one worksheet. IWorkbook workbook = application.Workbooks.Create(1); // The first worksheet object in the worksheets collection is accessed. IWorksheet sheet = workbook.Worksheets[0]; // Workbook version is set workbook.Version = ExcelVersion.Excel2010; string filename = "Sample.xlsx"; // `ExcelHttpContentType` is set as the file version. workbook.SaveAs(filename, HttpContext.ApplicationInstance.Response, ExcelDownloadType.Open, ExcelHttpContentType.Excel2010);
VB
'Step 1 : Instantiate the spreadsheet creation engine. Dim excelEngine As ExcelEngine = New ExcelEngine() 'Step 2 : Instantiate the Excel application object. Dim application As IApplication = excelEngine.Excel 'Creating the workbook with Dim workbook As IWorkbook = application.Workbooks.Create(1) 'The first worksheet object in the worksheets collection is accessed. Dim sheet As IWorksheet = workbook.Worksheets(0) 'Workbook version is set workbook.Version = ExcelVersion.Excel2010 Dim filename As String = "Sample.xlsx" `ExcelHttpContentType` is set as the file version. workbook.SaveAs(filename, HttpContext.ApplicationInstance.Response, ExcelDownloadType.Open, ExcelHttpContentType.Excel2010)
The sample illustrating this can be downloaded here.
Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about how to avoid file corruption while opening the generated file using XlsIO in .NET MVC web application.
You can refer to our .NET MVC XlsIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our .NET MVC XlsIO example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!