zoukankan      html  css  js  c++  java
  • poi读excel小例子

    比较简单,直接看代码:


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


    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;


    public class PoiReadExcel {

    public static void readExcel(){
    File f = new File("C:\\Documents and Settings\\Administrator\\桌面\\123.xls");
    try {
                FileInputStream is = new FileInputStream(f);
                HSSFWorkbook wbs = new HSSFWorkbook(is);
                HSSFSheet childSheet = wbs.getSheetAt(0);
                System.out.println("有行数1 :" + childSheet.getLastRowNum());
                int j;
                for (j = 0; j <= childSheet.getLastRowNum(); j++) {
                    HSSFRow row = childSheet.getRow(j);
    //                 System.out.println(row.getPhysicalNumberOfCells());   
                     System.out.println("有列数" + row.getLastCellNum());   
                    if (null != row) {
                        for (int k = 0; k < row.getLastCellNum(); k++) {
                            HSSFCell cell = row.getCell((short)k);
                            if (null != cell) {
                                switch (cell.getCellType()) {
                                case HSSFCell.CELL_TYPE_NUMERIC: // 数字   
                                    System.out.print(cell.getNumericCellValue()
                                            + "   ");
                                    break;
                                case HSSFCell.CELL_TYPE_STRING: // 字符串   
                                    System.out.print(cell.getStringCellValue()
                                            + "   ");   
                                    break;   
                                case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean   
                                    System.out.println(cell.getBooleanCellValue()   
                                            + "   ");   
                                    break;   
                                case HSSFCell.CELL_TYPE_FORMULA: // 公式   
                                    System.out.print(cell.getCellFormula() + "   ");   
                                    break;   
                                case HSSFCell.CELL_TYPE_BLANK: // 空值   
                                    System.out.println(" ");   
                                    break;   
                                case HSSFCell.CELL_TYPE_ERROR: // 故障   
                                    System.out.println(" ");   
                                    break;   
                                default:   
                                    System.out.print("未知类型   ");   
                                    break;   
                                }
                            } else {
                                System.out.print("-   ");
                            }
    //                        System.out.println();
                        }
                    }
                    
                    System.out.println();
                    
                }
                System.out.println();
                System.out.println("有行数2 " + childSheet.getLastRowNum()+"--"+j);
            } catch (Exception e) {
                e.printStackTrace();
            }
    }


    }

  • 相关阅读:
    C++ 抽象类二(抽象类的基本语法)
    C++ 抽象类一(多继承与赋值兼容性原则)
    C++ 类的多态五(多态的语法本质分析)
    C++ 类的多态四(虚析构函数的重要性)
    C++ 类的多态三(多态的原理--虚函数指针--子类虚函数指针初始化)
    MEF等Ioc框架引起内存泄露-PartCreationPolicy
    MEF部件的生命周期(PartCreationPolicy)
    wpf prism IRegionManager 和IRegionViewRegistry
    silverlight开发实例(Prism+MVVM+RIA)(二)--创建shell及用户登录
    Prism框架 如何在主程序中合理的弹出子窗体
  • 原文地址:https://www.cnblogs.com/qlong8807/p/2741579.html
Copyright © 2011-2022 走看看