/** * this method is append date in csv file * @param data */ public void setData(Object data) { _sBuffer.append(AV_CHAR); _sBuffer.append(data); _sBuffer.append(AV_CHAR); _sBuffer.append(DEL_CHAR); }
public void writeLine() { if (_sBuffer.charAt(_sBuffer.length() - 1) == ',') _sBuffer.delete(_sBuffer.length() - 1, _sBuffer.length()); _sBuffer.append("\r\n"); }
* 每条记录占一行
* 以逗号为分隔符
* 逗号前后的空格会被忽略
* 字段中包含有逗号,该字段必须用双引号括起来
* 字段中包含有换行符,该字段必须用双引号括起来
* 字段前后包含有空格,该字段必须用双引号括起来
* 字段中的双引号用两个双引号表示
* 字段中如果有双引号,该字段必须用双引号括起来
* 第一条记录,可以是字段名
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("APPLICATION/OCTET-STREAM");
resp.setHeader("Content-Disposition", "attachment; filename=\""+ filename+ ".csv"+"\"");
OutputStream out = resp.getOutputStream();
//你把csv文件从数据库里面读出来转换成字节数组
bContent=....;
try {
out.write(bContent);
out.close();
} catch (Exception ex) {
out.close();
}
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("APPLICATION/OCTET-STREAM");
resp.setHeader("Content-Disposition", "attachment; filename=\""+ filename+ ".csv"+"\"");
OutputStream out = resp.getOutputStream();
//你把csv文件从数据库里面读出来转换成字节数组
byte[] bContent=....;
try {
out.write(bContent);
out.close();
} catch (Exception ex) {
out.close();
}
}
response.addHeader("Content-Disposition", "attachment;filename=" + FileName);//FileName设为以.csv结尾的字符串
PrintWriter writer = null;
OutputStream os = response.getOutputStream();
writer = new PrintWriter(os);
writer.write(".....");//这里写入从oracle数据库里读出的数据,然后以csv格式拼接的字符串
writer.close();
os.close();//注意 catch exception
1.这个是创建一个CSV文件.
package com.ufinity.mealsordering.common.util;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;import com.ufinity.mealsordering.common.Config;
import com.ufinity.mealsordering.common.Constant;
import com.ufinity.mealsordering.dao.impl.BaseDAO;public class CreateCSVFile extends BaseDAO{ private static CreateCSVFile _createCSVFile = null;
private FileOutputStream _fos = null;
private StringBuffer _sBuffer = null;
private String _path = "";
public static final String DEL_CHAR = ",";
public static final String AV_CHAR = "\"";
public CreateCSVFile() {
_path = Config.getString("csv_file_path");
this.init(_path);
}
/**
*
* @param path create csv file's path
*/
public void init(String path) {
String method = "createCSVFile_init";
if(path == null || path.trim().equals("")) {
_path = Constant.DEFAULT_PATH;
} else {
_path = path;
}
try {
_fos = new FileOutputStream(_path,false);
_sBuffer = new StringBuffer();
debug("", method, "create csv file sccuessful");
} catch (FileNotFoundException e) {
e.printStackTrace();
debug("", method, "not found exception of create csv file");
}
}
/**
* this method is append date in csv file
* @param data
*/
public void setData(Object data) {
_sBuffer.append(AV_CHAR);
_sBuffer.append(data);
_sBuffer.append(AV_CHAR);
_sBuffer.append(DEL_CHAR);
}
public void writeLine() {
if (_sBuffer.charAt(_sBuffer.length() - 1) == ',')
_sBuffer.delete(_sBuffer.length() - 1, _sBuffer.length());
_sBuffer.append("\r\n");
}
/**
* this method is close fileoutputstream
*/
public void close() {
String method = "close";
try {
if(_sBuffer != null) {
_fos.write(_sBuffer.toString().getBytes());
debug("", method, "close fileOutputStream successful");
} else {
debug("", method, "null point exception");
}
} catch (IOException e) {
debug("", method, "close fileOutputStream failure");
} finally {
try {
if(_fos != null) {
_fos.close();
_fos = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.这个是下载.public ActionForward downloadCSVFile(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
String method = "downloadCSVFile";
String userId = (String) request.getSession().getAttribute("Portal"); PrintWriter pw = null;
response.setContentType("application/octet-stream;charset=GB2312");
response.setHeader("Content-Disposition",
"attachment; filename=\"mealordering.csv\"");
List allOrderHistoryList = new LinkedList(); String strHead = Constant.RESTAURANT_NAME + "," + Constant.MEAL_NAME
+ "," + Constant.MEAL_PRICE + "," + Constant.NUM + ","
+ Constant.SUBSCRIBER_NAME + "," + Constant.ORDER_TIME; try {
pw = response.getWriter();
pw.println(strHead);
allOrderHistoryList = (List)request.getSession().getAttribute("addOrderHistory_list"); if (allOrderHistoryList == null || allOrderHistoryList.size() <= 0) { this.debug("'", method, Constant.MESSAGE_QUERY_NOELEMENT); } else {
for (Iterator iterator = allOrderHistoryList.iterator(); iterator
.hasNext();) {
OrderHistory orderHistory = (OrderHistory) iterator.next();
String restName = String.valueOf(orderHistory.getMeal()
.getRestaurant().getRestName()); String mealName = String.valueOf(orderHistory.getMeal()
.getMealName());
String mealPrice = String.valueOf(orderHistory.getMeal()
.getSinglePrice());
String num = String.valueOf(orderHistory.getNum());
String subscriber = String.valueOf(orderHistory
.getEmployee().getFullName());
String orderTime = String.valueOf(orderHistory.getTime()); String strLine = "\"" + restName + "\",\"" + mealName
+ "\",\"" + mealPrice + "\",\"" + num + "\",\""
+ subscriber + "\",\"" + orderTime + "\""; pw.println(strLine);
}
pw.flush();
pw.close();
}
} catch (IOException e) {
e.printStackTrace();
this.debug(userId, method, "IOException");
}
return null;
}
_sBuffer.append(AV_CHAR);
_sBuffer.append(data);
_sBuffer.append(AV_CHAR);
_sBuffer.append(DEL_CHAR);
}
public void writeLine() {
if (_sBuffer.charAt(_sBuffer.length() - 1) == ',')
_sBuffer.delete(_sBuffer.length() - 1, _sBuffer.length());
_sBuffer.append("\r\n");
}
这个方法有点不明白。。传入的data是指oracle里查到的数据对象。那么加上个"\"这个符号有何意义?
2.下载里的问题。PrintWriter 的pw 这个对象只是pw.println(strLine)就能弹出“保存“对话框了?
"\"这个是从CSV文件解析来的..你看看CSV的解析.你就知道为什么要加这个了..