前端代码:
//导出
function FileExcelOutClick(event) {
var data = {
"pageSize": F.ui.ddlPageSize.getValue(),
"pageIndex": F.ui.GridProductSupplier.pageIndex,
"productCode": F.ui.txtGridProductSupplierCode.getValue(),
"productName": F.ui.txtGridProductSupplierName.getValue(),
"isActive": F.ui.ddlActive.getValue() == "" ? -1 : F.ui.ddlActive.getValue(),
"isRelated": F.ui.ddlCorrespondingStatu.getValue() == "" ? -1 : F.ui.ddlCorrespondingStatu.getValue(),
"supplierCode": F.ui.ddlSupplier.getValue()
}
//调用角色信息接口,绑定数据到Grid
var blobData = http.product.supplier.ExportToExcel(JSON.stringify(data));
const blob = new Blob([blobData], { type: "application/vnd.ms-excel" });
var oa = document.createElement('a');
oa.href = URL.createObjectURL(blob);
oa.download = "供应商商品导出" + new Date().getTime() + ".xls";
document.body.appendChild(oa);
oa.click();
}
//ExportToExcel 表示ajax访问地址
http.product.supplier.ExportToExcel : function (data) {
return request.postRequest(this.url + 'api/product/ProductSupplier/ExportToExcel', data);
}
postRequest: function (url, data) {
var userToken = window.localStorage.getItem("oms_token");
var resultData;
$.ajax({
url: url,
enableAjaxLoading: true,
ajaxLoadingType: "mask",
data: data,
async: false,
type: "POST",
headers: {
'Content-type': 'application/json',
'Authorization': 'Bearer ' + userToken
},
success: function (result) {
if (result.message == null) {
resultData = result;
return resultData;
}
if (result.code == 1) {
resultData = result;
} else {
showNotify("操作失败,原因:" + result.message);
}
},
error: function (result) {
console.log(result)
showNotify("网络错误!");
}
});
return resultData;
}
后端代码:
/// <summary>
/// 根据搜索条件查询导出EXCEL
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[Route("ExportToExcel")]
public async Task<IActionResult> ExportToExcel([FromBody] GetProductSupplierInputDto input)
{
try
{
var data = await _ProductSupplierService.GetExportData(input);
string TH_HTML = "<th>{0}</th>";
string TD_HTML = "<td>{0}</td>";
StringBuilder sb = new StringBuilder();
sb.Append("<meta http-equiv=\"Content-Type\" content=\"application/vnd.ms-excel;charset=utf-8\"/>");
sb.Append("<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">");
sb.Append("<tr>");
sb.AppendFormat(TH_HTML, "");
sb.AppendFormat(TH_HTML, "供应商");
sb.AppendFormat(TH_HTML, "对应状态");
sb.AppendFormat(TH_HTML, "产品状态");
sb.AppendFormat(TH_HTML, "供应商产品编码");
sb.AppendFormat(TH_HTML, "供应商产品名称");
sb.AppendFormat(TH_HTML, "供应商产品类别");
sb.AppendFormat(TH_HTML, "供应商产品规格");
sb.AppendFormat(TH_HTML, "供应商产品单位");
sb.AppendFormat(TH_HTML, "生产厂家");
sb.Append("</tr>");
int rowIndex = 1;
foreach (ProductSupplierDto dto in data.Result)
{
sb.Append("<tr>");
sb.AppendFormat(TD_HTML, rowIndex++);
sb.AppendFormat(TD_HTML, dto.SupplierName);//供应商
sb.AppendFormat(TD_HTML, dto.RelatedName);//对应状态
sb.AppendFormat(TD_HTML, dto.ActiveName);//产品状态
sb.AppendFormat(TD_HTML, dto.ProductCode);//供应商产品编码
sb.AppendFormat(TD_HTML, dto.ProductName);//供应商产品名称
sb.AppendFormat(TD_HTML, dto.ProductClassName);//供应商产品类别
sb.AppendFormat(TD_HTML, dto.Specification);//供应商产品规格
sb.AppendFormat(TD_HTML, dto.UseUnit);//供应商产品单位
sb.AppendFormat(TD_HTML, dto.ManufactureName);//生产厂家
sb.Append("</tr>");
}
sb.Append("</table>");
return File(Encoding.UTF8.GetBytes(sb.ToString()), "application/vnd.ms-excel", "myexcel.xls");
}
catch (BusinessException e)
{
return Json(new { code = "0", e.Message });
}
catch (Exception e)
{
return Json(new { code = "0", e.Message });
}
}