<bean parent="webscripts.formatmap">
<property name="formats">
<props>
<prop key="csv">application/vnd.ms-excel</prop>
</props>
</property>
</bean>
<webscript>
<shortname>CSV Export</shortname>
<description>CSV Export</description>
<url>/myApp/export</url>
<format default="csv">extension</format>
<authentication>user</authentication>
<transaction>required</transaction>
</webscript>
<#list folders as folder>"${folder.name}";"${folder.properties.description!’’}
</#list>
res.setHeader("Content-disposition", "attachment; filename=monfichier.csv");
res.setContentType("text/csv");
res.setContentEncoding("UTF-8");
…package com.atolcd.web.scripts;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import org.alfresco.model.ContentModel;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.namespace.NamespaceService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.extensions.webscripts.AbstractWebScript;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
import org.springframework.util.Assert;
import com.csvreader.CsvWriter;
public class ExportCSV extends AbstractWebScript implements InitializingBean {
public final static char separator = ';';
private NodeService nodeService;
private SearchService searchService;
private NamespaceService namespaceService;
public void setNodeService(NodeService nodeService) {
this.nodeService = nodeService;
}
public void setSearchService(SearchService searchService) {
this.searchService = searchService;
}
public void setNamespaceService(NamespaceService namespaceService) {
this.namespaceService = namespaceService;
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(nodeService);
Assert.notNull(searchService);
Assert.notNull(namespaceService);
}
public void execute(WebScriptRequest req, WebScriptResponse res)
throws IOException {
// Create an CSV Object
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CsvWriter csv = new CsvWriter(baos, separator, Charset.forName("UTF-8"));
try {
// Write into the CSV Object
csv.writeRecord(new String[] { "Nom", "Description" });
List<NodeRef> nodes = searchService.query(new StoreRef("workspace", "SpacesStore"), SearchService.LANGUAGE_LUCENE, "+TYPE:\""
+ ContentModel.TYPE_FOLDER.toPrefixString(namespaceService) + "\"").getNodeRefs();
for (NodeRef node : nodes) {
String[] tab = new String[] {
(String) nodeService.getProperty(node, ContentModel.PROP_NAME),
(String) nodeService.getProperty(node, ContentModel.PROP_DESCRIPTION)
};
csv.writeRecord(tab);
}
} catch (IOException e) {
System.err.println(e.getMessage());
}
// Close the CSV Object
csv.close();
// Send the file into the WS getOutputStream
res.setHeader("Content-disposition", "attachment; filename=monfichier.csv");
res.setHeader("Content-Type", "text/csv; charset=utf-8");
// res.setHeader("Accept-Charset", "utf-8");
// res.setHeader("Content-Length", Long.toString(baos.size()));
res.setContentType("text/csv");
res.setContentEncoding("UTF-8");
baos.writeTo(res.getOutputStream());
}
}
Content from pre 2016 and from language groups that have been closed.
Content is read-only.
By using this site, you are agreeing to allow us to collect and use cookies as outlined in Alfresco’s Cookie Statement and Terms of Use (and you have a legitimate interest in Alfresco and our products, authorizing us to contact you in such methods). If you are not ok with these terms, please do not use this website.