站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题百分求教如何把csv文件里的信息读到程序里最好能转换成数组。

百分求教如何把csv文件里的信息读到程序里最好能转换成数组。

摘要: 百分求教如何把csv文件里的信息读到程序里最好能转换成数组。


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


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


下面这个是返回数组 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 values = new ArrayList();
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) {
}
}
}





我去试试!~~好用了回来就结帖


List 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 values = new ArrayList();
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) {
}
}
}


我刚帮你试了下,可以的,用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包就可以了。


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");
}
}


谢谢大家了!~我先结了后自己去试试!~要是还不会我再开个问大家!~


↑返回目录
前一篇: 如何将applet中的弹出窗口置于最顶层?
后一篇: java中web页面的下拉列表框中数据怎样绑定数据库!