POI解析2003Excel、2007Excel

来源:互联网 发布:淘宝开店的货源怎么找 编辑:程序博客网 时间:2024/06/05 19:46
 package ImportExcel;import java.io.FileInputStream;import java.io.InputStream;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;/** * <POI简单的解析> *  */public class ImportExcelDemo{    public static void readExcel2003()    {        FileInputStream file = null;        try        {            file = new FileInputStream("d:\\a.xls");            HSSFWorkbook work = new HSSFWorkbook(file);            HSSFSheet sheet = work.getSheetAt(0);            int rowSumNum = sheet.getLastRowNum();            HSSFRow row = null;            for (int i = 1; i < rowSumNum; i++)            {                row = sheet.getRow(i);                System.out.println(row + "------------------" + i);                if (row == null)                {                    continue;                }                HSSFCell cell = row.getCell(0);                System.out.println(cell.getStringCellValue());            }            file.close();        }        catch (Exception e)        {            e.printStackTrace();        }    }        // 07    public static void readExcel2007()    {        InputStream in = null;        try        {            in = new FileInputStream("d:\\c.xlsx");            // 创建work            XSSFWorkbook work = new XSSFWorkbook(in);            // 得到第一个sheet            XSSFSheet sheet = work.getSheetAt(0);            // 获取总行数            int rowNum = sheet.getLastRowNum();                        XSSFRow row = null;            // 循环每一行            for (int i = 1; i <= rowNum; i++)            {                row = sheet.getRow(i);                if (null == row)                {                    continue;                }                XSSFCell cell = row.getCell(0);                if (cell != null)                {                    if (cell.getCellType() == Cell.CELL_TYPE_STRING)                    {                        System.out.println(cell.getStringCellValue());                    }                                    }            }        }        catch (Exception e)        {            e.printStackTrace();        }finally        {            if(in!=null)               in.close();        }    }        public static void main(String[] args)    {        // readExcel2003();                readExcel2007();    }}