请问
从数据库中,读出一个记录字符窜,strItem,
请问JSP中 如何往一个指定文件名的CSV文件(Excel文件)里写数据(strItem)?
假设文件名为c:text.csv

解决方案 »

  1.   

    package Test;import java.io.*;
    import java.util.*;public class CsvFileImport
    {
    private static String line = null;
    BufferedReader in = null;/**
    * CsvFileImport constructor comment.
    */
    public CsvFileImport() {
    super();
    }public static void main(String args[]){try
    {String fileName = "C:\\javatest\\CSVImport.csv";
    BufferedReader in = new BufferedReader(new FileReader(fileName));
    while((line = in.readLine()) != null){
    StringTokenizer st=new StringTokenizer(line,",");
    int tokenCount=0;
    while(st.hasMoreTokens()){
    tokenCount++;
    String token=st.nextToken();
    if(token.indexOf("\"")!=-1){
    //this token contains a " , so we should read the next one too
    String next;
    while((next=st.nextToken()).indexOf("\"")==-1)
    token+=next;//ugly,might use a stringbuffer
    //it might also break if csv incorrectly formed
    }
    System.out.println(token);//you now have your token here, do extra processing with it
    if(tokenCount==3){//this is the salary
    try{
    double value=Double.parseDouble(token);
    value+=1000;
    token=String.valueOf(value);
    }catch(NumberFormatException e) {}
    }
    //now put the token in another file...
    //TODO Wink
    }
    }}catch (IOException e)
    {
    System.err.println("...");
    }}
    }
      

  2.   

    前年做过,和你的需求类似,可以用POI或者jxl实现:
    http://blog.csdn.net/lcllcl987/archive/2005/01/12/250129.aspx
      

  3.   

    POI 
    可以读取和修改office的文档,包括word,excel....
      

  4.   

    http://qingyuan18.spaces.live.com
    这里面有你要的java读写excel文件
      

  5.   

    csv文件是不需要这么麻烦的,它是逗号分隔符文件,不想xls这样的excel文件有格式。
    姓名 年龄
    a    1
    b    2
    c    3
    这些数据写在csv文件中其实就是
    姓名,年龄
    a,1
    b,2
    c,3
    这样的文本文件
      

  6.   

    OutputStream out = new FileOutputStream("c:\\text.csv");
    String line = "姓名,年龄";
    line = line + "\r\n";
    out.write(line);
    line = "a,1";
    line = line + "\r\n";
    out.write(line);
    line = "b,2";
    line = line + "\r\n";
    out.write(line);
    line = "c,3";
    line = line + "\r\n";
    out.write(line);
    out.flush();
    out.close();