import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Date; import java.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.XSSFWorkbook; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 读取Excel * * @author zengwendong */ public class ReadExcelUtils { private Logger logger = LoggerFactory.getLogger(ReadExcelUtils.class); private Workbook wb; private Sheet sheet; private Row row; public ReadExcelUtils(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) { logger.error("FileNotFoundException", e); } catch (IOException e) { logger.error("IOException", e); } } /** * 读取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<Integer, Map<Integer,Object>> readExcelContent() throws Exception{ if(wb==null){ throw new Exception("Workbook对象为空!"); } Map<Integer, Map<Integer,Object>> content = new HashMap<Integer, Map<Integer,Object>>(); 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<Integer,Object> cellValue = new HashMap<Integer, Object>(); 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 = "F:test.xls"; ReadExcelUtils excelReader = new ReadExcelUtils(filepath); // 对读取Excel表格标题测试 // String[] title = excelReader.readExcelTitle(); // System.out.println("获得Excel表格的标题:"); // for (String s : title) { // System.out.print(s + " "); // } // 对读取Excel表格内容测试 Map<Integer, Map<Integer,Object>> map = excelReader.readExcelContent(); System.out.println("获得Excel表格的内容:"); for (int i = 1; i <= map.size(); i++) { System.out.println(map.get(i)); } } catch (FileNotFoundException e) { System.out.println("未找到指定路径的文件!"); e.printStackTrace(); }catch (Exception e) { e.printStackTrace(); } } }

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
