excel2003 与 excel2007 导入
package com.chen.ExcelUtil; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.poi.POIXMLDocument; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; 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; public class ReadExcel { /** 错误信息 */ private String errorInfo; // ------------------test---------------------- public static void main(String[] args) { ReadExcel readExcel = new ReadExcel(); String filePath = "C:\\Users\\Administrator\\Desktop\\123\\personInfo\\personInfo.xlsx"; if(readExcel.validateExcel(filePath)){ Workbook wb = readExcel.read(filePath); readExcel.read(wb); }else{ System.out.println("不是excel文件!"); } } /** * 验证EXCEL文件是否合法 */ public boolean validateExcel(String filePath) { /** 判断文件名是否为空或者文件是否存在 */ if (!CEVUtil.fileExist(filePath)) { errorInfo = "文件不存在"; return false; } /** 检查文件是否是Excel格式的文件 */ if (!CEVUtil.isExcel(filePath)) { errorInfo = "文件名不是excel格式"; return false; } return true; } /** * @描述:根据文件名读取excel文件 */ public Workbook read(String filePath) { // List<List<String>> dataLst = new ArrayList<List<String>>(); InputStream is = null; Workbook wb = null; try { /** 验证文件是否合法 */ if (!validateExcel(filePath)) { System.out.println(errorInfo); return null; } /** 判断文件的类型,是2003还是2007 */ boolean isExcel2003 = true; if (CEVUtil.isExcel2007(filePath)) { isExcel2003 = false; } /** 调用本类提供的根据流读取的方法 */ is = new FileInputStream(new File(filePath)); if (isExcel2003) { wb = new HSSFWorkbook(is); } else { wb = new XSSFWorkbook(is); } is.close(); } catch (IOException e) { e.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { is = null; e.printStackTrace(); } } } return wb; } /** * @描述:读取数据 */ @SuppressWarnings("unused") private List<List<String>> read(Workbook wb) { List<List<String>> dataLst = new ArrayList<List<String>>(); /** 得到总的shell */ int sheetAccount = wb.getNumberOfSheets(); /** 得到第一个shell */ Sheet sheet = wb.getSheetAt(0); /** 得到Excel的行数 */ int rowCount = sheet.getPhysicalNumberOfRows(); /** 也可以通过得到最后一行数 */ int lastRowNum = sheet.getLastRowNum(); /** 循环Excel的行 */ for (int r = 0; r < rowCount; r++) { Row row = sheet.getRow(r); if (row == null) { continue; } List<String> rowLst = new ArrayList<String>(); /** 循环Excel的列 */ for (int c = 0; c < row.getPhysicalNumberOfCells(); c++) { Cell cell = row.getCell(c); String cellValue = ""; if (null != cell) { // 以下是判断数据的类型 switch (cell.getCellType()) { // XSSFCell可以达到相同的效果 case HSSFCell.CELL_TYPE_NUMERIC: // 数字 double d = cell.getNumericCellValue(); if (HSSFDateUtil.isCellDateFormatted(cell)) {// 日期类型 // Date date = cell.getDateCellValue(); Date date = HSSFDateUtil.getJavaDate(d); cellValue = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss").format(date); } else {// 数值类型 cellValue = cell.getNumericCellValue() + ""; } cellValue = cell.getDateCellValue() + ""; break; case HSSFCell.CELL_TYPE_STRING: // 字符串 cellValue = cell.getStringCellValue(); break; case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean cellValue = cell.getBooleanCellValue() + ""; break; case HSSFCell.CELL_TYPE_FORMULA: // 公式 cellValue = cell.getCellFormula() + ""; break; case HSSFCell.CELL_TYPE_BLANK: // 空值 cellValue = ""; break; case HSSFCell.CELL_TYPE_ERROR: // 故障 cellValue = "非法字符"; break; default: cellValue = "未知类型"; break; } } System.out.print(cellValue + "\t"); rowLst.add(cellValue); } System.out.println(); dataLst.add(rowLst); } return dataLst; } } /** * 工具类:判断是否为Excel文件,并检查Excel版本 * * @author javaloveiphone * */ class CEVUtil { /** * 依据后缀名判断读取的是否为Excel文件 * * @param filePath * @return */ public static boolean isExcel(String filePath) { if (filePath.matches("^.+\\.(?i)(xls)$") || filePath.matches("^.+\\.(?i)(xlsx)$")) { return true; } return false; } /** * 检查文件是否存在 */ public static boolean fileExist(String filePath) { if (filePath == null || filePath.trim().equals("")) return false; File file = new File(filePath); if (file == null || !file.exists()) { return false; } return true; } /** * 依据内容判断是否为excel2003及以下 */ public static boolean isExcel2003(String filePath) { try { BufferedInputStream bis = new BufferedInputStream( new FileInputStream(filePath)); if (POIFSFileSystem.hasPOIFSHeader(bis)) { System.out.println("Excel版本为excel2003及以下"); return true; } } catch (IOException e) { e.printStackTrace(); return false; } return false; } /** * 依据内容判断是否为excel2007及以上 */ public static boolean isExcel2007(String filePath) { try { BufferedInputStream bis = new BufferedInputStream( new FileInputStream(filePath)); if (POIXMLDocument.hasOOXMLHeader(bis)) { System.out.println("Excel版本为excel2007及以上"); return true; } } catch (IOException e) { e.printStackTrace(); return false; } return false; } }
相关推荐
c#ExceL导入支持2003和2007两种导入,里面有说明和案例,还有遇到问题的解决方法。
springboot 导出excel 导入excel 生成excel 内容有点多 springboot 导出excel 导入excel 生成excel 内容有点多 springboot 导出excel 导入excel 生成excel 内容有点多 springboot 导出excel 导入excel 生成excel ...
1.CSV文件转换 CsvHelper CSV文件导入DataTable和DataTable导出到Csv文件等操作 2.导出Excel 操作类 DataToExcel 从“Excel导出数据的帮助类 ... 支持导入excel 2003、excel 2007、excel 2010 等各种版本
本人从网上搜集资料,加上自己研究探索,现在完成了用Jxls导入导出Excel2003和Excel2007数据,读取和写入xls和xlsx文件,现把结果告诉大家,希望大家不走弯路,直接掌握先进实用的技术,解决实际工作问题。...
本人从网上搜集资料,加上自己研究探索,现在完成了用Jxls导入导出Excel2003和Excel2007数据,读取和写入xls和xlsx文件,现把结果告诉大家,希望大家不走弯路,直接掌握先进实用的技术,解决实际工作问题。...
通过poi操作excel文档,获取excel文档中的数据导入到数据库中
本人从网上搜集资料,加上自己研究探索,现在完成了用Jxls导入导出Excel2003和Excel2007数据,读取和写入xls和xlsx文件,现把结果告诉大家,希望大家不走弯路,直接掌握先进实用的技术,解决实际工作问题。...
Thinkphp5整合excel导入导出Thinkphp5整合excel导入导出Thinkphp5整合excel导入导出Thinkphp5整合excel导入导出Thinkphp5整合excel导入导出Thinkphp5整合excel导入导出Thinkphp5整合excel导入导出Thinkphp5整合excel...
thinkhphp3.2 excel导入导出demothinkhphp3.2 excel导入导出demothinkhphp3.2 excel导入导出demothinkhphp3.2 excel导入导出demothinkhphp3.2 excel导入导出demothinkhphp3.2 excel导入导出demothinkhphp3.2 excel...
本软件主要功能是导入,导出EXCEL文档(支持2003,2007),包括全部源代码,请下载~!
POI导入excel大数据处理,支持excel2003,2007, 有例子,可以直接运行
.net开发,网页excel导入导出功能模板,网页表格直接导出为excel 2003和2007格式。
DataSet_read_Excel导入2003,2007excel,xls,xlsx DataSet 导入 2007excel xls xlsx
NULL 博文链接:https://mjm13.iteye.com/blog/1386591
NULL 博文链接:https://fsh430623.iteye.com/blog/1318545
C#将数据导入excel和Excel数据导入数据库使用的是SQL Server数据库
本人从网上搜集资料,加上自己研究探索,现在完成了用Jxls导入导出Excel2003和Excel2007数据,读取和写入xls和xlsx文件,现把结果告诉大家,希望大家不走弯路,直接掌握先进实用的技术,解决实际工作问题。...
java使用poi进行excel2003、excel2007导入,返回List[]>,实测可用 xbean-2.2.0.jar包 与 xmlbeans-2.3.0.jar包冲突 需删除xbean-2.2.0.jar包
VB EXCEL导入 EXCEL导入 EXCEL导入