Chinaunix

标题: java操作Excel文件 [打印本页]

作者: wlcacc    时间: 2009-12-31 16:20
标题: java操作Excel文件
利用java操作Excel,有个开源的-jxl.jar,可以到
http://sourceforge.net/project/showfiles.php?group_id=79926
下载。

读取:

    public static String readExcel(File file) {
        StringBuffer sb = new StringBuffer();
        Workbook wb = null;
        try {
            wb = Workbook.getWorkbook(file);
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (wb == null)
            return "";
        Sheet[] sheet = wb.getSheets();
        if (sheet != null && sheet.length > 0) {
//smoke///////////////////////////////////////////////////////////////////////////////////
            ListSmoke> l=new ArrayListSmoke>();
            int rowNum = sheet[0].getRows();
            System.out.println("Row Num:"+rowNum);
            int t=00;
            for (int j = 0; j  rowNum; j++) {
                Smoke s=new Smoke();
                Cell[] cells = sheet[0].getRow(j);
                if (cells != null && cells.length > 0) {
                    //s.setId(Integer.parseInt(cells[0].getContents()));
                    s.setType(cells[1].getContents());
                    s.setModel(cells[2].getContents());
                    s.setName(cells[3].getContents());
                }
                l.add(s);
                t++;
            }
            System.out.println("number of execute:"+t);
            System.out.println("list size:"+l.size());
            boolean b=save(l);
            System.out.println("result of saving:"+b);
//smoke***********************************************************************************
            
//            for (int i = 0; i
//                int rowNum = sheet.getRows();
//                for (int j = 0; j
//                    Cell[] cells = sheet.getRow(j);
//                    if (cells != null && cells.length > 0) {
//                        for (int k = 0; k
//                            String cellValue = cells[k].getContents();
//                            sb.append(cellValue + "\t");
//                        }
//                    }
//                    sb.append("\r\n");
//                }
//                sb.append("\r\n");
//            }
        }
        wb.close();
        return sb.toString();
    }
   
程序有点乱。
原文:
http://www.javaeye.com/topic/55844


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/22371/showart_2136586.html




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2