java web导出csv_Java 导出CSV文件及实现web下载CSV

越源
2023-12-01

这个下载文件是"用Java EE 5实现的网上书店实例"的更新版本, 原来版本是在NetBeans 5.5.1上开发, 新版本是用NetBeans 6.0开发,并且增加了jMaki实现的Web层. 下载地址: http://developers.sun.com.cn/blog/ada/resource/javaee_bookshop_nb6_zh_CN.zip   Track

本文主要介绍Java 导出CSV文件到本地及实现web下载CSV。

1.Java 导出CSV文件到本地

csvWriter:

package com.csvio;

import java.io.BufferedWriter;

import java.io.IOException;

import java.io.Writer;

import java.util.List;

public class CsvWriter extends BufferedWriter {

/**

* set wirter

*

* @param out writer

*/

public CsvWriter(final Writer out) {

super(out);

}

/**

* csv Write line

*

* @param csvLine

* csv line

* @throws IOException IOException

*/

public void writeLine(final List csvLine) throws IOException {

StringBuffer sb = new StringBuffer();

for (int i = 0; i < csvLine.size(); i++) {

String line = csvLine.get(i);

if (line == null) {

line = "";

}

sb.append("\"").append(line.replaceAll("\"", "\"\"")).append("\",");

}

super.write(sb.deleteCharAt(sb.length() - 1).toString());

super.newLine();

}

}

Student.java

Java web下载文件功能的确很简单。如下代码片段   String fileName ="....";   response.setHeader("Content-disposition","attachment; filename="+fileName);   //response.setContentType("application/ms-word");      BufferedInputStream bis = null;

package com.csvio;

public class Student {

private String name;

private String sex;

private int age;

public Student() {

}

public Student(String name, String sex, int age) {

this.name = name;

this.sex = sex;

this.age = age;

}

public int getAge() {

return age;

}

public String getName() {

return name;

}

public String getSex() {

return sex;

}

public void setAge(int age) {

this.age = age;

}

public void setName(String name) {

this.name = name;

}

public void setSex(String sex) {

this.sex = sex;

}

@Override

public String toString() {

return "Student [toString()=" + this.name + "-->" + this.sex + "-->"

+ this.age + "]";

}

}

client:

package com.csvio;

import java.io.File;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;

public class CsvClient {

/**

* @param args

*/

public static void main(String[] args) {

File file = getFile();

List stooges = new ArrayList();

for (int i=0;i<5;i++){

Student stu = new Student();

stu.setAge(i+10);

stu.setName("name " +i);

stu.setSex(i/2==0 ?"boy":"girl");

stooges.add(stu);

}

CsvWriter cw = null;

try {

// J2EE Web下载时为下面注释的代码,传人的参数是HttpServletResponse

// cw = new CsvWriter(response.getWriter());

cw = new CsvWriter(new PrintWriter(file));

for (Student stu : stooges) {

cw.writeLine(getCsvLine(stu));

}

cw.flush();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (cw != null) {

cw.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

System.out.println("Done.");

}

private static List getCsvLine(Student stu) {

List csvLine = new ArrayList();

csvLine.add(stu.getName());

csvLine.add(Integer.toString(stu.getAge()));

csvLine.add(stu.getSex());

return csvLine;

}

private static File getFile() {

String path = "E:\\test\\";

String filename="abc.csv";

File directory = new File(path);

if (!directory.exists())

directory.mkdirs();

File file = new File(path + filename);

if (!file.exists()){

try {

file.createNewFile();

} catch (IOException e1) {

e1.printStackTrace();

}

}

return file;

}

}

2. web下载CSV web下载csv的原理和输出到本地一致,就是将HttpServletResponse的流内容(writer)写入到浏览器,前提是该response的header中content-Type要告诉浏览器以下载模式接受,具体参数参见http://blog.csdn.net/bluefish625/article/details/6659288 。

 类似资料: