您好,欢迎光临本网站![请登录][注册会员]  
文件名称: 读取xls、xlsx文件所需poi.jar包
  所属分类: Java
  开发工具:
  文件大小: 8mb
  下载次数: 0
  上传时间: 2017-09-30
  提 供 者: qq_27******
 详细说明: package com.cepri.framework.utils; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Date; import jav a.util.HashMap; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; 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; public class ReadMain { private Workbook wb; private Sheet sheet; private Row row; public ReadMain(String filepath) { if(filepath==null){ return; } String ext = filepath.substring(filepath.lastIndexOf(".")); try { InputStream is = new FileInputStream(filepath); if(".xls".equals(ext)){ wb = new HSSFWorkbook(is); }else if(".xlsx".equals(ext)){ wb = new XSSFWorkbook(is); }else{ wb=null; } } catch (FileNotFoundException e) { System.out.println("FileNotFoundException"); } catch (IOException e) { System.out.println("IOException"); } } /** * 读取Excel表格表头的内容 * * @param InputStream * @return String 表头内容的数组 * @author zengwendong */ public String[] readExcelTitle() throws Exception{ if(wb==null){ throw new Exception("Workbook对象为空!"); } sheet = wb.getSheetAt(0); row = sheet.getRow(0); // 标题总列数 int colNum = row.getPhysicalNumberOfCells(); System.out.println("colNum:" + colNum); String[] title = new String[colNum]; for (int i = 0; i < colNum; i++) { // title[i] = getStringCellValue(row.getCell((short) i)); title[i] = row.getCell(i).getCellFormula(); } return title; } /** * 读取Excel数据内容 * * @param InputStream * @return Map 包含单元格数据内容的Map对象 * @author zengwendong */ public Map> readExcelContent() throws Exception{ if(wb==null){ throw new Exception("Workbook对象为空!"); } Map> content = new HashMap>(); sheet = wb.getSheetAt(0); // 得到总行数 int rowNum = sheet.getLastRowNum(); row = sheet.getRow(0); int colNum = row.getPhysicalNumberOfCells(); // 正文内容应该从第二行开始,第一行为表头的标题 for (int i = 1; i <= rowNum; i++) { row = sheet.getRow(i); int j = 0; Map cellValue = new HashMap(); while (j < colNum) { Object obj = getCellFormatValue(row.getCell(j)); cellValue.put(j, obj); j++; } content.put(i, cellValue); } return content; } /** * * 根据Cell类型设置数据 * * @param cell * @return * @author zengwendong */ private Object getCellFormatValue(Cell cell) { Object cellvalue = ""; if (cell != null) { // 判断当前Cell的Type switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC:// 如果当前Cell的Type为NUMERIC case Cell.CELL_TYPE_FORMULA: { // 判断当前的cell是否为Date if (DateUtil.isCellDateFormatted(cell)) { // 如果是Date类型则,转化为Data格式 // data格式是带时分秒的:2013-7-10 0:00:00 // cellvalue = cell.getDateCellValue().toLocaleString(); // data格式是不带带时分秒的:2013-7-10 Date date = cell.getDateCellValue(); cellvalue = date; } else {// 如果是纯数字 // 取得当前Cell的数值 cellvalue = String.valueOf(cell.getNumericCellValue()); } break; } case Cell.CELL_TYPE_STRING:// 如果当前Cell的Type为STRING // 取得当前的Cell字符串 cellvalue = cell.getRichStringCellValue().getString(); break; default:// 默认的Cell值 cellvalue = ""; } } else { cellvalue = ""; } return cellvalue; } public static void main(String[] args) { try { String filepath = "C:\\Users\\adminiatrotor\\Desktop\\111.xlsx"; ReadMain excelReader = new ReadMain(filepath); // 对读取Excel表格内容测试 Map> map = excelReader.readExcelContent(); System.out.println("获得Excel表格的内容:"); for (int i = 1; i <= map.size(); i++) { Map a = map.get(i); System.out.println(a); } } catch (FileNotFoundException e) { System.out.println("未找到指定路径的文件!"); e.printStackTrace(); }catch (Exception e) { e.printStackTrace(); } } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: xlsx excel poi
 输入关键字,在本站1000多万海量源码库中尽情搜索: