我正在开发一个需要从一组字段值生成csv文件的visualforce项目。
我使用以下代码生成csv,我应该点击命令按钮来生成文件,但问题是当我点击顶点页面上的保存时会生成一个空文件没有获取所需的数据。
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>
控制器:
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.';
}
}
答案 0 :(得分:2)
使用PageReference.getParameters()方法在查询字符串中传递的数据量非常有限。请注意,此方法仅限于URL参数。 POST变量需要通过使用具有非瞬态成员的公共控制器类来处理,以将数据反序列化。见PageReference.setRedirect(Boolean)
如果设置为false,则重定向是服务器端转发,当且仅当目标页面使用相同的控制器并包含源页面使用的正确扩展子集时,才会保留视图状态。
我汇总了一个通过Visualforce查看CSV的示例 - Prototype CSV viewer for Visualforce。您可以根据自己的需要进行调整。
顺便说一下,Salesforce Stackexchange是向Salesforce提出具体问题的好地方。