Java 读取Excel(兼容两种格式)
2017-02-10 16:26
441 查看
本文将为您简单介绍java读取excel的方法
本文将为您简单介绍java读取excel的方法你可能需要了解
你可能需要预先准备
接下来动手敲一敲
片尾留注
你可能需要了解
Java读写Excel的包是Apache POI,因此需要先获取POI的jar包,下载地址: [ 点我下载 ]Excel2003 与Excel2007区别较大,最直观的就是两者的后缀不同,前者后缀为.xls,后者为.xlsx
读取“.xls”格式使用 import org.apache.poi.hssf.usermodel.*;包的内容,例如:HSSFWorkbook
读取“.xlsx”格式使用 import org.apache.poi.xssf.usermodel.*; 包的内容,例如:XSSFWorkbook
读取两种格式使用 import org.apache.poi.ss.usermodel.* 包的内容,例如:Workbook
你可能需要预先准备
新建JavaProject->新建lib文件夹->导入以下jar包接下来动手敲一敲
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; 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.XSSFWorkbook; import org.apache.xmlbeans.impl.piccolo.io.FileFormatException; public class ExcelReadTest { private static final String EXTENSION_XLS = "xls"; private static final String EXTENSION_XLSX = "xlsx"; public static void main(String[] args) { ExcelReadTest excelReadTest = new ExcelReadTest(); try { //根据实际情况加载excel文件 //excelReadTest.readExcel("test.xls"); excelReadTest.readExcel("test.xlsx"); } catch (FileNotFoundException | FileFormatException e) { e.printStackTrace(); } } /** * 获取workbook对象(xls和xlsx对象不同,不过都是Workbook的实现类) * @param filePath * @return * @throws IOException */ private Workbook getWorkbook(String filePath) throws IOException{ Workbook workbook = null; InputStream iStream = new FileInputStream(filePath); if(filePath.endsWith(EXTENSION_XLS)){ workbook = new HSSFWorkbook(iStream); }else if(filePath.endsWith(EXTENSION_XLSX)){ workbook = new XSSFWorkbook(iStream); } return workbook; } /** * 文件检查 * @param filePath * @throws FileNotFoundException * @throws FileFormatException */ private void preReadCheck(String filePath) throws FileNotFoundException, FileFormatException{ File file = new File(filePath); if(!file.exists()){ throw new FileNotFoundException("传入的文件不存在:"+filePath); } if(!(filePath.endsWith(EXTENSION_XLS) || filePath.endsWith(EXTENSION_XLSX))){ throw new FileFormatException("传入的文件不是Excel"); } } public void readExcel(String filePath) throws FileNotFoundException, FileFormatException { //1.检查文件 this.preReadCheck(filePath); //2.获取workbook对象 Workbook workbook = null; try { workbook = this.getWorkbook(filePath); //读文件 int sheetCount = workbook.getNumberOfSheets();//获取sheet数量 for(int i = 0;i<sheetCount;i++){ Sheet sheet = workbook.getSheetAt(i); if(sheet==null){ continue; } int rowStart = sheet.getFirstRowNum(); int rowEnd = sheet.getLastRowNum(); Row row = null; for(int j = rowStart ;j<rowEnd;j++){ row = sheet.getRow(j); int cellStart = row.getFirstCellNum(); int cellEnd = row.getLastCellNum(); Cell cell = null; for(int m = cellStart;m<cellEnd;m++){ 4000 cell = row.getCell(m); //获取单元格的值 System.out.print(this.getCellValue(cell, true)+"-"); } System.out.println(""); } } } catch (IOException e) { e.printStackTrace(); } } @SuppressWarnings("deprecation") private String getCellValue(Cell cell,boolean treatAsStr){ if(cell==null){ return ""; } if(treatAsStr){ //将其他格式转换为文本格式来读取 cell.setCellType(Cell.CELL_TYPE_STRING); } if(cell.getCellType()==Cell.CELL_TYPE_BOOLEAN){ return String.valueOf(cell.getBooleanCellValue()); }else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){ return String.valueOf(cell.getNumericCellValue()); }else{ return String.valueOf(cell.getStringCellValue()); } } }
片尾留注
*本文部分引用了 幻影 的代码*以上代码经个人实际测试有效,可以引用测试
*本篇文章的由来:
项目需求:“通过在表单提交时(POST方式)获取Excel文件并进行导入数据库操作”
相关文章推荐
- java读取两种格式(xls、xlsx)的Excel
- Java读取excel(兼容03和07格式)
- java读取Excel的方法,解析xls、xlsx两种格式
- java读取Excel的方法,解析xls、xlsx两种格式
- Java读取excel(兼容03和07格式)
- 通过POI统一读取Excel文件(兼容97-2003和2007+两种格式)
- java读取Excel的方法,解析xls、xlsx两种格式
- 通过POI统一读取Excel文件(兼容97-2003和2007+两种格式)
- Java读取Excel的方法,解析xls、xlsx两种格式
- java读取Excel的方法,解析xls、xlsx两种格式
- java 读取 Excel 读取兼容2003,2007
- Java中通过POI读取Excel 2003 - 2007的xls,xlsx格式
- Java读取Excel的两种方式方式介绍
- java使用poi.3.10读取excel 2003 (xls格式)
- poi导入导出excel文件,兼容.xls和.xlsx两种格式
- JAVA读取Excel兼容2003、2007
- POI读取加密的EXCEL(兼容XLS,XLSX格式)
- Java读取Excel 兼容2007以上以及2003版本
- java读取excel(带时间格式的)的内容并展出出结果
- Java学习-013-文本文件读取实例源代码(两种数据返回格式)