zoukankan      html  css  js  c++  java
  • java使用POI读取excel报表

    留此作为记录

     1 package com.demo;
     2 
     3 import java.io.FileInputStream;
     4 import java.util.Iterator;
     5 
     6 import org.apache.poi.hssf.usermodel.HSSFCell;
     7 import org.apache.poi.hssf.usermodel.HSSFRow;
     8 import org.apache.poi.hssf.usermodel.HSSFSheet;
     9 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    10 
    11 public class DemoTest {
    12     public static void main(String[] args) {
    13         System.out.println(readXls(System.getProperty("user.dir") + "\case\副本业务编码大全.xls"));
    14     }
    15 
    16     public static String readXls(String path) {
    17         String text = "";
    18         try {
    19             FileInputStream is = new FileInputStream(path);
    20             HSSFWorkbook excel = new HSSFWorkbook(is);
    21             // 获取第一个sheet
    22             HSSFSheet sheet0 = excel.getSheetAt(0);
    23             for (Iterator<?> rowIterator = sheet0.iterator(); rowIterator.hasNext();) {
    24                 HSSFRow row = (HSSFRow) rowIterator.next();
    25                 for (Iterator<?> iterator = row.cellIterator(); iterator.hasNext();) {
    26                     HSSFCell cell = (HSSFCell) iterator.next();
    27                     // 根据单元的的类型 读取相应的结果
    28                     if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
    29                         text += cell.getStringCellValue() + "	";
    30                     else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
    31                         text += cell.getNumericCellValue() + "	";
    32                     else if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA)
    33                         text += cell.getCellFormula() + "	";
    34                 }
    35                 text += "
    ";
    36             }
    37         } catch (Exception e) {
    38             // TODO Auto-generated catch block
    39             e.printStackTrace();
    40         }
    41         return text;
    42     }
    43 }
  • 相关阅读:
    AspNetPager.dll 分页控件使用方法、含有代码示例 [转]
    XmlDocument序列化到Session[转]
    静态构造函数
    错误日志[常用方法]
    Vss 源代码管理中的目录问题
    StopWatch 获得更精确的运行时间
    最近写的一个验证码.
    windowsservice 中的 currentdirectory
    vs2005常用快捷键(转贴)
    一个mapyEasy 图片切割工具 及源码
  • 原文地址:https://www.cnblogs.com/javallh/p/8986175.html
Copyright © 2011-2022 走看看