本文介绍了WebApi使用EF EPPlus在exe中返回乱码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在我的EF WebApi中使用EPPlus来返回Excel工作簿中的数据。
对网络服务器的调用已成功完成,但是当我的浏览器打开excel文档时...所有内容都是乱码。
I am trying to use EPPlus in my EF WebApi to return data in a excel workbook.The call to webserver gets made successfully, but when my browser opens the excel doc... all the content is gibberish.
这是我的webapi
This is my webapi
private xxxV003Context db = new xxxV003Context();
[HttpGet]
public HttpResponseMessage Get()
{
HttpResponseMessage response;
response = Request.CreateResponse(HttpStatusCode.OK);
MediaTypeHeaderValue mediaType = new MediaTypeHeaderValue("application/octet-stream");
//MediaTypeHeaderValue mediaType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.Content = new StreamContent(ExcelSheet());
response.Content.Headers.ContentType = mediaType;
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "Orders.xlsx";
return response;
}
public List<Client> Orders()
{
List<Client> tList = new List<Client>();
using (db)
{
tList.Add(new Client { ClientID = 50, ClientName = "testName1" });
tList.Add(new Client { ClientID = 51, ClientName = "testName2" });
}
return tList;
}
public MemoryStream ExcelSheet()
{
using (var package = new ExcelPackage())
{
var worksheet = package.Workbook.Worksheets.Add("Orders");
worksheet.Cells["A1"].LoadFromCollection(Orders(), false);
package.Save();
var stream = new MemoryStream(package.GetAsByteArray());
return stream;
}
}
这是屏幕截图
here is the screenshot
新错误消息
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<logFileName>error125800_03.xml</logFileName>
<summary>Errors were detected in file 'C:\Users\xxx\Downloads\Orders (2).xlsx'</summary>
-<additionalInfo>
<info>Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.</info>
</additionalInfo>
</recoveryLog>
推荐答案
EPPlus创建.xlsx文件-将文件扩展名从 .xls
到 .xlsx
EPPlus creates .xlsx files - Change the file extension from .xls
to .xlsx
这篇关于WebApi使用EF EPPlus在exe中返回乱码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!