现在的问题是我有一个csv文件里边存放的是客户信息,我想把他导入到LDAP类型的数据库里。
我有现成的方法可以把每条信息直接加入到库里。也就是只要能把csv文件里的每行信息做个
一个对象加入到一个Object数组里就行。
本人是个菜鸟希望有高手帮忙解决。分数一定奉上。

解决方案 »

  1.   

    没有弄过,应该和读取xls一样,用jxl包看能不能读。
      

  2.   

    下面这个是返回数组 values       InputStreamReader isr = null;
            BufferedReader br = null;        try {
                isr = new InputStreamReader(is, "Windows-31J");
                br = new BufferedReader(isr);            char separator;
      
                separator = ',';
              
                for (;;) {
                    String line = br.readLine();                if (line == null) {
                        break;
                    }                line = line + separator;
                    List<String> values = new ArrayList<String>();
                    int len = line.length();
                    int begin = 0;
                    while (begin < len) {
                        int end = findSeparator(line, begin);
                        if (end == -1) {
                            throw new IOException("error data " + line);
                        }
                        String value = line.substring(begin, end);
                        values.add(unescape(value));
                        begin = end + 1;
                    }               return values;
                }
                
    private static int findSeparator(String rowString, int begin,
                CsvSeparatorType type) {
            int len = rowString.length();
            int quoteCount = 0;        char separator;
            
                separator = ',';        for (int i = begin; i < len; i++) {
                char ch = rowString.charAt(i);
                if (ch == '"') {
                    quoteCount++;
                } else if ((ch == separator) && ((quoteCount % 2) == 0)) {
                    return i;
                }
            }
            return -1;
        }                    } catch (IOException e) {
               //         } finally {
                try {
                    if (br != null) {
                        br.close();
                        br = null;
                    }                if (isr != null) {
                        isr.close();
                        isr = null;
                    }
                } catch (Exception e) {
                }
            }
        }
      

  3.   

    List<String> read(InputStream is) {        InputStreamReader isr = null;
            BufferedReader br = null;        try {
                isr = new InputStreamReader(is, "Windows-31J");
                br = new BufferedReader(isr);            char separator;
      
                separator = ',';
              
                for (;;) {
                    String line = br.readLine();                if (line == null) {
                        break;
                    }                line = line + separator;
                    List<String> values = new ArrayList<String>();
                    int len = line.length();
                    int begin = 0;
                    while (begin < len) {
                        int end = findSeparator(line, begin);
                        if (end == -1) {
                            throw new IOException("error data " + line);
                        }
                        String value = line.substring(begin, end);
                        values.add(unescape(value));
                        begin = end + 1;
                    }               return values;
        }
    } catch (IOException e) {
               // error define 

            } finally {
                try {
                    if (br != null) {
                        br.close();
                        br = null;
                    }

                    if (isr != null) {
                        isr.close();
                        isr = null;
                    }
                } catch (Exception e) {
                }
            }
        }   
                
    private static int findSeparator(String rowString, int begin,
                CsvSeparatorType type) {
            int len = rowString.length();
            int quoteCount = 0;        char separator;
            
                separator = ',';        for (int i = begin; i < len; i++) {
                char ch = rowString.charAt(i);
                if (ch == '"') {
                    quoteCount++;
                } else if ((ch == separator) && ((quoteCount % 2) == 0)) {
                    return i;
                }
            }
            return -1;
        }                    } catch (IOException e) {
               //         } finally {
                try {
                    if (br != null) {
                        br.close();
                        br = null;
                    }                if (isr != null) {
                        isr.close();
                        isr = null;
                    }
                } catch (Exception e) {
                }
            }
        }
      

  4.   

    我刚帮你试了下,可以的,用jxl可以读取。。
    public List jxlImportExcelToDate(String FileName) { try {
    //读取excel数据返回到此List
    List result = new ArrayList();

    File uploadFileName = new File(FileName);
    InputStream is = new FileInputStream(uploadFileName); // 创建一个excel文件对象
    jxl.Workbook excel = Workbook.getWorkbook(is);
    // 获得第一个excel文件的sheet
    Sheet sheet = excel.getSheet(0);
    // 获取sheet单元的总行数
    int rows = sheet.getRows();
    // 获得sheet单元的总列数
    int columns = sheet.getColumns();

    // 读取数据
    for (int r = 0; r < rows; r++) {

    //每行数据
    String[] rowDates = new String[columns];
    for (int c = 0; c < columns; c++) {
    Cell cell = sheet.getCell(c, r);
    String cellValue = cell.getContents();
    rowDates[c] = cellValue;
    }

    //将值加入到List中返回
    result.add(rowDates);
    } // 关闭excel对象
    excel.close();
    System.out.println("Read Excel file sucess!");
    return result;
    } catch (Exception e) {
    System.out.println(e.getMessage());
    return null;
    } }
    你去下载个jxl包就可以了。
      

  5.   

    test:
    ---------------------------------------
    public static void testJxlRead(){

    JxlOperationExcel jxlExcel = new JxlOperationExcel();
    List l = jxlExcel.jxlImportExcelToDate("C:\\Documents and Settings\\Administrator\\My Documents\\Classroom.csv");

    for (int i = 0; i < l.size(); i++){
    String[] ss = (String[]) l.get(i); 
    for (int j = 0; j < ss.length; j++){
    System.out.print(ss[j]+" ");
    }
    System.out.print("\n");
    }
    }