有时候涉及到数据的批量导入导出,就需要用代码去操作excel了


  • 首先去Apache 官网下载jar包并解压 ,导入到libs中 下载sdk

    • 项目结构如图
  • 或者采用Maven的形式引入
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.0.1</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.0.1</version></dependency>
  • 读文件
public class ReadExcel {/*** * @param path 传入文件路径*/public static void readExcel(String path) {File file = new File(path);FileInputStream fis = null;Workbook workBook = null;if (file.exists()) {try {fis = new FileInputStream(file);//创建工作簿workBook = WorkbookFactory.create(fis);//获取共有多少张表int numberOfSheets = workBook.getNumberOfSheets();System.out.println("共有" + numberOfSheets + "张工作表");// sheet工作表for (int s = 0; s < numberOfSheets; s++) {//获取当前需要操作的表Sheet sheetAt = workBook.getSheetAt(s);//获取工作表名称String sheetName = sheetAt.getSheetName();System.out.println("工作表名称:" + sheetName);// 获取当前Sheet的总行数int rowsOfSheet = sheetAt.getPhysicalNumberOfRows();if (rowsOfSheet == 0) {continue;}over:for (int r = 0; r < sheetAt.getLastRowNum(); r++) {Row row = sheetAt.getRow(r);if (row == null) {continue;}if (r==0){// 第一行int physicalNumberOfCells = row.getPhysicalNumberOfCells();String[] title = new String[physicalNumberOfCells];for (int i = 0; i < physicalNumberOfCells; i++) {title[i] = row.getCell(i).getStringCellValue();if (i == 0) {System.out.print(title[i] + "|");} else {System.out.println(title[i] + "|");}}continue ;}for (int c= 0; c < row.getLastCellNum(); c++) {// 总列(格)Cell cell = row.getCell(c);if (cell==null){break over;}/*** cell.getCellType() 获取每个单元格的数据类型,调用不同的方法* public enum CellType {*     _NONE(-1),*     NUMERIC(0),*     STRING(1),*     FORMULA(2),*     BLANK(3),*     BOOLEAN(4),*     ERROR(5);*     }*/switch (cell.getCellType()) {case STRING:System.out.print(cell.getStringCellValue()+ "|");break;case NUMERIC:long phoneNum = (long) cell.getNumericCellValue();System.out.println(phoneNum + "|");break;default:System.out.println("数据错误!");}}}}} catch (Exception e) {e.printStackTrace();} finally {try {if (fis != null) {fis.close();}if (workBook != null) {workBook.close();}} catch (IOException e) {e.printStackTrace();}}} else {System.out.println("文件不存在!");}}
}
public class Main {public static void main(String[] args) {ReadExcel.readExcel("test_2003.xls");//ReadExcel.readExcel("test_2007.xlsx");}
}

执行结果如下:


  • 写入数据到表格

 class WriteExcel {public static void writeExcel() {//创建工作簿Workbook wb = new HSSFWorkbook();//创建工作表Sheet sheet = wb.createSheet("通讯录");Row title = sheet.createRow(0);Cell titleCell = title.createCell(0);titleCell.setCellValue("通讯录");//创建表头Row titleRow = sheet.createRow(1);Cell cellTitle0 = titleRow.createCell(0);cellTitle0.setCellValue("用户名");Cell cellTitle1 = titleRow.createCell(1);cellTitle1.setCellValue("手机号");for (int i = 2; i < 6; i++) {//创建单元行Row row = sheet.createRow(i);for (int j = 0; j <2; j++) {Cell cell = row.createCell(j);if (j==0){cell.setCellValue("ITplus"+i);}else {cell.setCellValue("1330455123"+i);}}}try (FileOutputStream fos = new FileOutputStream("createExcel.xls")) {//路径需要存在wb.write(fos);wb.close();System.out.println("写数据结束!");} catch (IOException e) {System.out.println("写数据异常!");e.printStackTrace();}}
}
public class Main {public static void main(String[] args) {WriteExcel.writeExcel();}
}

执行效果如图:


纯属于个人学习用,具体拓展要根据需求而定;不过数据的写入和输出都在Cell接口中操作

public interface Cell {int getColumnIndex();int getRowIndex();Sheet getSheet();Row getRow();/** @deprecated */@Deprecated@Removal(version = "5.0")void setCellType(CellType var1);void setBlank();CellType getCellType();/** @deprecated */@Deprecated@Removal(version = "4.2")CellType getCellTypeEnum();CellType getCachedFormulaResultType();/** @deprecated */@Deprecated@Removal(version = "4.2")CellType getCachedFormulaResultTypeEnum();void setCellValue(double var1);void setCellValue(Date var1);void setCellValue(LocalDateTime var1);default void setCellValue(LocalDate value) {this.setCellValue(value == null ? null : value.atStartOfDay());}void setCellValue(Calendar var1);void setCellValue(RichTextString var1);void setCellValue(String var1);void setCellFormula(String var1) throws FormulaParseException, IllegalStateException;void removeFormula() throws IllegalStateException;String getCellFormula();double getNumericCellValue();Date getDateCellValue();LocalDateTime getLocalDateTimeCellValue();RichTextString getRichStringCellValue();String getStringCellValue();void setCellValue(boolean var1);void setCellErrorValue(byte var1);boolean getBooleanCellValue();byte getErrorCellValue();void setCellStyle(CellStyle var1);CellStyle getCellStyle();void setAsActiveCell();CellAddress getAddress();void setCellComment(Comment var1);Comment getCellComment();void removeCellComment();Hyperlink getHyperlink();void setHyperlink(Hyperlink var1);void removeHyperlink();CellRangeAddress getArrayFormulaRange();boolean isPartOfArrayFormulaGroup();
}

poi解析excel文件(支持xls和xlsx)java学习版相关推荐

  1. python3 根据sql导出excel文件 支持xls和xlsx

    python3 根据sql导出excel文件 支持xls和xlsx 代码简介: sql_output_excel函数是用来导出excel文件的,其他两个函数是导出xls和xlsx格式的. 使用pymy ...

  2. Java用poi解析Excel,支持xls/xlsx

    由于JXL不能支持.xlsx的Excel文件,因此采用POI. 这里要用到的Jar包只需要:org.apache.poi 3.15和org.apache.poi-ooxml 3.15 在POI中,解析 ...

  3. Android使用POI操作excel(支持xls和xlsx)

    一.说明及准备工作 1.用于AS使用POI读取.创建.另存excel,兼容xlsx和xls格式 2.下载poi-3.12-android-a.jar.poi-ooxml-schemas-3.12-20 ...

  4. Java面试poi中excel版本大小_java 中 poi解析Excel文件版本问题解决办法

    java 中 poi解析Excel文件版本问题解决办法 发布时间:2020-10-02 03:46:15 来源:脚本之家 阅读:91 作者:程诺 poi解析Excel文件版本问题解决办法 poi解析E ...

  5. poi解析Excel文件版本问题

    poi解析Excel文件时有两种格式: HSSFWorkbook格式用来解析Excel2003(xls)的文件 XSSFWorkbook格式用来解析Excel2007(xlsx)的文件 如果用HSSF ...

  6. java解析excel报错,poi解析excel文件报错

    getFileMagic() only operates on streams which support mark(int) 使用 bis 解决 BufferedInputStream bis = ...

  7. java使用poi解析Excel文件

    本文取自http://www.cnblogs.com/hongten/p/java_poi_excel_xls_xlsx.html java中读取Excel文件并解析 Excel2007及以前的文件使 ...

  8. Android 读取excel (支持 xls和xlsx)

    最近公司项目需要Android应用读取excel文件内容,所以就找了相关资料,找到两种读取excel文件的方法,下面为大家介绍: 一.jxl 读取excel文件 1.1.添加依赖: implement ...

  9. 使用POI导出Excel,以及xls和xlsx格式问题

    /*--------------------------------------导出开始--------------------------------------------------*/@Ove ...

  10. Python将excel文件从xls转换为xlsx

    本文使用场景:将一个xls格式Excel文件转换为xlsx文件格式.接下来将一步一步演示该操作.你也可以对代码进行修改使其适用于你所需的场景. 安装Python3 首先需要安装Python,我这里安装 ...

最新文章

  1. Zabbix的问题日志清单查看
  2. 计算机英语语言学考研真题,考研类试卷英语专业语言学历年真题试卷汇编14
  3. java阅读题_java 练习题带答案
  4. cd返回上一 git_PHP项目中应用CI/CD的碎碎恋!
  5. 构造函数和实例对象之间的关系 构造函数创建对象带来的问题 原型
  6. php tiff,在PHP中将tiff转换为jpg?
  7. php查询表导出excel文件路径,Thinkphp5如何将导出的excel表格存储到服务器中项目目录的指定目录中?...
  8. Java集合之HashMap源码分析
  9. mysql数据库事务的概念_如何理解数据库事务中的一致性的概念?
  10. electron-vue使用electron-updater实现自动更新
  11. 虚拟化四路服务器,IDC:4路及8路服务器现状未来趋势分析
  12. js简单正则表达式验证密码
  13. 这个情人节,工程师用阿里云来试着表达不一样的爱意 1
  14. 第三版新视野大学英语读写教程4结业考点(1,2,3,5,6单元)
  15. python 几行代码实现自动回复功能
  16. sohu_news搜狐新闻类型分类
  17. java8实战:使用流收集数据之toList、joining、groupBy(多字段分组)(1)
  18. shell脚本:删除文本中的字母、找单词、筛选,匹配,删除,替换
  19. 【笔记】Oracle的除法并保留两位小数点
  20. 谷歌临时工成二等公民 ,部分人年薪仅 3万美元

热门文章

  1. 细胞自动机 java_中国MOOC_面向对象程序设计——Java语言_期末考试编程题_1细胞自动机...
  2. cmake-3.17 cmake-3.18.2下载
  3. 一步步学习java后台(一)(IDEA, Spring, Maven, MyBatis)
  4. 先锋意识dota人生
  5. 倍福plc的型号_倍福模块选型
  6. 我们分析了5万多场英雄联盟比赛,教你如何轻松用Python预测胜负
  7. List集合排序的几种方式
  8. Docker cgroups资源控制
  9. 人工智能机器学习模型构建数据集猫狗数据集(cats_and_dogs_filtered.zip)数据集百度网盘下载地址
  10. 西门子S7-300 PLC视频教程(百度网盘)