コード例 #1
0
 /// <summary>
 /// 获取查询条件下所有数据
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 public MethodReturnResult <DataSet> GetAllData(LotMaterialListOutViewModel model)
 {
     using (LotMaterialListServiceClient client = new LotMaterialListServiceClient())
     {
         MaterialDataParameter p = new MaterialDataParameter()
         {
             ProductMaterialCode = model.ProductMaterialCode,
             BomMaterialCode     = model.BomMaterialCode,
             BomMaterialName     = model.BomMaterialName,
             OutPackageNo        = model.OutPackageNo,
             OutStartTime        = model.OutStartTime,
             OutEndTime          = model.OutEndTime,
             PageSize            = model.PageSize,
             PageNo = 0
         };
         MethodReturnResult <DataSet> result = client.GetRPTMaterialData(ref p);
         AllRecords = p.Records;
         if (result.Code == 0 && result.Data != null && result.Data.Tables.Count > 0)
         {
             return(result);
         }
         else
         {
             return(null);
         }
     }
 }
コード例 #2
0
        /// <summary>
        /// 导出批次物料出库数据到Excel
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task <ActionResult> ExportOutToExcel(LotMaterialListOutViewModel model)
        {
            DataTable dt = new DataTable();

            if (Resulted.Code == 0 && Resulted.Data != null && Resulted.Data.Tables.Count > 0)
            {
                dt = Resulted.Data.Tables[0];
            }
            else
            {
                using (LotMaterialListServiceClient client = new LotMaterialListServiceClient())
                {
                    MaterialDataParameter p = new MaterialDataParameter()
                    {
                        ProductMaterialCode = model.ProductMaterialCode,
                        BomMaterialCode     = model.BomMaterialCode,
                        BomMaterialName     = model.BomMaterialName,
                        OutPackageNo        = model.OutPackageNo,
                        OutStartTime        = model.OutStartTime,
                        OutEndTime          = model.OutEndTime,
                        PageSize            = model.PageSize,
                        PageNo = 0
                    };
                    await Task.Run(() =>
                    {
                        MethodReturnResult <DataSet> ds = client.GetRPTMaterialData(ref p);
                        dt = ds.Data.Tables[0];
                    });
                }
            }
            #region 导出到EXCEL
            string     filePath = System.Web.HttpContext.Current.Server.MapPath("~/LotMaterialOutData.xlsx");
            FileStream fs       = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read);
            //创建工作薄。
            IWorkbook wb = new XSSFWorkbook();
            //IWorkbook wb = new HSSFWorkbook();
            //设置EXCEL格式
            ICellStyle style = wb.CreateCellStyle();
            style.FillForegroundColor = 10;
            //有边框
            style.BorderBottom = BorderStyle.Thin;
            style.BorderLeft   = BorderStyle.Thin;
            style.BorderRight  = BorderStyle.Thin;
            style.BorderTop    = BorderStyle.Thin;
            IFont font = wb.CreateFont();
            font.Boldweight = 10;
            style.SetFont(font);

            ISheet ws = null;

            for (int j = 0; j < dt.Rows.Count; j++)
            {
                if (j == 0)
                {
                    ws = wb.CreateSheet();
                    IRow  row  = ws.CreateRow(0);
                    ICell cell = null;
                    #region //列名
                    foreach (DataColumn dc in dt.Columns)
                    {
                        cell           = row.CreateCell(row.Cells.Count);
                        cell.CellStyle = style;
                        cell.SetCellValue(dc.Caption);
                    }
                    #endregion
                    font.Boldweight = 5;
                }
                IRow rowData = ws.CreateRow(j + 1);
                #region //数据
                ICell cellData = null;
                foreach (DataColumn dc in dt.Columns)
                {
                    cellData           = rowData.CreateCell(rowData.Cells.Count);
                    cellData.CellStyle = style;

                    if (dc.DataType == typeof(double) || dc.DataType == typeof(float))
                    {
                        cellData.SetCellValue(Convert.ToDouble(dt.Rows[j][dc]));
                    }
                    else if (dc.DataType == typeof(int))
                    {
                        cellData.SetCellValue(Convert.ToInt32(dt.Rows[j][dc]));
                    }
                    else
                    {
                        cellData.SetCellValue(Convert.ToString(dt.Rows[j][dc]));
                    }
                }
                #endregion
            }
            var ms = new NpoiMemoryStream();
            ms.AllowClose = false;
            wb.Write(fs);
            wb.Write(ms);
            ms.Flush();
            ms.Position   = 0;
            ms.AllowClose = false;
            return(File(ms, "application/vnd.ms-excel", "LotMaterialOutData.xlsx"));

            #endregion
        }