Chinaunix首页 | 论坛 | 博客
  • 博客访问: 341231
  • 博文数量: 329
  • 博客积分: 2633
  • 博客等级: 少校
  • 技术积分: 3633
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-22 15:43
文章分类

全部博文(329)

文章存档

2013年(244)

2012年(46)

2011年(39)

我的朋友

分类: Java

2011-05-09 18:16:41

  1. package com.send.action;//包名称可以自己取


  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;

  5. import org.apache.poi.hssf.usermodel.HSSFCell;
  6. import org.apache.poi.hssf.usermodel.HSSFRow;
  7. import org.apache.poi.hssf.usermodel.HSSFSheet;
  8. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  9. import org.apache.poi.poifs.filesystem.POIFSFileSystem;

  10. /**
  11.  * @author (版权归原作者) 用于读取excel
  12.  */
  13. public class ExcelReader {
  14.     private HSSFWorkbook wb = null;// book [includes sheet]

  15.     private HSSFSheet sheet = null;
  16.     private HSSFRow row = null;
  17.     private int sheetNum = 0; // 第sheetnum个工作表

  18.     private int rowNum = 0;
  19.     private FileInputStream fis = null;
  20.     private File file = null;
  21.     public ExcelReader() {
  22.     }
  23.     public ExcelReader(File file) {
  24.         this.file = file;
  25.     }
  26.     public void setRowNum(int rowNum) {
  27.         this.rowNum = rowNum;
  28.     }
  29.     public void setSheetNum(int sheetNum) {
  30.         this.sheetNum = sheetNum;
  31.     }
  32.     public void setFile(File file) {
  33.         this.file = file;
  34.     }
  35.     /**
  36.      * 读取excel文件获得HSSFWorkbook对象
  37.      */
  38.     public void open() throws IOException {
  39.         fis = new FileInputStream(file);
  40.         wb = new HSSFWorkbook(new POIFSFileSystem(fis));
  41.         fis.close();
  42.     }
  43.     /**
  44.      * 返回sheet表数目
  45.      *
  46.      * @return int
  47.      */
  48.     public int getSheetCount() {
  49.         int sheetCount = -1;
  50.         sheetCount = wb.getNumberOfSheets();
  51.         return sheetCount;
  52.     }
  53.     /**
  54.      * sheetNum下的记录行数
  55.      *
  56.      * @return int
  57.      */
  58.     public int getRowCount() {
  59.         if (wb == null)
  60.             System.out.println("=============>WorkBook为空");
  61.         HSSFSheet sheet = wb.getSheetAt(this.sheetNum);
  62.         int rowCount = -1;
  63.         rowCount = sheet.getLastRowNum();
  64.         return rowCount;
  65.     }
  66.     /**
  67.      * 读取指定sheetNum的rowCount
  68.      *
  69.      * @param sheetNum
  70.      * @return int
  71.      */
  72.     public int getRowCount(int sheetNum) {
  73.         HSSFSheet sheet = wb.getSheetAt(sheetNum);
  74.         int rowCount = -1;
  75.         rowCount = sheet.getLastRowNum();
  76.         return rowCount;
  77.     }
  78.     /**
  79.      * 得到指定行的内容
  80.      *
  81.      * @param lineNum
  82.      * @return String[]
  83.      */
  84.     public String[] readExcelLine(int lineNum) {
  85.         return readExcelLine(this.sheetNum, lineNum);
  86.     }
  87.     /**
  88.      * 指定工作表和行数的内容
  89.      *
  90.      * @param sheetNum
  91.      * @param lineNum
  92.      * @return String[]
  93.      */
  94.     public String[] readExcelLine(int sheetNum, int lineNum) {
  95.         if (sheetNum < 0 || lineNum < 0)
  96.             return null;
  97.         String[] strExcelLine = null;
  98.         try {
  99.             sheet = wb.getSheetAt(sheetNum);
  100.             row = sheet.getRow(lineNum);

  101.             int cellCount = row.getLastCellNum();
  102.             strExcelLine = new String[cellCount + 1];
  103.             for (int i = 0; i <= cellCount; i++) {
  104.                 strExcelLine[i] = readStringExcelCell(lineNum, i);
  105.             }
  106.         } catch (Exception e) {
  107.             e.printStackTrace();
  108.         }
  109.         return strExcelLine;
  110.     }
  111.     /**
  112.      * 读取指定列的内容
  113.      *
  114.      * @param cellNum
  115.      * @return String
  116.      */
  117.     public String readStringExcelCell(int cellNum) {
  118.         return readStringExcelCell(this.rowNum, cellNum);
  119.     }
  120.     /**
  121.      * 指定行和列编号的内容
  122.      *
  123.      * @param rowNum
  124.      * @param cellNum
  125.      * @return String
  126.      */
  127.     public String readStringExcelCell(int rowNum, int cellNum) {
  128.         return readStringExcelCell(this.sheetNum, rowNum, cellNum);
  129.     }
  130.     /**
  131.      * 指定工作表、行、列下的内容
  132.      *
  133.      * @param sheetNum
  134.      * @param rowNum
  135.      * @param cellNum
  136.      * @return String
  137.      */
  138.     public String readStringExcelCell(int sheetNum, int rowNum, int cellNum) {
  139.         if (sheetNum < 0 || rowNum < 0)
  140.             return "";
  141.         String strExcelCell = "";
  142.         try {
  143.             sheet = wb.getSheetAt(sheetNum);
  144.             row = sheet.getRow(rowNum);

  145.             if (row.getCell((short) cellNum) != null) { // add this condition

  146.                 // judge

  147.                 switch (row.getCell((short) cellNum).getCellType()) {
  148.                 case HSSFCell.CELL_TYPE_FORMULA:
  149.                     strExcelCell = "FORMULA ";
  150.                     break;
  151.                 case HSSFCell.CELL_TYPE_NUMERIC: {
  152.                     strExcelCell = String.valueOf(row.getCell((short) cellNum)
  153.                             .getNumericCellValue());
  154.                 }
  155.                     break;
  156.                 case HSSFCell.CELL_TYPE_STRING:
  157.                     strExcelCell = row.getCell((short) cellNum)
  158.                             .getStringCellValue();
  159.                     break;
  160.                 case HSSFCell.CELL_TYPE_BLANK:
  161.                     strExcelCell = "";
  162.                     break;
  163.                 default:
  164.                     strExcelCell = "";
  165.                     break;
  166.                 }
  167.             }
  168.         } catch (Exception e) {
  169.             e.printStackTrace();
  170.         }
  171.         return strExcelCell;
  172.     }
  173.     // 主函数用于测试,一般用这里面的函数来进行操作

  174.     public static void main(String args[]) {
  175.         //标示文件路径

  176.         File file = new File("C:\\importperson.xls");
  177.         ExcelReader readExcel = new ExcelReader(file);
  178.         //打开文件

  179.         try {
  180.             readExcel.open();
  181.         } catch (IOException e) {
  182.             e.printStackTrace();
  183.         }
  184.         readExcel.setSheetNum(0); // 设置读取索引为0的工作表

  185.         // 总行数

  186.         int count = readExcel.getRowCount();
  187.         //循环读取Excel文件中的内容

  188.         for (int i = 0; i <= count; i++) {
  189.             String[] rows = readExcel.readExcelLine(i);
  190.             for (int j = 0; j < rows.length; j++) {
  191.                 System.out.print(rows[j] + " ");
  192.             }
  193.             System.out.print("\n");
  194.         }
  195.     }
  196. }
阅读(4983) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:java关于多线程的部分操作

给主人留下些什么吧!~~