Articles in this section
Category / Section

How to avoid file corruption while opening the generated file using XlsIO in .NET MVC ?

2 mins read

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 how to set ExcelHttpContentType while saving .xlsx format file using a 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 
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 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 file version
workbook.SaveAs(filename, HttpContext.ApplicationInstance.Response, ExcelDownloadType.Open, ExcelHttpContentType.Excel2010)


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.

You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.

If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.

If you have any queries or require clarification, please let us know in the comments below or contact us through our support forumsSupport Tickets, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied