我想要把从DB里读取出来的数据写到指定的CSV文件中,
请问该如何实现呢,
可以的话,希望各位大侠写个例子出来,
不用SERVLET,最好是只用JAVA类可以实现就行。谢谢(^_^)!!!!

解决方案 »

  1.   

    http://www.91linux.com/html/article/program/java/20070901/6641.html
      

  2.   

    dawn023 ,你给的那个例子是从指定的CSV文件读数据吧,
    我要的是,把从DB里取到的数据,写到CSV文件中,
    有没有上面中类似的方法呀?
      

  3.   

    1:从数据库查询数据,放到list中,这个应该知道吧。
    2:再写个方法,把list传过来。参照这个:
     public static byte[] exportMOEnterChangeExcel(List dataList,String sheetName)
            throws Exception
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            HSSFWorkbook book = new HSSFWorkbook(); // 创建一个Excel工作区        HSSFCellStyle cellStyleTop = book.createCellStyle();// 标题的样式
            HSSFCellStyle cellStyleCondition = book.createCellStyle();// 条件的样式
            HSSFCellStyle cellStyle = book.createCellStyle();// 循环数据的样式
            cellStyle.setWrapText(false);        // 设置标题的样式
            HSSFFont f = book.createFont(); // 字体样式类
            f.setFontName("宋体"); // 标题字体
            f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 标题字体加粗
            f.setFontHeightInPoints((short) 15); // 字体大小
            cellStyleTop.setFont(f); // 设置字体样式
            cellStyleTop.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 标题居中        // 设置条件样式
            HSSFFont ff = book.createFont(); // 字体样式类
            ff.setFontName("宋体"); // 标题字体
            ff.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 标题字体加粗
            ff.setFontHeightInPoints((short) 10); // 字体大小
            cellStyleCondition.setAlignment(HSSFCellStyle.ALIGN_LEFT);
            cellStyleCondition.setFont(ff);        // 设置循环数据的样式
            cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM);        HSSFCellStyle cellStyle_left = book.createCellStyle();
            cellStyle_left.setWrapText(false);
            cellStyle_left.setAlignment(HSSFCellStyle.ALIGN_LEFT);
            cellStyle_left.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM);        HSSFSheet sheet = book.createSheet();        // 设置sheet显示名称
            book.setSheetName(0, sheetName, HSSFWorkbook.ENCODING_UTF_16);        sheet.setDefaultColumnWidth((short) 9);
            sheet.setDefaultRowHeight((short) 20);        // 以下是通过sheet分别设置各列的宽度
            sheet.setColumnWidth((short) 0, (short) (256 * 2 * 10));// 第一列
            sheet.setColumnWidth((short) 1, (short) (256 * 2 * 6));// 第二列
            sheet.setColumnWidth((short) 2, (short) (256 * 2 * 6));// 第三列   
            sheet.setColumnWidth((short) 3, (short) (256 * 2 * 6));// 第四列        sheet.setPrintGridlines(true);        int rowIndex = 0;        HSSFRow row = sheet.createRow((short) rowIndex);
            Region reg = new Region(rowIndex, (short) 0, rowIndex, (short) 11);
            sheet.addMergedRegion(reg);        createCell(row, 0, "标题名称", cellStyleTop);        row = sheet.createRow(++rowIndex);
           
            reg = new Region(rowIndex, (short) 0, rowIndex, (short) 1);// 合并单元格(0、1列)
            sheet.addMergedRegion(reg);        row = sheet.createRow(++rowIndex);
            createCell(row, 0, "第一列名称", cellStyle);
            createCell(row, 1, "第二列名称", cellStyle);
            createCell(row, 2, "第三列名称", cellStyle);
            createCell(row, 3, "第四列名称", cellStyle);        for (int i = 0; i < dataList.size(); i++)
            {
                
                row = sheet.createRow(++rowIndex);//这是行
                createCell(row, 0, 查询出的数据, cellStyle);
                //这里可以多列,上面定义了四列,这里也写四列的值
            }        book.write(baos);        return baos.toByteArray();
        }
    3:将byte通过inputstream写入文件即可。
      

  4.   

    楼上的GG,这个方法看起来好像是写到EXCEL文件中的样子啊
    我要的是写CSV文件
      

  5.   

    各位GGJJ请帮忙看看吧,
    小妹感激不尽~~
      

  6.   

    csv格式的文件就是标题
    列名,列名,列名,列名,列名
    字段1,字段2,字段3,字段4,字段5
      

  7.   

    我知道CSV的格式是什么样的,
    我想要的是怎么写CSV文件,最好能给我提供一个例子,谢谢~~
      

  8.   

    我写的一个例子,还差文件头之类的,因为我的是web不是纯java,剩下的自己补补就行了
    ColumnPrintInfo cpi;
    String str = "";
         //表头
    for (int k = 0; k < titles.size(); k++) {
    cpi = (ColumnPrintInfo) titles.get(k);
    out.write(cpi.getTitle().getBytes());
    if (k != titles.size() - 1) {
    out.write(",".getBytes());
    }
    }
    out.write("\r\n".getBytes());
    int tag = 0;
    //查询出的数据
    while (rs.next()) {
    for (int i = 0; i < titles.size(); i++) {
    cpi = (ColumnPrintInfo) titles.get(i);
    if (rs.getObject(cpi.getName()) == null) {
    out.write("\"\"".getBytes());
    } else {
    str = getCellValue(cpi, rs.getObject(cpi.getName()));
    if (str != null) {
    Pattern pat = Pattern.compile("\\d{1}[\\d-/.]*");
    Matcher matcher = pat.matcher(str);
    if (matcher.matches()) {
    str = "=\"" + str + "\"";
    }
    }
    out.write(str.getBytes());
    }
    if (i != titles.size() - 1) {
    out.write(",".getBytes());
    }
    }
    out.write("\r\n".getBytes());

    }
      

  9.   

    csv文件就是文本文件了列之间用,隔开就行了
      

  10.   

    http://sourceforge.net/projects/javacsv/
      

  11.   

    csv就是名字是CSV的文本文件,你就 当文本写,自己处理下格式,用文件IO就能实现。
      

  12.   

    用jxl写啊
    那个好 很好学
      

  13.   

    jxl?没用过哎,,
    找找看看撒
      

  14.   

    CSV 直接写好了,比较简单的。 你照这个写个main方法吧,logFile下看产生CSV文件没有,导出的文件内容有很多符号会影响到CSV文件的格式,视具体处理
    EX:
          File file = new File(logFile);  //logFile 是路径,就叫做String logFile = 生成路径+想要的文件名+".csv";
         FileOutputStream out = new FileOutputStream(file);
         OutputStreamWriter osw = new OutputStreamWriter(out, "GB2312");     BufferedWriter bw = new BufferedWriter(osw);
         //然后就随便你自己去构造了,我写个第一行表示表头,然后第二行起循环      bw.write("a1" + "," + "a2" + "," + "a3" + "\r\n");//请注意,CSV默认是已逗号","分隔单元格的。这里是表头      List list = service.getYouDb;//取出的数据
     
          for(int i = 0;i<list.size();i++) {
         Object objs = (Object) list.get(i);//hibernate
         mmessage1 = objs.get......
         mmessage2 = objs.get......
         mmessage3 = objs.get......
         bw.write(mmessage1 + "," + message2 + "," + message3+ "\r\n");
    }
         bw.close();
         osw.close();
         out.close();