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 ******
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 < 10; 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;
}
}
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 ******
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 < 10; 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;
}
}