csv - 在 Visualforce 中单击命令按钮时,如何从 apex 页面的字段值生成 csv 文件?

标签 csv salesforce visualforce apex

我正在开发一个 Visualforce 项目,该项目需要从一组字段值生成 csv 文件。

我使用以下代码生成 csv,我应该单击命令按钮来生成文件,但问题是,当我单击 apex 页面上的“保存”时,会生成一个空文件,而没有获取所需的文件数据。

Visualforce 页面:

 <apex:page controller="test2" contentType="text/csv#{!fileName}.csv"  showHeader="false"    sidebar="false" standardStylesheets="false">
      <apex:form >
           <apex:outputText value="{!input}" escape="false"/>
           <apex:commandButton action="{!exportContent}"/>
      </apex:form>
 </apex:page>

Controller :

 public with sharing class test2 {
      // Controller for ExportData.page,
      // which expects to be handed the following POST variables:
      // @inputdata(string) data to export (i.e. in CSV format)
      // @filename(string) name of the file to export, e.g. 'AccountData' 

      public transient String input { public get; private set; }
      public transient String fileName { public get; private set; }

      public void exportContent(){
           Map<String,String> params = ApexPages.currentPage().getParameters();

          // We expect to be handed POST variables called 'inputdata' and 'filename'
          fileName = params.get('filename');
          if (fileName == null) fileName = 'Data';

          input = params.get('inputdata');
          if (input == null) input = 'No data provided.';
     }
}

最佳答案

使用 PageReference.getParameters() 在查询字符串中传递的数据量将受到极大限制。方法。请注意,此方法仅限于 URL 参数。 POST 变量需要通过具有非 transient 成员的公共(public) Controller 类来处理,以将数据反序列化到其中。请参阅PageReference.setRedirect(Boolean)

If set to false, the redirect is a server-side forward that preserves the view state if and only if the target page uses the same controller and contains the proper subset of extensions used by the source page.

我整理了一个通过 Visualforce 查看 CSV 的示例 - Prototype CSV viewer for Visualforce 。您也许可以根据您的需要进行调整。

顺便说一句,Salesforce Stackexchange是询问 Salesforce 特定问题的好地方。

关于csv - 在 Visualforce 中单击命令按钮时,如何从 apex 页面的字段值生成 csv 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27143006/

相关文章:

python - 从提取的 JSON 数据追加和/或写入 CSV

regex - 如何有效地搜索/替换大型 txt 文件?

c# - 无法在 C# 中连接到 SalesForce

salesforce - 在visualforce中选中复选框后显示文本字段

python - 作为调用函数的一部分从列表中传递变量

salesforce - 在 Salesforce 页面中添加标签自定义字段

jakarta-ee - 我们可以通过编程方式创建一个 Visual force 页面吗?

MYSQL查询数据无法正常显示

c# - C# 中的 Ironpython 导入模块导入 csv 模块时出错

session - Salesforce Session变量,在Session中设置和获取变量