读取excel文件信息实例2
来源:互联网 发布:ec软件 编辑:程序博客网 时间:2024/06/05 20:48
package excel;import java.io.FileInputStream;import java.io.InputStream;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.List;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;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.DateUtil;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 testExcel {private XSSFWorkbook xssfWorkbook;private HSSFWorkbook hSSFWorkbook;public List<List<String>> readXls(String path) throws Exception{InputStream is = new FileInputStream(path);hSSFWorkbook = new HSSFWorkbook(is);List<List<String>> result = new ArrayList<List<String>>();//循环每一页,并处理当前循环页for(int numSheet=0;numSheet<hSSFWorkbook.getNumberOfSheets();numSheet++){//这里的HSSFSheet表示某一页HSSFSheet hssfSheet = hSSFWorkbook.getSheetAt(numSheet);if(hssfSheet == null){continue;}//处理当前页,循环读取每一行for(int rowNum=1;rowNum<=hssfSheet.getLastRowNum();rowNum++){//HSSFRow表示行HSSFRow hssfRow = hssfSheet.getRow(rowNum);int minColIx = hssfRow.getFirstCellNum();int maxColIx = hssfRow.getLastCellNum();List<String> rowList = new ArrayList<String>();//遍历该行,获取处理每个cell元素for(int colIx =minColIx;colIx<maxColIx;colIx++){//HSSFCell表示单元格HSSFCell cell = hssfRow.getCell(colIx);if(cell==null){continue;}rowList.add(getStringVal(cell));}result.add(rowList);}}return result;}public List<List<String>> readXlsx(String path) throws Exception{InputStream is = new FileInputStream(path);xssfWorkbook = new XSSFWorkbook(is);List<List<String>> result = new ArrayList<List<String>>();//循环每一页,并处理当前循环页for(XSSFSheet xssfSheet : xssfWorkbook){if(xssfSheet == null){continue;}//处理当前页,循环读取每一行for(int rowNum=1;rowNum<=xssfSheet.getLastRowNum();rowNum++){XSSFRow xssfRow = xssfSheet.getRow(rowNum);int minColIx = xssfRow.getFirstCellNum();int maxColIx = xssfRow.getLastCellNum();List<String> rowList = new ArrayList<String>();//遍历该行,获取处理每个cell元素for(int colIx =minColIx;colIx<maxColIx;colIx++){//HSSFCell表示单元格XSSFCell cell = xssfRow.getCell(colIx);if(cell==null){continue;}//rowList.add(cell.toString());rowList.add(getStringVal(cell));}result.add(rowList);}}return result;} public String getStringVal(Cell cell){ switch(cell.getCellType()){ case Cell.CELL_TYPE_BOOLEAN: return cell.getBooleanCellValue()?"TRUE":"FALSE"; case Cell.CELL_TYPE_FORMULA: return cell.getCellFormula(); case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(cell.getDateCellValue()); } cell.setCellType(Cell.CELL_TYPE_STRING);; case Cell.CELL_TYPE_STRING: return cell.getStringCellValue(); default: return ""; } } /* * 此方法与上方法对比发现是重载关系,此方法中的参数实现了上一方法的接口类型的参数,这两个方法不糊冲突 * 实际使用的时候回自动选择 */ public String getStringVal(XSSFCell cell){ switch(cell.getCellType()){ case Cell.CELL_TYPE_BOOLEAN: return cell.getBooleanCellValue()?"TRUE":"FALSE"; case Cell.CELL_TYPE_FORMULA: return cell.getCellFormula(); case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(cell.getDateCellValue()); } cell.setCellType(Cell.CELL_TYPE_STRING); case Cell.CELL_TYPE_STRING: return cell.getStringCellValue(); default: return ""; } }public static void main(String[] args) {testExcel t = new testExcel();try {List<List<String>> list = t.readXlsx("E:\\test\\excel\\test.xlsx");System.out.println(list);List<List<String>> list1 = t.readXls("E:\\test\\excel\\test.xls");System.out.println(list1);} catch (Exception e) {e.printStackTrace();}}}
阅读全文
1 0
- 读取excel文件信息实例2
- 读取excel文件中的信息实例1
- [ahk]读取excel文件实例
- php读取excel文件的数据信息
- Workbook解析excel文件 读取信息
- 文件读取实例2
- PHPExcel读取excel文件2
- thinkphp3.2读取Excel文件
- PHP读取EXCEL文件(包括资源型的图片信息)
- java上传excel文件并读取表格信息
- Java 读取Excel实例
- PHPExcel生成和读取Excel文件实例程序
- 模拟XML文件信息的读取并创建实例
- Java读取Excel表格信息。
- 读取Excel文件
- 读取Excel文件
- 读取excel文件
- 读取Excel文件
- hdu 6085
- 【编程语言学习】C++ Primer 对constexpr 和常量表达式的讲解
- HashMap的实现原理
- C 一一 static和extern关键字详解
- 使用python fit,predict出现的错误
- 读取excel文件信息实例2
- 并发基本概念介绍
- python基础——函数编程
- javascript随机数
- Surrounded_Regions
- 混合云起航
- HDU5493 Queue(线段树)
- ubuntu安装redis及使用
- 防盗链的基本原理与实现