This article implements a simple CSV file export program instance based on the servlet technology.
The code is as follows, where the role of the Setcsvdata function is to set the exported data and save the result in a vector, which can be extended arbitrarily when applied:
public class Go2csv extends HttpServlet
{
Public Vector Veccsvdata;
Private String FileName;
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException
{
FileName = "Untitled.csv"; Default file name
Veccsvdata = new Vector ();
Sets the data to be exported
Setcsvdata (Request);
Exporting vector to CSV file
String strout = "";
for (int i = 0; i < veccsvdata.size (); i++)
{
String[] StrLine = (string[]) veccsvdata.elementat (i);
int col_num = Strline.length;
for (int j = 0; J < Col_num; J + +)
{
Strout + = Strline[j];
if (J < col_num-1)
{
Strout + = ",";
}
}
Strout + = "\ n";
}
Output strout to Response Hu Jintao
Response.reset (); Reset the response
Response.setcontenttype ("application/octet-stream;charset=gb2312"); The encoding of this example is GB2312
Response.setheader ("Content-disposition", "Attachment filename=\" "+ filename +" \ ");
The contents of the export file Untitled.csv are as follows:
0-0,0-1,0-2,0-3,0-4,0-5,0-6,0-7,0-8,0-9
1-0,1-1,1-2,1-3,1-4,1-5,1-6,1-7,1-8,1-9
2-0,2-1,2-2,2-3,2-4,2-5,2-6,2-7,2-8,2-9
3-0,3-1,3-2,3-3,3-4,3-5,3-6,3-7,3-8,3-9
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.