各位大大,小弟菜鸟一个刚开始学习javaweb开发,求教一个问题,页面上有2个连接,点击后弹出打开保存对话框,要求保存为csv文件,数据是从oracle的对应表里查出的,小弟应该怎么做,求教各位大大,有代码示范最好~~

解决方案 »

  1.   

    jspSmartUploadhttp://topic.csdn.net/t/20051116/23/4398519.html
      

  2.   

     CSV其实就是文本文件,只是用逗号隔开了字段而已,自己组合一下,注意一下CSV的规则      CSV文件,也叫逗号分隔值文件,英文名称COMMA SEPARATED VALUE。具体格式规则如下: 
        * 每条记录占一行 
        * 以逗号为分隔符 
        * 逗号前后的空格会被忽略 
        * 字段中包含有逗号,该字段必须用双引号括起来 
        * 字段中包含有换行符,该字段必须用双引号括起来 
        * 字段前后包含有空格,该字段必须用双引号括起来 
        * 字段中的双引号用两个双引号表示 
        * 字段中如果有双引号,该字段必须用双引号括起来 
        * 第一条记录,可以是字段名 
      

  3.   

    用一个链接请求servlet
    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();
    }
    }
      

  4.   

    用一个链接请求servlet
    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();
    }
    }
      

  5.   

    推荐一个就是apahce commons组件里面有一个CSV的jar包,可以帮助你生成CSV文件,如果需要的话,可以给我发邮件,我发给你[email protected]
      

  6.   

    mlitsn大大。有点不明白的地方。数据库读出来的我把它放到bean里去了用了vector装起来,放到前台的jsp里面了,把数据转换成csv文件这个能理解。但是。怎么把它和打开保存对话框结合起来有点不明白。。
      

  7.   

    链接地址采用servlet,在servlet里指定输出格式为text的即可
      

  8.   

    servlet里写代码如下:      response.setContentType(CONTENT_TYPE);
          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
      

  9.   

    CONTENT_TYPE = "text/html; charset=GBK"
      

  10.   

    我刚写了一个,你看看吧..希望对你有帮助..
    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;
    }
      

  11.   

    skyqiang 谢谢大大。。我大概明白思路了。关于你得代码我有两个问题希望能够得到解答,1。创建csv文件里 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");
        }
    这个方法有点不明白。。传入的data是指oracle里查到的数据对象。那么加上个"\"这个符号有何意义?
    2.下载里的问题。PrintWriter 的pw 这个对象只是pw.println(strLine)就能弹出“保存“对话框了?
      

  12.   

    可以的..我们这次写的这个项目应经用了..很好啊..你用一下就知道了..做J2EE开发..首选要让程序跑起来..自己看看就知道什么意思...
    "\"这个是从CSV文件解析来的..你看看CSV的解析.你就知道为什么要加这个了..