---
title: "JXL 读取 Excel"
author: "hwpok"
date: "2008-08-13T17:32:00Z"
tags: []
slug: "jxl-excel"
draft: false
lastmod: "2008-08-13T17:32:00Z"
id: 615860
url: "https://hwpok.iteye.com/blog/615860"
---

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.IOException;  
import java.io.InputStream;  
import java.util.HashMap;

import jxl.Cell;  
import jxl.Sheet;  
import jxl.Workbook;  
import jxl.read.biff.BiffException;

import org.apache.struts.upload.FormFile;

/\**  
 * @author Hay Vanpull  
 *   
 \*/  
public class JXLTOOL  
{  
    private Workbook                workbook   = null; // 工作部对象  
                                                         
    private HashMap&lt;String, String&gt; mapData    = null; // data数据  
                                                         
    private Sheet                   sheet      = null; // 工作表  
                                                         
    public int                      totalRows  = 0;   // 总行数  
                                                         
    public int                      totalCells = 0;   // 总列数  
                                                         
    /\**  
     * 以一个InputStream为参数的构造器  
     *   
     * @param inputStream  
     * @throws IOException  
     * @throws BiffException  
     \*/  
    public JXLTOOL(InputStream inputStream) throws BiffException, IOException  
    {  
        this.workbook = Workbook.getWorkbook(inputStream);  
        this.sheet = this.workbook.getSheet(0);  
        this.getRows();  
        this.getCells();  
    }  
      
    /\**  
     * 以一个Struts FormFile为参数的构造器  
     *   
     * @param file  
     * @throws IOException  
     * @throws FileNotFoundException  
     * @throws BiffException  
     \*/  
    public JXLTOOL(FormFile file) throws FileNotFoundException, IOException,  
            BiffException  
    {  
        this(file.getInputStream());  
    }  
      
    /\**  
     * 以一个File为参数的构造器  
     *   
     * @param file  
     * @throws IOException  
     * @throws BiffException  
     \*/  
    public JXLTOOL(File file) throws BiffException, IOException  
    {  
        this(new FileInputStream(file));  
    }  
      
    /\**  
     * 以一个文件路径path的构造器  
     *   
     * @param filePath  
     * @throws IOException  
     * @throws BiffException  
     \*/  
    public JXLTOOL(String filePath) throws BiffException, IOException  
    {  
          
        this(new File(filePath));  
    }  
      
    /\**  
     * 把所有数据放到一个map中去,key为行号加列号  
     *   
     * @return  
     \*/  
    public HashMap&lt;String, String&gt; getExcelDate()  
    {  
        mapData = new HashMap&lt;String, String&gt;();  
        for (int i = 0; i &lt; this.totalRows; i++)  
        {  
            for (int j = 0; j &lt; this.totalCells; j++)  
            {  
                this.mapData.put(i + "" + j, this.getData(j, i));  
            }  
        }  
        return this.mapData;  
    }  
      
    /\**  
     * 得到总行数  
     \*/  
    private void getRows()  
    {  
        this.totalRows = sheet.getRows();  
    }  
      
    /\**  
     * 得到总列数  
     \*/  
    private void getCells()  
    {  
        this.totalCells = this.sheet.getColumns();  
    }  
      
    /\**  
     * 得到数据  
     *   
     * @param cell  
     * @param row  
     * @return  
     \*/  
    private String getData(int cell, int row)  
    {  
        Cell rs = this.sheet.getCell(cell, row);  
        return rs.getContents();  
    }  
}