按按钮操作生成文本文件,并使用Acumatica保存到本地驱动器

按按钮操作生成文本文件,并使用Acumatica保存到本地驱动器,acumatica,Acumatica,我试图在操作按钮上生成一个文本文件,其中包含在图形类中创建的数据视图的内容,并将该文件保存在本地驱动器中。但是我做不到 请帮我生成文件…谢谢 我正在使用Acumatica版本2019R2(版本19.203.0042) 我的代码在这里 public PXSelect<MayBankGIRO> Document; //this is my dataview public PXAction<MayBankGiroFilter> createTextFile; [

我试图在操作按钮上生成一个文本文件,其中包含在图形类中创建的数据视图的内容,并将该文件保存在本地驱动器中。但是我做不到

请帮我生成文件…谢谢

我正在使用Acumatica版本2019R2(版本19.203.0042)

我的代码在这里

public PXSelect<MayBankGIRO> Document; //this is my dataview
public PXAction<MayBankGiroFilter> createTextFile;
        [PXUIField(DisplayName = "Create Text File")]
        [PXButton()]
        public virtual IEnumerable CreateTextFile(PXAdapter adapter)
        {
            string filepath = "C:\\Subhashish Dawn";
            System.IO.StreamWriter sw = new System.IO.StreamWriter(filepath);
            MayBankGIRO giroObject = this.Document.Current;
            List<object> myListObject = new List<object> { };
            FixedLengthFile flatFile = new FixedLengthFile();
            foreach (MayBankGIRO dacRecord in this.Document.Select())
            {
                if (giroObject.ReordType == "00")
                {
                    myListObject.Add(dacRecord.ReordType + "|" + dacRecord.CorporateID + "|" + dacRecord.ClientBatchID + "|");
                }
                else
                {
                    myListObject.Add(dacRecord.ReordType + "|" + dacRecord.CorporateID + "|" + dacRecord.ClientBatchID + "|" + dacRecord.Country + "|");
                    string data = dacRecord.ReordType;

                }

                this.Document.Update(dacRecord);
            }
            flatFile.WriteToFile(myListObject, sw); 

            sw.Flush();
            sw.FlushAsync();

            string path = "DAWN" + ".txt";
            PX.SM.FileInfo file = new PX.SM.FileInfo(Guid.NewGuid(), path, null, System.Text.Encoding.UTF8.GetBytes(**path**)); // what shall i substitite in place of  **path**
            throw new PXRedirectToFileException(file, true);
}
``````````````````````````````````````````````````````
Can anyone please specify what changes in have to make in the above code.


public选择文档//这是我的数据视图
公共PXAction创建文本文件;
[PXUIField(DisplayName=“创建文本文件”)]
[PXButton()]
公共虚拟IEnumerable CreateTextFile(PXAdapter)
{
string filepath=“C:\\Subhashish Dawn”;
System.IO.StreamWriter sw=新的System.IO.StreamWriter(文件路径);
MayBankGIRO giroObject=this.Document.Current;
List myListObject=新列表{};
FixedLengthFile平面文件=新的FixedLengthFile();
foreach(此.Document.Select()中的MayBankGIRO记录)
{
如果(giroObject.ReordType==“00”)
{
添加(dacRecord.ReordType+“|”+dacRecord.CorporateID+“|”+dacRecord.ClientBatchID+“|”);
}
其他的
{
myListObject.Add(dacRecord.ReordType+“|”+dacRecord.CorporateID+“|”+dacRecord.ClientBatchID+“|”+dacRecord.Country+“|”);
字符串数据=dacrerd.ReordType;
}
本文件的更新(记录);
}
WriteToFile(myListObject,sw);
sw.Flush();
sw.FlushAsync();
字符串path=“DAWN”+“.txt”;
PX.SM.FileInfo file=new PX.SM.FileInfo(Guid.NewGuid(),path,null,System.Text.Encoding.UTF8.GetBytes(**path**));//我应该用什么替代**path**
抛出新的PXRedirectToFileException(文件,true);
}
``````````````````````````````````````````````````````
任何人都可以指定在上面的代码中需要做哪些更改。

我利用UploadFileMaintenance来实现这一点。我不确定这是否能满足您的需求,但这是我的代码的核心,它适合我

byte[] labelBytes = Encoding.ASCII.GetBytes(myLabelData);
if(labelBytes.Length > 0)
{
    string filename = "label-" + Guid.NewGuid().ToString() + ".txt";
    PX.SM.FileInfo labelFileInfo = new FileInfo(filename, null, labelBytes);

    UploadFileMaintenance upload = PXGraph.CreateInstance<UploadFileMaintenance>();

    if (upload.SaveFile(labelFileInfo))
    {
        string targetUrl = PXRedirectToFileException.BuildUrl(labelFileInfo.UID);
        throw new PXRedirectToUrlException(targetUrl, "Print Labels");
    }
}
byte[]labelBytes=Encoding.ASCII.GetBytes(myLabelData);
如果(labelBytes.Length>0)
{
字符串filename=“label-”+Guid.NewGuid().ToString()+”.txt”;
PX.SM.FileInfo labelFileInfo=新文件信息(文件名,null,labelBytes);
UploadFileMaintenance upload=PXGraph.CreateInstance();
if(upload.SaveFile(labelFileInfo))
{
字符串targetUrl=pxrirectToFileException.BuildUrl(labelFileInfo.UID);
抛出新的pxRedirectTourexception(targetUrl,“打印标签”);
}
}

假设您的
文档
对象是您需要的视图,并且它的
Select()
方法返回您需要的文件中的所有数据记录,这应该可以工作:

//我们至少需要这些,列出它们以供参考
使用PX数据;
使用制度;
使用系统集合;
使用System.Collections.Generic;
使用System.IO;
//这是您的数据视图
公共文件;
//你的行动代表
公共PXAction创建文本文件;
[PXUIField(DisplayName=“创建文本文件”)]
[PXButton()]
公共虚拟IEnumerable CreateTextFile(PXAdapter)
{
//您可以使用此方法打印自定义设置的调试信息
//测试完成后,只需移除
PXTrace.WriteInformation(“生成记录”);
//我们将首先将内容构建为字符串列表
List myList=新列表{};
//如果每个记录的“ReordType”值都可以更改,则不需要这样做
MayBankGIRO giroObject=this.Document.Current;
foreach(此.Document.Select()中的MayBankGIRO记录)
{
//每个记录的“ReordType”是否更改?
//如果需要,您可能需要在本if中使用“dacRecord.ReordType”
如果(giroObject.ReordType==“00”)
{
//仅当所有这些成员都是字符串或可以转换为字符串时,此操作才有效
添加(dacRecord.ReordType+“|”+dacRecord.CorporateID+“|”+dacRecord.ClientBatchID+“|”);
}
其他的
{
//仅当所有这些成员都是字符串或可以转换为字符串时,此操作才有效
myList.Add(dacRecord.ReordType+“|”+dacRecord.CorporateID+“|”+dacRecord.ClientBatchID+“|”+dacRecord.Country+“|”);
}
}
PXTrace.WriteInformation(“生成文件”);
//设定名称
字符串filename=“DAWN”+“.txt”;
//使用我们的下载方法
下载(myList,文件名);
}
//我们可以定义一个静态方法,以便以后能够将其重新用于其他DAC
公共静态void下载(列表行、字符串名称)
{
变量字节=默认值(字节[]);
//将所有行写入流
使用(MemoryStream stream=new MemoryStream())
{
StreamWriter sw=新StreamWriter(流);
foreach(行中的字符串行)
{
西南写入线(行);
}
sw.Close();
流位置=0;
字节=stream.ToArray();
};
//将内容保存到文件对象
PX.SM.FileInfo textDoc=新的PX.SM.FileInfo(名称,空,字节);
如果(textDoc!=null)
{
//触发器文件下载
抛出新的PXRedirectToFileException(textDoc,true);
}否则{
//TODO:您也可以在此处引发异常以通知用户
PXTrace.WriteInformation(“无法生成文件”);
}
}

您好,Markoan您的代码帮助我创建了文本文件,但文本文件的内容随着数据视图的第一条记录而重复。我的reord类型对于第一条记录只有三个值“00”,对于第二条到n-1条记录为“01”,对于第n条记录为“99”

虽然我对你的代码做了一些修改

// We need at least these, listing them for reference
using PX.Data;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

// This is your dataview
public PXSelect<MayBankGIRO> Document; 

// Your action delegate
public PXAction<MayBankGiroFilter> createTextFile;

[PXUIField(DisplayName = "Create Text File")]
[PXButton()]
public virtual IEnumerable CreateTextFile(PXAdapter adapter)
{
    // You can use this method to print debug information for your customizations
    // Just remove when you are done testing
    PXTrace.WriteInformation("Generating records");

    // We will build the content as a string list first
    List<string> myList = new List<string> { };

    // If the value of 'ReordType' can change for each record, you don't need this
    MayBankGIRO giroObject = this.Document.Current;

   foreach (MayBankGIRO dacRecord in this.Document.Select())
            {
                // Does 'ReordType' change for each record?
                // if it does you may need to use 'dacRecord.ReordType' in this if instead
                myList.Add(dacRecord.ReordType + "|" + dacRecord.CustomerReferenceNumber + "|" + dacRecord.ClientBatchID + "|" + dacRecord.Country + "|");

            }

    PXTrace.WriteInformation("Generating file");

    // Set the name
    string filename = "DAWN" + ".txt";

    // Use our download method
    Download(myList, filename);
}

// We can define a static method to be able to reuse this later for other DACs
public static void Download(List<string> lines, string name)
{
    var bytes = default(byte[]);

    // Write all lines to stream
    using (MemoryStream stream = new MemoryStream())
    {
        StreamWriter sw = new StreamWriter(stream);
        foreach (string line in lines)
        {
            sw.WriteLine(line);
        }
       // sw.Close(); this was showing some error 

        stream.Position = 0; // "Cannot reach a closed stream" hence i added it in the next line
        bytes = stream.ToArray();
        sw.Close();

    };

    // Save content to file object
    PX.SM.FileInfo textDoc = new PX.SM.FileInfo(name, null, bytes);

    if (textDoc != null)
    {
        // Trigger file download
        throw new PXRedirectToFileException(textDoc, true);
    } else {
      //TODO: You could raise an exception here also to notify the user
      PXTrace.WriteInformation("Could not generate file");
    }
}


//我们至少需要这些,列出它们以供参考
使用PX数据;
使用制度;
使用系统集合;
使用System.Collections。