金蝶云星空插件开发-SQL报表按客户分文件Excel导出并压缩下载

客户使用云星空企业版,之前为客户做了一张返利往来对账表的SQL报表,客户想按照业务员负责的客户来批量下载此业务员负责的每一个客户的往来对账单,于是在SQL报表增加批量导出功能,在报表单据头上增加一个多行文本框(显示所选的客户)和两个按钮“客户选择”“批量导出”。

点击界面上的“客户选择”按钮,插件实现客户多选并返回所选客户名称反写到“导出客户”文本框中:

点击界面上的“批量导出”按钮,插件实现按照所选的客户,每个客户导出一个Excel文件,数据为此SQL报表的返利往来对账表数据,然后将这些客户的Excel文件压缩到一个压缩包中并进行下载:

导出的压缩包文件里面是每个客户对应的Excel文件:

插件使用C#开发,压缩导出的客户往来对账Excel文件并下载的示例代码:

/// <summary>
/// 压缩文件
/// </summary>
public void CompresseFiles()
{
    // 待压缩临时文件夹路径
    string temppath = HttpContext.Current.Server.MapPath(KeyConst.TEMPFILEPATH);
    if (!Directory.Exists(temppath))
    {
        Directory.CreateDirectory(temppath);
    }
    string tempZipDirPath = Path.Combine(temppath, Guid.NewGuid().ToString());
    Directory.CreateDirectory(tempZipDirPath);

    // 这里根据自身需求获取到需要压缩的文件
    List<string> fileList = new List<string>();
    fileList.Add("CUST0001_客户1.xls");
    fileList.Add("CUST0002_客户2.xls");
    fileList.Add("CUST0003_客户3.xls");

    // 将文件移到压缩文件夹内
    foreach (var item in fileList)
    {
        string tempZipFilePath = Path.Combine(tempZipDirPath, item);
        string filePath = Path.Combine(temppath, item);
        File.Move(filePath, tempZipFilePath);
    }

    // 压缩文件
    string zipfilename = string.Format("{0}.zip", Guid.NewGuid());
    string zipfilepath = Path.Combine(temppath, zipfilename);
    CreateZipFile(tempZipDirPath, zipfilepath);

    // 删除临时文件夹
    DeleteDirectory(tempZipDirPath);

    // 弹出下载框
    Download(zipfilepath);
}

/// <summary>
/// 传入源文件夹物理路径及待生成的目标压缩文件物理路径,压缩文件目录到指定的压缩文件。
/// </summary>
/// <param name="sourceDirectory">源文件夹物理路径</param>
/// <param name="zipFilePath">待生成的目标压缩文件物理路径</param>
private void CreateZipFile(string sourceDirectory, string zipFilePath)
{
    if (!Directory.Exists(sourceDirectory))
    {
        return;
    }

    ZipOutputStream stream = null;
    try
    {
        stream = new ZipOutputStream(File.Create(zipFilePath));
        stream.SetLevel(0); // 压缩级别 0-9
        byte[] buffer = new byte[4096]; //缓冲区大小
        string[] filenames = Directory.GetFiles(sourceDirectory, "*.*", SearchOption.AllDirectories);
        foreach (string file in filenames)
        {
            ZipEntry entry = new ZipEntry(file.Replace(sourceDirectory, "").TrimStart('\\'));
            entry.DateTime = DateTime.Now;
            stream.PutNextEntry(entry);
            using (FileStream fs = File.OpenRead(file))
            {
                int sourceBytes;
                do
                {
                    sourceBytes = fs.Read(buffer, 0, buffer.Length);
                    stream.Write(buffer, 0, sourceBytes);
                } while (sourceBytes > 0);
            }
        }
    }
    finally
    {
        if (stream != null)
        {
            stream.Finish();
            stream.Close();
        }
    }
}

/// <summary>
/// 删除非空文件夹
/// </summary>
/// <param name="path">要删除的文件夹目录</param>
private void DeleteDirectory(string path)
{
    DirectoryInfo dir = new DirectoryInfo(path);
    if (dir.Exists)
    {
        DirectoryInfo[] childs = dir.GetDirectories();
        foreach (DirectoryInfo child in childs)
        {
            child.Delete(true);
        }
        dir.Delete(true);
    }
}

/// <summary>
/// 下载
/// </summary>
/// <param name="srcFilePath"></param>
private void Download(string srcFilePath)
{
    string fileName = Path.GetFileName(srcFilePath);
    // 生成文件的下载地址
    var fileUrl = PathUtils.GetServerPath(KeyConst.TEMPFILEPATH, fileName);

    // 打开文件下载界面
    var showParameter = new DynamicFormShowParameter();
    showParameter.FormId = "BOS_FileDownLoad";
    showParameter.OpenStyle.ShowType = ShowType.Modal;
    showParameter.CustomParams.Add("url", fileUrl);
    this.View.ShowForm(showParameter);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值