2015年3月19日 星期四

Jsp : 轉成csv (download csv)

<%@ page language="java" contentType="text/html; charset=BIG5" %>
<%@ page import="java.util.*" %>

<%
        ArrayList list = new ArrayList();

list =  from select database data (use map in list)
 
  String filename = "test.csv";

response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
out.clear();

        out.write("COM1NAME,COM2NAME");

out.newLine();

if(list.size()>0 )
{
for(int i=0; i<list.size(); i++)
{
HashMap map = (HashMap)list.get(i);

out.write(map.get("COM1")+","+map.get("COM2") );
out.newLine();
}
}

%>

沒有留言:

張貼留言