Exemple #1
0
        /// <summary>
        /// 供货计划相关EXCEL导出
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public string SupplyPlanExportExcel(HttpContext context)
        {
            ///ENTITY_NAME
            string entityName = context.Request["ENTITY_NAME"];

            if (entityName.IndexOf("_") != -1)
            {
                entityName = entityName.Substring(0, entityName.IndexOf("_"));
            }
            if (HttpCommon.IsNullOrEmptyOrUndefined(entityName))
            {
                throw new Exception("MC:1x00000030");///参数获取错误
            }
            ///fileName
            string fileName = entityName + DateTime.Now.Ticks + ".xlsx";
            ///filePath
            string filePath = HttpContext.Current.Server.MapPath("/TEMP/EXPORTFILES");

            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            ///sort
            string orderText = string.Empty;

            if (!HttpCommon.IsNullOrEmptyOrUndefined(context.Request["sort"]))
            {
                orderText += context.Request["sort"];
                if (!orderText.Contains("_"))
                {
                    orderText = DataCommon.GetFieldName(orderText.Trim());
                }
            }
            ///order
            if (!HttpCommon.IsNullOrEmptyOrUndefined(context.Request["order"]))
            {
                orderText += " " + context.Request["order"];
            }
            ///FILTER
            string textWhere = context.Request["FILTER"];

            if (HttpCommon.IsNullOrEmptyOrUndefined(textWhere))
            {
                textWhere = string.Empty;
            }
            if (textWhere.Trim().ToUpper() == "AND")
            {
                textWhere = string.Empty;
            }
            ///创建标题行
            List <EntityFieldInfo> entityFieldInfos = new List <EntityFieldInfo>();

            switch (entityName)
            {
            ///供货计划
            case "SupplyPlan":
                entityFieldInfos = GetSupplyPlanFields(textWhere, entityName)
                                   .OrderBy(d => d.DisplayOrder.GetValueOrDefault())
                                   .OrderBy(d => d.ExportExcelOrder.GetValueOrDefault()).ToList(); break;

            ///缺件检查
            case "LackOfInspection":
                entityFieldInfos = GetSupplyPlanCheckFields(textWhere, entityName)
                                   .OrderBy(d => d.DisplayOrder.GetValueOrDefault())
                                   .OrderBy(d => d.ExportExcelOrder.GetValueOrDefault()).ToList(); break;
            }

            if (entityFieldInfos.Count == 0)
            {
                throw new Exception("MC:1x00000033");///未设置导出内容
            }
            ///CODE_ITEM下拉菜单集合
            Dictionary <string, Dictionary <string, string> > dicDropdowns = new Dictionary <string, Dictionary <string, string> >();
            Dictionary <string, string> columnNames = SYSHandler.GetExcelColumnNames(entityFieldInfos, ref dicDropdowns);
            ///page
            int pageIndex = 1;
            ///rows
            int maxRow = int.MaxValue;

            int dataTotal;
            ///获取数据
            DataTable dataTable = new DataTable();

            switch (entityName)
            {
            ///供货计划
            case "SupplyPlan": dataTable = GetSupplyPlanData(textWhere, orderText, pageIndex, maxRow, out dataTotal); break;

            ///缺件检查
            case "LackOfInspection": dataTable = GetLackOfInspectionData(textWhere, orderText, pageIndex, maxRow, out dataTotal); break;
            }

            if (dataTable.Rows.Count > 65535)
            {
                throw new Exception("MC:1x00000036");///导出数据不能多于65535条
            }
            ///NPOI
            NpoiHelper.TableToExcel(dataTable, columnNames, dicDropdowns, entityName, filePath + @"\" + fileName);
            ///
            return("../TEMP/EXPORTFILES/" + fileName);
        }