java poi读取2003或2007版本excl为list.docx_第1页
java poi读取2003或2007版本excl为list.docx_第2页
java poi读取2003或2007版本excl为list.docx_第3页
java poi读取2003或2007版本excl为list.docx_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

MAVEN配置org.apache.poipoi3.14org.apache.poipoi-ooxml3.14Java代码本段代码主要是读取2003和2007版本的excl文件,如果要读取所有sheet的文本内容,可以在这基础上循环sheet,做些简单的封装即可。package com.youku.core.utils;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import mons.fileupload.FileItem;import mons.io.FilenameUtils;import mons.lang.StringUtils;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;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;/* * Excel组件 * * author sunyue * version 2.0 * since 2.0 */public abstract class ExcelHelper /* * Excel 2003 */private final static String XLS = xls;/* * Excel 2007 */private final static String XLSX = xlsx;/* * 由Excel文件的Sheet导出至List * * param file * param sheetNum * return */public static ListList readExcel(FileItem file, int sheetNum) throws IOException String extensionName = FilenameUtils.getExtension(file.getName();InputStream is = file.getInputStream();return readExcel(is, extensionName, sheetNum);/* * 读取excl文件 * * param is * param extensionName * param sheetNum * return * throws IOException */SuppressWarnings(resource)private static ListList readExcel(InputStream is, String extensionName, int sheetNum)throws IOException if (extensionName.toLowerCase().equals(XLS) Workbook workbook = new HSSFWorkbook(is);Sheet sheet = workbook.getSheetAt(sheetNum);return getXlsSheetList(sheet); else if (extensionName.toLowerCase().equals(XLSX) XSSFWorkbook workbook = new XSSFWorkbook(is);XSSFSheet sheet = workbook.getSheetAt(sheetNum);return getXlsxSheetList(sheet);elsereturn null;/* * 读取XLS后缀excl * * param workbook * param sheetNum * return * throws IOException */private static ListList getXlsSheetList(Sheet sheet) ListList result = new ArrayListList();int minRow = sheet.getFirstRowNum();int maxRow = sheet.getLastRowNum();for (int rowIx = minRow; rowIx = maxRow; rowIx+) List list = new ArrayList();Row row = sheet.getRow(rowIx);int minCell = row.getFirstCellNum();int maxCell = row.getLastCellNum();for (int ci = minCell; ci maxCell; ci+) Cell cell = row.getCell(ci);if (cell != null) cell.setCellType(XSSFCell.CELL_TYPE_STRING);String cellVal = cell.getStringCellValue();if(StringUtils.isNotBlank(cellVal)list.add(cellVal);elselist.add();elselist.add();result.add(list);return result;/* * 读取XLSX后缀excl * * param workbook * param sheetNum * return * throws IOException */private static ListList getXlsxSheetList(XSSFSheet sheet) ListList result = new ArrayListList();try int minRow = sheet.getFirstRowNum();int maxRow = sheet.getLastRowNum();for (int rowIx = minRow; rowIx = maxRow; rowIx+) List list = new ArrayList();XSSFRow row = sheet.getRow(rowIx);int minCell = row.getFirstCellNum();int maxCell = row.getLastCellNum();for (int ci = minCell; ci maxCell; ci+) XSSFCell cell = row.getCell(ci);if (cell != null) cell.setCellType(XSSFCell.CELL_TYPE_STRING);String cellVal = cell.getStringCellValue();

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论