简体   繁体   中英

Importing data from JSP file (CSV format) into Excel with Spring framework

ALL!

I have a question related to Spring, JSP. I generate a JSP file with data from database and I want to import it to Excel. I actually know how to do it using either Controllers or just providing Java code in JSP file itself. (Abstract(J)ExcelView). But my question is if I can do it without any explicit java code in JSP itself, only using custom tags. So everything will be done only with help of one .jsp and some xml configuration files. Tips on writing own taglib are welcomed.

Thank you! Would really appreciate your help!

Regards, Nigar.

You can do it like writing normal html:

<c:foreach var="row" items="${table}">
  <c:out value="${row[0]}">;<c:out value="${row[1]}">;<c:out value="${row[2]}">;
</c:foreach>

Then the only think you need to set correct is the http response header.

Attention: this will not work with jspx - because jspx will remove the whitespaces (line breaks)

Anyway: I do not recommend this way.

Was that easy at the end. here is a taglib. placed it to WEB_INF/tags folder

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ attribute name="contentType" required="true" type="java.lang.String" %>
<%@ attribute name="file" required="true" type="java.lang.String" %>


<% response.setHeader("Content-disposition",file ); 
response.setHeader("Content-type",contentType);%>

and this is jsp file:

<%@ taglib prefix="tg" tagdir="/WEB-INF/tags" %>    
<tg:excel contentType="application/vnd.ms-excel" file="attachment;filename=mf.xls" />

afterwards simple html table building with data ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM