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;
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
private static int findSeparator(String rowString, int begin, CsvSeparatorType type) { int len = rowString.length(); int quoteCount = 0; char separator;
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"); } }
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) {
}
}
}
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) {
}
}
}
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包就可以了。
---------------------------------------
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");
}
}