二开案例.套打导出.导出Excel设置纸张方向原创
金蝶云社区-风的幻想
风的幻想
68人赞赏了该文章 371次浏览 未经作者许可,禁止转载编辑于2023年12月01日 17:12:08

【依赖版本】

需要8.1.529.1(PT-146939 [8.1.0.20230309])及以上版本

【应用场景】 

产品套打导出的Excel默认的纸张方向是纵向的,部分客户想导出的Excel的方向变为横向。

【案例演示

 采购合同套打导出Excel的纸张方向为横向

【案例演示

 <1>编写列表插件,代码如下

using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Util;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;

namespace Kingdee.BOS.PlugInTest
{
    /// <summary>
    /// 【列表插件】套打导出Excel设置纸张方向(NPOI)
    /// </summary>
    [Description("【列表插件】套打导出Excel设置纸张方向(NPOI)"), HotUpdate]
    public class ExportExcelPlugIn2 : AbstractListPlugIn
    {
        private HashSet<string> _hash = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

        public ExportExcelPlugIn2()
        {
            //_hash.Add(".xls");
            _hash.Add(".xlsx");
        }

        /// <summary>
        /// 重写套打导出结束事件
        /// </summary>
        /// <param name="e"></param>
        public override void OnAfterPrintExport(AfterPrintExportEventArgs e)
        {
            // 检查数据是否存在
            if (e != null && e.ExportInfo != null && e.ExportInfo.ExportFileInfos != null && e.ExportInfo.ExportFileInfos.Count > 0)
            {
                // 循环导出文件
                foreach (var exportFileInfo in e.ExportInfo.ExportFileInfos)
                {
                    // 检查文档是否存在
                    if (File.Exists(exportFileInfo.FilePath))
                    {
                        string extensionName = Path.GetExtension(exportFileInfo.FilePath);

                        // 只处理Excel
                        if (this._hash.Contains(extensionName) == false)
                            continue;

                        IWorkbook wk = null;
                        using (FileStream fs = new FileStream(exportFileInfo.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            // 只支持xlsx格式
                            if (".xlsx".Equals(extensionName, StringComparison.OrdinalIgnoreCase))
                            {
                                wk = new XSSFWorkbook(fs);
                            }

                            if (wk == null)
                                continue;

                            ISheet sheet = wk.GetSheetAt(0);
                            sheet.PrintSetup.Landscape = true;

                            using (FileStream fs2 = new FileStream(exportFileInfo.FilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                            {
                                wk.Write(fs2);
                            }
                        }
                    }
                }
            }
        }
    }
}

 <2>拷贝插件组件到应用站点的WebSite\Bin目录下,重启IIS

 <3>注册插件

image.png

 <4>现在可以登业务站点,打开采购合同界面,导出Excel,验证一下结果。

image.png

赞 68