java 对.xls 文件格式的操作

来源:互联网 发布:香樟木家具的危害知乎 编辑:程序博客网 时间:2024/03/28 18:18
 

二、java 对.xls 文件格式的操作

 1
 2package com.job5156.xlstodb;
 3
 4import java.io.FileInputStream;
 5import java.io.InputStream;
 6
 7import jxl.Cell;
 8import jxl.Workbook;
 9
10/**
11 * @author Alpha
12 * JAVA 操作 excel 中的 .xls文件格式
13 */

14public class ExcelUtil
15{
16    public static void main(String[] args) 
17    {
18        ExcelUtil eu = new ExcelUtil();
19        eu.run("D:/alpha/ab.xls");
20    }

21    
22    private void run(String filename)
23    {
24        try
25        {
26            InputStream is = new FileInputStream(filename);
27            jxl.Workbook rwb = Workbook.getWorkbook(is);
28            //获得总 Sheets
29            //Sheet[] sheets = rwb.getSheets();
30            //int sheetLen = sheets.length;
31            //获得单个Sheets 含有的行数
32            jxl.Sheet rs = rwb.getSheet(0); //读取第一个工作表的数据
33            //Cell[] cell_domain = rs.getColumn(0);//读取第一列的值
34            int num = rs.getRows();//得到此excel有多少行..
35            for(int i=0;i<num;i++)
36            {
37                Cell[] cell = rs.getRow(i);//得到第i行的数据..返回cell数组
38                String name = cell[0].getContents();//得到第i行.第一列的数据.
39                String email = cell[1].getContents();//得到第i行.第二列的数据.
40                String tel = cell[2].getContents(); 
41                String number = cell[3].getContents(); 
42                
43                System.out.println("===name:"+name);
44                System.out.println("===email:"+email);
45                System.out.println("===tel:"+tel);
46                System.out.println("===number:"+number);
47                System.out.println(" ");
48            }

49        }

50        catch(Exception ex)
51        {
52                ex.printStackTrace();
53        }

54        finally{
55        }

56    }

57}

58
File f=new File("D:/MMC/TestReadXls.xls");
/*
try{
    myUrl = new URL("http://202.123.189.11/import/155.xls");
    
}
catch (Exception e) {
    out.println(new String(e.getMessage()));
}
   
    HttpURLConnection http = (HttpURLConnection) myUrl.openConnection();
            http.connect();
            myUrlStream = http.getInputStream();
  */         

try {
            //FileInputStream fis=new FileInputStream(f);
            BufferedReader br=new BufferedReader(new FileReader(f));
            //Workbook w=Workbook.getWorkbook(myUrlStream);
            Workbook w=Workbook.getWorkbook(f);
            Sheet sh=w.getSheet("Sheet1");
            int columns=sh.getColumns();//获取总共有几列
            int rows=sh.getRows();//获取该表中总共有几行
           
            System.out.println(columns+"---"+rows);
            //循环行列单元格
            for (int i = 0; i < rows; i++) {
                //for(int j=0;j<columns;j++){
                    //获取单元格需要注意的是它的两个参数,第一个是列数,第二个是行数,这与通常的行、列组合有些不同。
                    Cell cell=sh.getCell(0, i);
                    //单元格值
                    String cellValue=cell.getContents();
                    System.out.print(cellValue+"  ");
                //}
                System.out.println();
            }
           
            //或者
            for (int ii = 0; ii < rows; ii++) {
                //返回该行中所有列给cell数组
                Cell[] c=sh.getRow(ii);
                for (int jj = 0; jj < c.length; jj++) {
                    //获取单元格值
                    String value=c[jj].getContents();
                    System.out.print(value+"----  ");
                }
                System.out.println();
            }
            //关闭
            w.close();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }catch (BiffException exx) {
            // TODO Auto-generated catch block
            exx.printStackTrace();
        } catch (IOException exxx) {
            // TODO Auto-generated catch block
            exxx.printStackTrace();
        }
       
    }