java读取xls

来源:互联网 发布:淘宝客返利网 赚钱 编辑:程序博客网 时间:2024/06/05 03:14

java读取xls

利用jsl.jar这个包,可以很容易的读取xls文件,包在下面的附件中

Java代码 复制代码 收藏代码
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import jxl.Sheet;
  6. import jxl.Workbook;
  7. import jxl.read.biff.BiffException;
  8. public class ParseExcel {
  9. static List<String[]> parse(File file) {
  10. List<String[]> excelValueList = new ArrayList<String[]>();
  11. if (file.exists() && file.canRead()
  12. && (file.getName().lastIndexOf(".xls") >=1)) {
  13. Workbook workbook = null;
  14. try {
  15. workbook = Workbook.getWorkbook(file);
  16. Sheet sheet = workbook.getSheet(0);
  17. int row = sheet.getRows();
  18. int col = sheet.getColumns();
  19. for (int r =0; r < row; r++) {
  20. String[] rowValue = new String[col];
  21. for (int c =0; c < col; c++) {
  22. rowValue[c] = sheet.getCell(c, r).getContents() != null ? sheet
  23. .getCell(c, r).getContents()
  24. : "";
  25. }
  26. excelValueList.add(rowValue);
  27. }
  28. } catch (BiffException e) {
  29. // TODO Auto-generated catch block
  30. e.printStackTrace();
  31. } catch (IOException e) {
  32. // TODO Auto-generated catch block
  33. e.printStackTrace();
  34. } finally {
  35. if (workbook != null) {
  36. workbook.close();
  37. }
  38. }
  39. }
  40. return excelValueList;
  41. }
  42. public staticvoid main(String[] args) {
  43. String fname = "E:\\1\\高新技术(处理后).xls";
  44. File file = new File(fname);
  45. List<String[]> excelValueList = new ArrayList<String[]>();
  46. excelValueList = parse(file);
  47. for(String[] sa:excelValueList){
  48. for(String s:sa){
  49. System.out.print(s+"----");
  50. }
  51. System.out.println();
  52. }
  53. }
  54. }

0 0
原创粉丝点击