读取Excel文件

/*
 * Created on 2005-3-25 Author 泰伯子仪
 *
 * Copyright (c) 2003-2005 by HodeSoft
 * All rights reserved.
 */
package excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;

public class ReadExcel
{
    public void readExcel()
    {
        try
        {
            //构建Workbook对象, 只读Workbook对象
            //直接从本地文件创建Workbook
            //从输入流创建Workbook
            File file = new File("E:/temp/HodeOA_2005/Book1.xls");
            InputStream is = new FileInputStream(file);
            Workbook rwb = Workbook.getWorkbook(is);
           
            //获得工作薄(Workbook)中工作表(Sheet)的个数
            //int sheets = rwb.getNumberOfSheets();
           
            //返回工作薄(Workbook)中工作表(Sheet)对象数组
            Sheet[] sheets = rwb.getSheets();
            for (int i = 0; i < sheets.length; i++)
            {
                System.out.println("-------------------------------");
                System.out.println("【工作表Sheet"+i+"的信息】");
               
                //获取获取工作表(Sheet)的名称
                String sheetName = sheets[i].getName();
                System.out.println("Sheet名称:" + sheetName);
               
                //获取Sheet表中所包含的总列数
                int rsColumns = sheets[i].getColumns();
                System.out.println("Sheet总列数:" + rsColumns);
               
                //获取Sheet表中所包含的总行数
                int rsRows = sheets[i].getRows();
                System.out.println("Sheet总行数:" + rsRows);
               
                //逐行输出单元格信息
                System.out.println("Sheet内容:" + rsRows);
                for (int j = 0; j < rsRows; j++)
                {
                    System.out.print("行"+j+":");
                    //获取某一行的所有单元格,返回的是单元格对象数组,示例子:
                    Cell[] cell = sheets[i].getRow(j);
                    for (int k = 0; k < cell.length; k++)
                    {
                        //获取单元内容
                        String cellContent = cell[k].getContents();
                        //获取单元类型
                        CellType cellType= cell[k].getType();
                       
                        System.out.print("  "+cellContent+"["+cellType+"]  ");
                    }
                    System.out.println();
                }
            }

            rwb.close();
        }
        catch (Exception e)
        {
         e.printStackTrace();
        }
    }
   
    public static void main(String[] args)
    {
        ReadExcel re = new ReadExcel();
        re.readExcel();
    }
}

阅读更多

没有更多推荐了,返回首页