.net接口导出excel文件
作者:互联网
/// <summary>
/// 导出机构管理列表
/// </summary>
[HttpGet]
[Route("api/schoolManage/ExportSchoolManageList")]
public HttpResponseMessage ExportSchoolManageList([FromUri]ReqGetSchoolList req)
{
//获取要导出的数据
var baseData = SchoolManageService.GetSchoolManageList(req.FullName, req.IsUsed, req.Phone, req.Union);
//导出excel表头
var headers = new List<string>
{
TopOnlineConst.SchoolManageList.SchoolName,
TopOnlineConst.SchoolManageList.SchoolAddress,
TopOnlineConst.SchoolManageList.Phone,
TopOnlineConst.SchoolManageList.ArrangeState,
TopOnlineConst.SchoolManageList.ArrangeTime,
TopOnlineConst.SchoolManageList.RemainBuyAccount,
TopOnlineConst.SchoolManageList.Union,
TopOnlineConst.SchoolManageList.WebDomain,
TopOnlineConst.SchoolManageList.BackDomain,
TopOnlineConst.SchoolManageList.IsUsed,
};
var stream = EPExcelHelper.DataToExcelStream(baseData, headers);
var browser = String.Empty;
if (HttpContext.Current.Request.UserAgent != null)
{
browser = HttpContext.Current.Request.UserAgent.ToUpper();
}
try
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue(CommonConst.ResponseConfigure.ContentTypeExcel);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue(CommonConst.ResponseConfigure.Attachment)
{
FileName =
browser.Contains(CommonConst.Firefox)
? Path.GetFileName(TopOnlineConst.ExportFileName.PlatformSchool_ExcelName)
: HttpUtility.UrlEncode(Path.GetFileName(TopOnlineConst.ExportFileName.PlatformSchool_ExcelName))
};
return response;
}
catch
{
return new HttpResponseMessage(HttpStatusCode.NoContent);
}
}
/// <summary>
/// 带标题
/// 将列表数据转换为MemoryStream
/// 此方法是按照行写入的
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="entitys">泛型列表</param>
/// <param name="heads">表头</param>
/// <returns>MemoryStream</returns>
public static MemoryStream DataToExcelStream<T>(this List<T> entitys, List<string> heads)
{
return DataToExcelStream(entitys, heads, null);
}
标签:req,excel,TopOnlineConst,导出,SchoolManageList,new,net,HttpResponseMessage,respons 来源: https://www.cnblogs.com/Cinderellablog/p/12201615.html