public ActionResult ExportGoods(string id) { string strErrText; var request = HttpContext.Request; string strTypeId = request.QueryString["typeId"] ?? string.Empty; string strGoodsNo = request.QueryString["goodsNo"] ?? string.Empty; string strGoodsName = request.QueryString["goodsName"] ?? string.Empty; string strSpecModel = request.QueryString["specModel"] ?? string.Empty; string strGWeight = request.QueryString["gWeight"] ?? string.Empty; string strGrade = request.QueryString["grade"] ?? string.Empty; string strPacking = request.QueryString["packing"] ?? string.Empty; //读取货物数据 DDSystem dd = new DDSystem(); List<Goods> listGoods = dd.LoadAllGoodsByConditions(strTypeId, strGoodsNo, strGoodsName, strSpecModel, strGWeight, strGrade, strPacking, LoginAccountId, LoginStaffName, out strErrText); if (listGoods == null) { throw new Exception(strErrText); } //生成GridView BoundField colGoodsNo = new BoundField(); colGoodsNo.HeaderText = InnoSoft.LS.Resources.Labels.GoodsNo; colGoodsNo.DataField = "GoodsNo"; BoundField colName = new BoundField(); colName.HeaderText = InnoSoft.LS.Resources.Labels.GoodsName; colName.DataField = "Name"; BoundField colTypeId = new BoundField(); colTypeId.HeaderText = InnoSoft.LS.Resources.Labels.TypeId; colTypeId.DataField = "TypeId"; BoundField colTypeFullName = new BoundField(); colTypeFullName.HeaderText = InnoSoft.LS.Resources.Labels.TypeName; colTypeFullName.DataField = "TypeFullName"; BoundField colSpecModel = new BoundField(); colSpecModel.HeaderText = InnoSoft.LS.Resources.Labels.Specification; colSpecModel.DataField = "SpecModel"; BoundField colGWeight = new BoundField(); colGWeight.HeaderText = InnoSoft.LS.Resources.Labels.GrammeWeight; colGWeight.DataField = "GWeight"; BoundField colGrade = new BoundField(); colGrade.HeaderText = InnoSoft.LS.Resources.Labels.Grade; colGrade.DataField = "Grade"; BoundField colBrand = new BoundField(); colBrand.HeaderText = InnoSoft.LS.Resources.Labels.Brand; colBrand.DataField = "Brand"; BoundField colPieceWeight = new BoundField(); colPieceWeight.HeaderText = InnoSoft.LS.Resources.Labels.PieceWeightOrConvertCoefficient; colPieceWeight.DataField = "PieceWeight"; BoundField colPacking = new BoundField(); colPacking.HeaderText = InnoSoft.LS.Resources.Labels.PackingSpecification; colPacking.DataField = "Packing"; BoundField colRemark = new BoundField(); colRemark.HeaderText = InnoSoft.LS.Resources.Labels.Remark; colRemark.DataField = "Remark"; var grid = new GridView(); grid.Columns.Add(colGoodsNo); grid.Columns.Add(colName); grid.Columns.Add(colTypeId); grid.Columns.Add(colTypeFullName); grid.Columns.Add(colSpecModel); grid.Columns.Add(colGWeight); grid.Columns.Add(colGrade); grid.Columns.Add(colBrand); grid.Columns.Add(colPieceWeight); grid.Columns.Add(colPacking); grid.Columns.Add(colRemark); grid.AutoGenerateColumns = false; grid.DataSource = from g in listGoods select new { GoodsNo = g.GoodsNo, Name = g.Name, TypeId = g.TypeId, TypeFullName = g.TypeFullName, SpecModel = g.SpecModel, GWeight = g.GWeight, Grade = g.Grade, Brand = g.Brand, PieceWeight = g.PieceWeight, Packing = g.Packing, Remark = g.Remark }; grid.DataBind(); //导出GridView Response.ClearContent(); Response.Charset = InnoSoft.LS.Resources.Encoding.ExcelCharset; Response.ContentEncoding = System.Text.Encoding.GetEncoding(InnoSoft.LS.Resources.Encoding.ExcelContent); Response.ContentType = "application/ms-excel"; Response.Write("<meta http-equiv=Content-Type content=text/html charset=" + InnoSoft.LS.Resources.Encoding.ExcelCharset + ">"); Response.AddHeader("content-disposition", "attachment; filename=Goods.xls"); StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); grid.RenderControl(htw); Response.Write(sw.ToString()); Response.End(); return View("SetGoods"); }
public JsonResult LoadGoodsGrid(string sidx, string sord, int page, int rows, string typeId, string goodsNo, string goodsName, string specModel, string gWeight, string grade, string packing) { //读取货物数据 string strErrText; DDSystem dd = new DDSystem(); List<Goods> listGoods = dd.LoadAllGoodsByConditions(typeId, goodsNo, goodsName, specModel, gWeight, grade, packing, LoginAccountId, LoginStaffName, out strErrText); if (listGoods == null) { throw new Exception(strErrText); } //提取当前页面数据 int nTotalRows = listGoods.Count; int nPageIndex = page; int nPageSize = rows; int nTotalPages = nTotalRows / nPageSize; if (nTotalRows % nPageSize > 0) nTotalPages++; string sortExpression = (sidx ?? "Id") + " " + (sord ?? "ASC"); var data = listGoods.OrderBy(sortExpression).Skip((nPageIndex - 1) * nPageSize).Take(nPageSize).ToList(); //生成表格数据 var ret = new { total = nTotalPages, page = nPageIndex, records = nTotalRows, rows = ( from g in data select new { id = g.Id, cell = new string[] { g.Id.ToString(), g.GoodsNo, g.Name, g.TypeFullName, g.SpecModel, g.GWeight, g.Grade, g.Brand, g.PieceWeight == null || g.PieceWeight==string.Empty ? null : decimal.Parse(g.PieceWeight).ToString("#0.######"), g.Packing, g.Remark } }).ToArray() }; return Json(ret, JsonRequestBehavior.AllowGet); }