Export excel.xls and .xlsx EPPlus.NET

I have code that works fine when I install the extension of the output file, for example, ".xlsx", but when I change it to ".xls", I have a window before opening the file in MSExcel, this file is incorrect ( the file format is incorrect) and then a lot of incorrect encoding characters (e.g. Japanese, etc.).

Has anyone had this problem? Is there a solution?

_currentContext.Response.Clear(); _currentContext.Response.ClearContent(); _currentContext.Response.ClearHeaders(); _currentContext.Response.AddHeader("content-disposition", "attachment; filename=FileName.xlsx"); _currentContext.Response.ContentEncoding = System.Text.Encoding.UTF8; _currentContext.Response.ContentType = "application/ms-excel"; _currentContext.Response.AddHeader("Content-Transfer-Encoding", "binary"); _currentContext.Response.BinaryWrite(_package.GetAsByteArray()); _currentContext.Response.Flush(); _currentContext.Response.End(); 

One moment - when I export it to xls on my local computer, everything works fine. When I try to use it on a remote server, I can correctly export it only to the xlsx extension.

+4
source share
2 answers

This is because AFAIK EPPlus can only export XLSX (OpenXML-Format Excel 2007 and higher) ... whils XLS is an old binary Excel-Formt, so Excel correctly says that something is wrong with the format ...

EDIT - Regarding the possible MIME types, try the following:

 application/vnd.ms-excel application/x-msexcel application/ms-excel application/msexcel application/x-excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet 
+4
source

it requests the wrong file extension. but although .xls is the correct extension, do not worry, click YES. Your file opens smoothly, without damage.

 //Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.ContentType = "application/x-msexcel"; 
0
source

Source: https://habr.com/ru/post/1382029/


All Articles