在.NET中使用FoxPro报告

问题描述 投票:0回答:1

我有一个 Visual FoxPro (frx) 报告,其格式设置为包含查询中的值。计划使用此格式作为 PDF 报告的模板。

有没有一种方法可以在.NET应用程序中使用它而无需任何付费第三方工具?

.net report visual-foxpro
1个回答
0
投票

C# 端收集参数,包括要生成的报告 ID 并将其以 XML 形式保存到 .DAT 文件。 然后调用 WGReport.exe,并将 .dat 名称作为参数传递。 VFP exe WGReport,打开该 .DAT 文件,解密(我发送加密的内容),运行报告,生成 PDF。

   public JsonResult GenerateReport(string reportId)
    {
        var downloadInfo = new DownloadInfo
        {
            NoData = true,
            Message = "",
            FileName = "",
            Id = ""
        };

    // Prepare reportParameters - as XML, report definition object ... 
        
        var result = CallReportCreator(ds.EncryptReportParameters(reportParameters), pdfName);
        var reportName = report.ReportName;

        if (result)
        {
            downloadInfo.NoData = false;
            downloadInfo.Message = Url.Action("DownloadReport", "Report", new { reportid = pdfId, filename = report.ReportName });
            downloadInfo.FileName = report.ReportName;
            downloadInfo.Id = pdfId;
        }
        else
        {
            downloadInfo.NoData = true;
            downloadInfo.Message = "No data found in given period.";
            downloadInfo.FileName = "";
            downloadInfo.Id = "";
        }
        
        return Json(downloadInfo, JsonRequestBehavior.AllowGet);
    }

    private bool CallReportCreator(string parameters, string resultPdf)
    {
        var exeName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WGReport.exe");
        var datName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data", Guid.NewGuid().ToString("N") + ".dat");

        System.IO.File.WriteAllText(datName, parameters);
        ShellExecMyProcess(exeName, string.Format(@"""{0}""", datName), true, 60);
        //System.IO.File.Delete(datName);
        return System.IO.File.Exists(resultPdf);
    }

    private string ShellExecMyProcess(string fileName, string arguments, bool waitToExit = true, int timeOut = 60)
    {
        bool processExited = false;
        DateTime start = DateTime.Now;

        try
        {
            ProcessStartInfo psi = new ProcessStartInfo();

            psi.FileName = fileName;
            psi.Arguments = arguments;
            psi.CreateNoWindow = true;
            psi.WindowStyle = ProcessWindowStyle.Hidden;
            psi.WorkingDirectory = Path.GetDirectoryName(psi.FileName);

            Process p = new Process();
            p.EnableRaisingEvents = true;
            p.Exited += (s, e) =>
            {
                processExited = true;
            };
            p.StartInfo = psi;


            p.Start();
            if (waitToExit)
            {
                while (!processExited && DateTime.Now < start.AddSeconds(timeOut))
                {
                    Thread.Sleep(500);
                }
                return "success";
            }
            else
            {
                return "process called";
            }
        }
        catch (Exception e)
        {
            return string.Format("error: [{0}]\n{1}", fileName, e.Message);
        }
    }

在 VFP 端主要看起来像:

Lparameters tcFileName

#include atrack.h
Set Exclusive Off
Set Safety Off

On Error Do ErrHandle With ;
    ERROR( ), Message( ), Message(1), Program( ), Lineno( )
Return RunReport(m.tcFileName)
On Error

Procedure RunReport(m.tcFileName)
    Local lcResult
    lcResult = report_ForWG(m.loReportParameters)
    Return m.lcResult
Endproc

report_ForWG(toReportParameters) 是使用给定参数运行报告并返回结果字符串的过程。就我而言,lcResult 是“在给定时间段内未找到数据”。或生成的 PDF 文件名。

© www.soinside.com 2019 - 2024. All rights reserved.