利用jsl.jar这个包,可以很容易的读取xls文件,包在下面的附件中
?
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ParseExcel {
static List<String[]> parse(File file) {
List<String[]> excelValueList = new ArrayList<String[]>();
if (file.exists() && file.canRead()
&& (file.getName().lastIndexOf(".xls") >= 1)) {
Workbook workbook = null;
try {
workbook = Workbook.getWorkbook(file);
Sheet sheet = workbook.getSheet(0);
int row = sheet.getRows();
int col = sheet.getColumns();
for (int r = 0; r < row; r++) {
String[] rowValue = new String[col];
for (int c = 0; c < col; c++) {
rowValue[c] = sheet.getCell(c, r).getContents() != null ? sheet
.getCell(c, r).getContents()
: "";
}
excelValueList.add(rowValue);
}
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (workbook != null) {
workbook.close();
}
}
}
return excelValueList;
}
public static void main(String[] args) {
String fname = "E:\\1\\高新技术(处理后).xls";
File file = new File(fname);
List<String[]> excelValueList = new ArrayList<String[]>();
excelValueList = parse(file);
for(String[] sa:excelValueList){
for(String s:sa){
System.out.print(s+"----");
}
System.out.println();
}
}
}
?
?