An instance of exporting a CSV file with a servlet implementation

Source: Internet
Author: User
Tags integer reset tostring
Servlet Author: Li Shupeng tukeyli@sohu.com

Keywords: Java Servlet CSV

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:

Package common;

Import java.io.IOException;
Import Java.io.PrintWriter;
Import Java.util.Vector;
Import javax.servlet.ServletException;
Import javax.servlet.http.*;

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 +" \ ");

PrintWriter out;
Try
{
out = Response.getwriter ();
Out.write (Strout);
}
catch (IOException E)
{
E.printstacktrace ();
}
//***************************************
}

public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException
{
Doget (request, response);
}

/**
* Sets the data to be exported
* @param request
*/
public void Setcsvdata (HttpServletRequest request)
{
Writing vector
for (int i = 0; i < 5; i++)
{
string[] StrLine = new STRING[10];
for (int j = 0; J < J + +)
{
STRLINE[J] = integer.tostring (i) + "-" + integer.tostring (j);
}
Veccsvdata.addelement (StrLine);
}
}

/**
* Sets the file name to be exported
* @param filename
*/
public void Setfilename (String filename)
{
filename = filename;
}
}

Call Method:
Http://hostname:port/ApplicationName/servlet/common.Go2Csv

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


Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.