/// <summary> /// 导出采购单信息 /// </summary> /// <returns></returns> public IActionResult ExportPurchaseOrder(string searchPurchaseOrderModelStr) { try { var searchPurchaseOrderModel = new SearchPurchaseOrderModel(); searchPurchaseOrderModel = JsonConvert.DeserializeObject <SearchPurchaseOrderModel>(searchPurchaseOrderModelStr); //获取数据 var data = _purchasingService.GetExportPurchasingOrder(searchPurchaseOrderModel); string fileName = "采购订单-" + DateTime.Now.ToString("yyyyMMddHHmmss"); //设置Excel表头 Dictionary <string, string> columnNames = new Dictionary <string, string> { { "采购单号", "PurchasingNumber" }, { "采购计划单号", "PurchasingOrderNumber" }, { "原单号", "OrgionSerialNumber" }, { "下单日期", "CreatedTime" }, { "审核日期", "CheckTime" }, { "供应商", "SupplierName" }, { "订单状态", "StateStr" }, { "备注", "Mark" }, }; //是否导出采购单详情 if (searchPurchaseOrderModel.IsDetail) { columnNames.Add("采购商品名称", "PurchasingProductName"); columnNames.Add("采购商品编码", "PurchasingProductCode"); columnNames.Add("数量", "Quantity");; columnNames.Add("单价", "UnitPrice"); columnNames.Add("合计金额", "SumPrice"); } DataTable table = data.ToDataTable(columnNames); //导出.xls格式的文件 if (searchPurchaseOrderModel.ExportType == ".xls") { table.TableName = "Sheet1"; Stream stream = CommonTools.WriteExcel(table); return(File(stream, "application/vnd.ms-excel;charset=UTF-8", fileName + searchPurchaseOrderModel.ExportType)); } //导出.csv格式的文件 else if (searchPurchaseOrderModel.ExportType == ".csv") { StringBuilder sb = new StringBuilder(); sb.Append("采购单号,采购计划单号,原单号,下单日期,审核日期,供应商,订单状态,备注"); if (searchPurchaseOrderModel.IsDetail) { sb.Append(",采购商品名称,采购商品编码,数量,单价,合计金额"); } sb.Append("\r\n"); for (int i = 0; i < table.Rows.Count; i++) { sb.Append("\"" + "\t" + table.Rows[i]["采购单号"] + "\"" + ","); sb.Append("\"" + "\t" + table.Rows[i]["采购计划单号"] + "\"" + ","); sb.Append("\"" + "\t" + table.Rows[i]["原单号"] + "\"" + ","); sb.Append("\"" + "\t" + table.Rows[i]["下单日期"] + "\"" + ","); sb.Append("\"" + "\t" + table.Rows[i]["审核日期"] + "\"" + ","); sb.Append("\"" + "\t" + table.Rows[i]["供应商"] + "\"" + ","); sb.Append("\"" + "\t" + table.Rows[i]["订单状态"] + "\"" + ","); sb.Append("\"" + "\t" + table.Rows[i]["备注"] + "\""); if (searchPurchaseOrderModel.IsDetail) { sb.Append("," + "\"" + "\t" + table.Rows[i]["采购商品名称"] + "\"" + ","); sb.Append("\"" + "\t" + table.Rows[i]["采购商品编码"] + "\"" + ","); sb.Append("\"" + "\t" + table.Rows[i]["数量"] + "\"" + ","); sb.Append("\"" + "\t" + table.Rows[i]["单价"] + "\"" + ","); sb.Append("\"" + "\t" + table.Rows[i]["合计金额"] + "\""); } sb.Append("\r\n"); } MemoryStream stream = new MemoryStream(); StreamWriter writer = new StreamWriter(stream); writer.Write(sb.ToString()); writer.Flush(); stream.Position = 0; return(File(stream, "text/csv;charset=UTF-8", fileName + searchPurchaseOrderModel.ExportType)); } else { return(Json(new { Result = false, Message = "错误信息:导出失败" })); } } catch (Exception ex) { return(Json(new { Result = false, Message = "错误信息:" + ex.Message })); } }