poi excel文档生成与读取

阿帕奇poi excel文档操作

    • 1. introduce
    • 2. 轮子
    • 3. demo 以九九乘法表为例
      • 3.1 xls的生成
      • 3.2 xlsx的生成
      • 3.3 读取xlsx

1. introduce

  1. poi是什么
    答:用于excel的操作的,可以对集合,map进行操作生成对应的excel文档。做报表。

  2. 对应的iText是pdf操作的

  3. excel的两种版本

    1. 1997~2003版本的
    2. 2003版本的
      区别:后缀名不同。某w*s就是03的。。

      xls用老版本的excel和新版本的excel软件都能打开。
      而新版本的excel文件不会向下兼容。

2. 轮子

	<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.9</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.9</version></dependency>

3. demo 以九九乘法表为例

3.1 xls的生成

   /*** 给定excel文件名 和 excel文档 生成excel文件* @param excelFileName* @param wb*/void createExcelFile(String excelFileName, Workbook wb) {// 生成文件,字节流输出File file = new File(excelFileName);OutputStream out = null;try {out = new FileOutputStream(file);try {wb.write(out);} catch (IOException e) {e.printStackTrace();}} catch (FileNotFoundException e) {e.printStackTrace();} finally {if (out != null) {try {out.close();} catch (IOException e) {e.printStackTrace();}}}}@Test/*** 生成xls的excel文档 97~03 年*/public void test2() {// 生成空白文档HSSFWorkbook hssfWorkbook = new HSSFWorkbook();// 生成sheet页HSSFSheet sheet1 = hssfWorkbook.createSheet("sheet page1");// 行for (int i = 0; i < 9; i++) {// 创建行HSSFRow row = sheet1.createRow(i);// 列for (int j = 0; j <= i; j++) {// 创建单元格HSSFCell cell = row.createCell(j);String res = (i + 1) + " * " + (j + 1) + " = " + (i + 1) * (j + 1);// 设置单元格内容cell.setCellValue(res);}}createExcelFile("excel1.xls", hssfWorkbook);}

3.2 xlsx的生成

换了个api

    /*** 生成xlsx 的文档*/@Testpublic void test3() {// 空白文档XSSFWorkbook xssfSheets = new XSSFWorkbook();// sheet页1XSSFSheet sheet1 = xssfSheets.createSheet("sheet1");// 行for (int i = 0; i < 9; i++) {// 创建行XSSFRow row = sheet1.createRow(i);// 列for (int j = 0; j <= i; j++) {// 创建单元格XSSFCell cell = row.createCell(j);String res = (i + 1) + " * " + (j + 1) + " = " + (i + 1) * (j + 1);// 设置单元格内容cell.setCellValue(res);}}createExcelFile("excel2.xlsx", xssfSheets);}

3.3 读取xlsx

    /*** 利用poi读取xlsx文件*/@Testpublic void test4() {try {InputStream in = new FileInputStream(new File("excel2.xlsx"));// 获得excel文档try {XSSFWorkbook xfb = new XSSFWorkbook(in);// sheet 页XSSFSheet sheet = xfb.getSheetAt(0);// 最大行的下标int lastRowNum = sheet.getLastRowNum();for (int i = 0; i < lastRowNum; i++) {// 拿到每行XSSFRow row = sheet.getRow(i);// 最大单元的行下标short cellNum = row.getLastCellNum();// 遍历单元格for (int j = 0; j < cellNum; j++) {// 获得单元格XSSFCell cell = row.getCell(j);if (cell != null)System.out.print(cell + " ");}System.out.println();}} catch (IOException e) {e.printStackTrace();}} catch (FileNotFoundException e) {e.printStackTrace();}}

最后,轮子的都得是,会用改,然后会用就行。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/420578.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

hdu4405:概率dp

题意&#xff1a; 总共有n1个格子&#xff1a;0-n 初始情况下在 0号格子 每次通过掷骰子确定前进的格子数 此外 还有一些传送门可以瞬间从 u 点传送到 v 点&#xff08;必须被传送&#xff09; 求走到&#xff08;或超过&#xff09;n点总共需要掷多少次骰子 分析&#xff1a; …

echarts生成图表

目录1. echarts是一个优秀的js绘图框架2. 如何使用echats框架绘图&#xff1f;3. 更多彩蛋1. echarts是一个优秀的js绘图框架 ECharts&#xff0c;一个使用 JavaScript 实现的开源可视化库&#xff0c;可以流畅的运行在 PC 和移动设备上&#xff0c;兼容当前绝大部分浏览器&…

HDU 1394 线段树or 树状数组~

Minimum Inversion Number   Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..., an, if we move the first m > 0 nu…

servlet,springmvc,springboot转发时页面静态资源404问题

目录不讨论静态资源过滤的问题。。。这个问题重定向不会404&#xff0c;因为重定向是找到了对应的页面&#xff0c;是浏览器决定的。 而转发在相同目录下转发会找到资源。但是从controller&#xff08;根目录&#xff09;里面转发到根目录的下面的目录&#xff0c;转发能过去&…

消除过期的对象引用

http://www.oak.hk/blog/2014/09/28/eliminate-obsolete-object-refrences/ 转载于:https://www.cnblogs.com/reader2012/p/4006299.html

mysql数据库面试总结

mysql数据库相关1. 数据库事务的四个特性及含义2. 视图的作用&#xff0c;视图可以更改么2.1 什么是视图&#xff0c;作用3. drop,delete与truncate的区别4. 索引的工作原理及其种类5. 连接查询的种类6. 数据库范式7. 数据库优化的思路7.1 sql语句的优化7.2 数据库结构优化7.3 …

SQL Server 中索引的禁用与删除

主题 1、 禁用索引 alter index index_name on table_name disable; 主题 2、 删除索引 drop index table_name.index_name; 转载于:https://www.cnblogs.com/JiangLe/p/4007095.html

关于split与StringTokenizer的理解

关于split与StringTokenizer的理解 一.split 依据匹配给定的正則表達式来拆分此字符串。此方法返回的数组包括此字符串的子字符串&#xff0c;每一个子字符串都由还有一个匹配给定表达式的子字符串终止&#xff0c;或者由此字符串末尾终止。数组中的子字符串按它们在此字符串中…

springboot, thymeleaf 教你快速搭建网站

目录项目结构国际化curd操作404页面拦截器地址&#xff1a; https://github.com/sevenyoungairye/spring-boot-study员工管理员系统&#xff0c;页面用html thymeleaf模板数据库用的是map集合&#xff0c;没用真实的数据库项目结构 国际化 默认中文 中文&#xff0c;英文切换…

springboot 整合druid

目录1. maven依赖2. yml配置3. druid配置类编写4. 后台性能监控https://github.com/sevenyoungairye/spring-boot-studydruid优点&#xff1a;提供性能监控&#xff0c;配置灵活丰富 1. maven依赖 <!-- mysql驱动 springboot内置 --><dependency><groupId>…

Ubuntu 查看默认软件安装位置

tags: Linux 方法 1&#xff1a;在命令行输入&#xff1a;dpkg -L 软件包名&#xff1b;方法 2&#xff1a;在/var/cache/apt/archives找的你安装程序的包&#xff0c;然后用gdebi-gtk软件包名可以查看具体安装在什么位置。转载于:https://www.cnblogs.com/svitter/p/4011433.h…

maven 项目管理和构建工具

mvn1. maven 是什么2. maven能解决什么问题3. maven 需要配置和下载4. 使用eclipse创建maven项目5. xml依赖配置 作用范围6. maven的常用命令1. maven 是什么 Maven 在美国是一个口语化的词语&#xff0c;代表专家、内行的意思&#xff0c; Maven是一个项目管理工具&#xff0…

大数据----基于sogou.500w.utf8数据的MapReduce编程

目录 一、前言二、准备数据三、编程实现3.1、统计出搜索过包含有“仙剑奇侠传”内容的UID及搜索关键字记录3.2、统计rank<3并且order>2的所有UID及数量3.3、上午7-9点之间&#xff0c;搜索过“赶集网”的用户UID3.4、通过Rank&#xff1a;点击排名 对数据进行排序 四、参…

Java源代码分析与生成

源代码分析&#xff1a;可使用ANTLRANTLR是开源的语法分析器&#xff0c;可以用来构造自己的语言&#xff0c;或者对现有的语言进行语法分析。JavaParser 对Java代码进行分析CodeModel 用于生成Java代码(但对于已有代码的支持可能有问题) 转载于:https://www.cnblogs.com/laoni…

springboot 整合mybatis实现curd

springboot 整合mybatispom文件mvc 架构application.properties 扩展配置&#xff0c;druid配置类项目地址&#xff1a;https://github.com/sevenyoungairye/spring-boot-study/tree/main/springboot-mybatis-05pom文件 <!--整合mybatis--><dependency><groupId…

关于android 调用网页隐藏地址栏

首先创建项目&#xff0c;在main.xml里 添加好WebView控件R.id为webview1。 HelloWebView.java 代码 package liu.ming.com; import android.app.Activity;import android.os.Bundle;import android.view.KeyEvent;import android.webkit.WebView;import android.webkit.WebVie…

spring security 认证与权限控制

目录1. 使用授权和认证的必要性2. spring security 与 shiro 与 过滤器&#xff0c;拦截器3. 具体配置使用项目地址&#xff1a;https://github.com/sevenyoungairye/spring-boot-study/tree/main/springboot-security-061. 使用授权和认证的必要性 什么是安全框架&#xff0c…