分期批量导出报表(仅用作记录)

栏目:云星空知识作者:金蝶来源:金蝶云社区发布:2024-09-16浏览:1

分期批量导出报表(仅用作记录)

using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts.Report;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Model;
using Kingdee.BOS.Model.ReportFilter;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.NotePrint;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Report.PlugIn;
using Kingdee.BOS.Util;
using Kingdee.BOS;
using Kingdee.BOS.Core.Enums;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Resource;
using Kingdee.K3.FIN.App.Core;
using Kingdee.K3.FIN.CB.App.Report;
using Kingdee.K3.FIN.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Kingdee.BOS.ServiceHelper.Excel;
namespace CBEXPAND
{
    public class EveryMonthExport : AbstractSysReportPlugIn
    {
        [Kingdee.BOS.Util.HotUpdate]
        [System.ComponentModel.Description("产品成本还原对比分析批量导出")]
        public override void BarItemClick(BarItemClickEventArgs e)
        {
            if (!string.Equals(e.BarItemKey, "tb_ExportMultiFile", StringComparison.OrdinalIgnoreCase))
                return;
            var filter = this.SysReportView.Model.FilterParameter;
            var filterconlumn = this.SysReportModel.FilterParameter.ColumnInfo;//获取显示隐藏列信息
            List<String> name = new List<string>();
            foreach (var dd in filterconlumn)
            {
                name.Add(dd.Caption.ToString());
            }
            ReportHeader headerfinal = new ReportHeader();
            foreach (var tt in filterconlumn)
            {
                headerfinal.AddChild(tt.FieldName, tt.Caption);
            }
            
            List<ListHeader> headersfinal = new List<ListHeader>();
            foreach (var child in headerfinal.GetChilds())
            {
                if (child.GetChildCount() == 0)
                {
                    headersfinal.Add(child);
                }
                else
                {
                    foreach (var childHeader in child.GetChilds())
                    {
                        headersfinal.Add(childHeader);
                    }
                }
            }
            ReportHeader header = this.SysReportView.Model.GetReportHeader();
            List<ListHeader> headers = new List<ListHeader>();
            foreach (var child in header.GetChilds())
            {
                if (child.GetChildCount() == 0)
                {
                    headers.Add(child);
                }
                else
                {
                    foreach (var childHeader in child.GetChilds())
                {
                    headers.Add(childHeader);
                }
                }
            }//获取所有列信息
            List<string> columnsNames = headers.Select((ListHeader p) => p.Caption.ToString()).ToList();//获取所有列名
            string schemeid = this.SysReportModel.FilterParameter.SchemeEntity.SchemeID;//获取账表过滤放id
            int Year = (int)Convert.ToInt16(filter.CustomFilter["Year"]);
            int Period = (int)Convert.ToInt16(filter.CustomFilter["Period"]);
            int Endyear = (int)Convert.ToInt16(filter.CustomFilter["EndYear"]);
            int EndPeriod = (int)Convert.ToInt16(filter.CustomFilter["EndPeriod"]);
            string Reportformid = this.View.BillBusinessInfo.GetForm().Id;//获取账表formID
            string ReportFiltername = this.View.BillBusinessInfo.GetForm().FilterObject;//获取账表过滤条件formID
            string reportname = SysReportModel.BillBusinessInfo.GetForm().Name.ToString(); //获取账表名字  
            string fileName = string.Format("{0},{1}.xlsx", reportname, DateTime.Now.ToString("yyyyMMdd_hhmmss"));
            string filePath = PathUtils.GetPhysicalPath(KeyConst.TEMPFILEPATH, fileName);
            string fileUrl = PathUtils.GetServerPath(KeyConst.TEMPFILEPATH, fileName);
            using (ExcelOperation excelHelper = new ExcelOperation(this.View))
            {
            excelHelper.BeginExport();
            List<int> result = GetAllValuesBetween(Year, Period, Endyear, EndPeriod);
            foreach (int date in result)
            {
                int year = date / 100;
                int month = date % 100;
                string sheetname = "产品成本还原对比分析"+date.ToString();
                DataTable dt = GetReportData(filter,filterconlumn,Reportformid, ReportFiltername, schemeid, year, month, this.Context);
                excelHelper.AddSheet(sheetname);
                excelHelper.ExportToFile(headersfinal, dt);
                excelHelper.FillColumnName(name, 0);
            }
            excelHelper.EndExport(filePath, SaveFileType.XLSX);
            };
            //导出每一天的套打文件
            DynamicFormShowParameter showParameter = new DynamicFormShowParameter();
            showParameter.FormId = "BOS_FileDownload";
            showParameter.OpenStyle.ShowType = ShowType.Modal;
            showParameter.CustomComplexParams.Add("url", fileUrl);
            this.View.ShowForm(showParameter);
        }

        /// <summary>
        /// 导出当前的文件
        /// </summary>


        private DataTable GetReportData(Kingdee.BOS.Core.CommonFilter.FilterParameter filterstart,List<Kingdee.BOS.Core.CommonFilter.ColumnField> columninfo, string Reportformid, string ReportFilter, string schemeid, int EndYear, int EndPeriod, Context ctx)
        {
            ISysReportService sysReporSservice = ServiceFactory.GetSysReportService(ctx);
            IPermissionService permissionService = ServiceFactory.GetPermissionService(ctx);
            var filterMetadata = FormMetaDataCache.GetCachedFilterMetaData(ctx);//加载字段比较条件元数据。
            var reportMetadata = FormMetaDataCache.GetCachedFormMetaData(ctx, Reportformid);//加载报表元数据。
            var reportFilterMetadata = FormMetaDataCache.GetCachedFormMetaData(ctx, ReportFilter);//加载过滤条件元数据。
            var reportFilterServiceProvider = reportFilterMetadata.BusinessInfo.GetForm().GetFormServiceProvider();
            var model = new SysReportFilterModel();
            model.SetContext(ctx, reportFilterMetadata.BusinessInfo, reportFilterServiceProvider);
            model.FormId = reportFilterMetadata.BusinessInfo.GetForm().Id;
            model.FilterObject.FilterMetaData = filterMetadata;
            model.InitFieldList(reportMetadata, reportFilterMetadata);
            model.GetSchemeList();
            var entity = model.Load(schemeid);//过滤方案的主键值,可通过该SQL语句查询得到:SELECT * FROM T_BAS_FILTERSCHEME
            //var filter = model.GetFilterParameter();
            var filter =  filterstart;
            IRptParams p = new RptParams();
            p.FormId = reportFilterMetadata.BusinessInfo.GetForm().Id;
            p.StartRow = 1;
            p.EndRow = int.MaxValue;//StartRow和EndRow是报表数据分页的起始行数和截至行数,一般取所有数据,所以EndRow取int最大值。
            p.FilterParameter = filter;
            filter.CustomFilter["Year"] = EndYear;
            filter.CustomFilter["Period"] = EndPeriod;
            filter.CustomFilter["EndYear"] = EndYear;
            filter.CustomFilter["EndPeriod"] = EndPeriod;
            filter.ColumnInfo = columninfo;
            p.FilterFieldInfo = model.FilterFieldInfo;
            p.BaseDataTempTable.AddRange(permissionService.GetBaseDataTempTable(this.Context, reportMetadata.BusinessInfo.GetForm().Id));
            DataTable dt = sysReporSservice.GetData(this.Context, reportMetadata.BusinessInfo, p);
            ServiceFactory.CloseService(sysReporSservice);
            ServiceFactory.CloseService(permissionService);
            return dt;
        }
        public static List<int> GetAllValuesBetween(int startYear, int startMonth, int endYear, int endMonth)
        {
            List<int> result = new List<int>();
            int startDate = startYear * 100 + startMonth;
            int endDate = endYear * 100 + endMonth;
            int currentDate = startDate;
            while (currentDate <= endDate)
            {
                result.Add(currentDate);
                if (currentDate % 100 == 12) // 检查是否为12月,如果是则增加年份
                {
                    currentDate += 89; // 跨越到下一年的1月
                }
                else
                {
                    currentDate += 1; // 每次增加一个月
                }
            }

            return result;
        }
    }
}

成本模块输入期间范围后,点击对应的按钮可以分期导出数据,如开始期间为1-9,则分别按1-1,2-2,3-3.。。。。。的逻辑生成一个拥有9张sheet的book

分期批量导出报表(仅用作记录)

using Kingdee.BOS.App.Data;using Kingdee.BOS.Contracts.Report;using Kingdee.BOS.Contracts;using Kingdee.BOS.ServiceHelper;using Ki...
点击下载文档
确认删除?
回到顶部
客服QQ
  • 客服QQ点击这里给我发消息