.net core Excel 下载接口
程序员文章站
2024-01-21 14:44:40
...
下载Excel接口
- Controller层
[Area("Api")]
public class DownloadExcelController : Controller
{
public ActionResult DownloadFile(int categoryID,string openID)
{
IH5Service m_H5Service = IocProxyFactory.GetService<IH5Service>();
try
{
var ret = m_H5Service.Download(categoryID,openID);
string fileName = DateTime.Today.ToString("yyyy-MM-dd") + categoryEntity.CategoryName + ".xls";
HttpContext.Response.Headers.Append("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8).ToString());
HttpContext.Response.ContentType = "application/ms-excel";
HttpContext.Response.WriteAsync(ret);
return RedirectToAction("ret");
}
catch (Exception ex)
{
LogHelper.Info("Excel_Error:" + ex.ToString());
return Json("下载失败");
}
}
}
- service层
public string Download(int categoryID,string openID)
{
ICategoryRelationshipDao m_CategoryRelationshipDao = IocProxyFactory.GetService<ICategoryRelationshipDao>();
var userInfo = CloudHillPaasHandler.Instance.MemberHandler.GetUserInfo(openID);
if (!userInfo.Success)
{
return userInfo.Message;
}
var memberCode = userInfo.Data.MemberCode;
var orderList = m_CategoryOrderRelationshipDao.GetListAll(i => i.CategoryOrderRelationshipID,
i => i.CategoryID == categoryID &&
i.MemberCode == memberCode &&
i.Enable == 1, true);
if (orderList.Count ==0)
{
throw new Exception("暂无该订单信息");
}
var provinceIDs = orderList.Select(i => i.ProvinceID).ToList();
var goodIDs = orderList.Select(i => i.GoodID).ToList();
var provinceList = m_ProvinceDao.GetListAll(i => i.ProvinceID, i => provinceIDs.Contains(i.ProvinceID) && i.Enable == 1, true);
if (provinceList.Count == 0)
{
throw new Exception("暂无该省信息");
}
var goodList = m_CategoryRelationshipDao.GetListAll(i => i.CategoryRelationshipID,
i => goodIDs.Contains(i.GoodID) &&
i.CategoryID == categoryID &&
i.Enable == 1, true);
if (goodList.Count ==0)
{
throw new Exception("暂无该城市信息");
}
var categoryRelationshipIDs = goodList.Select(i => i.CategoryRelationshipID).ToList();
var goodDetailList = m_CadaDataDetailDao.GetListAll(i => i.CadaDataDetailID,
i => categoryRelationshipIDs.Contains(i.CategoryRelationshipID) &&
i.Enable == 1, true);
if (goodDetailList.Count == 0)
{
throw new Exception("暂无该城市数据信息");
}
var newList = SerializeHelper.SerializeToList(goodList);
for (int i = 0; i < newList.Count; i++)
{
var _d = newList[i];
var provinceID = Convert.ToInt32(_d["ProvinID"]);
var goodID = Convert.ToInt32(_d["GoodID"]);
var categoryRelationshipID = Convert.ToInt32(_d["CategoryRelationshipID"]);
var provinceEntity = provinceList.SingleOrDefault(i => i.ProvinceID == provinceID);
_d["Province"] = provinceEntity != null ? provinceEntity.Province : string.Empty;
}
var context = new TemplateContext()
{
String = FileHelper.ReadFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Export/Download.xml"), Encoding.UTF8),
TemplateType = "preview",
Source = new
{
List = H5Service.DicToInvariantCultureIgnoreCase(newList),
Length = newList.Count() + 1
}
};
TemplateEngine.Current.Run(context);
return context.Result();
}
public static IList<IDictionary<string, object>> DicToInvariantCultureIgnoreCase(IList<IDictionary<string, object>> dicList)
{
var newList = new List<IDictionary<string, object>>();
foreach (var i in dicList)
{
var newDic = new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase);
foreach (var y in i)
{
newDic.Add(y.Key, y.Value);
}
newList.Add(newDic);
}
return newList;
}