使用poi写入Excel

news/2024/10/24 2:31:51/

Excel版本:

03版本结尾为xxx.xls 最高只有65536行
07版本结尾为xxx.xlsx 行数无限制

工作簿----->工作表----->行----->列
在这里插入图片描述

创建一个maven项目并导入poi依赖

导入03版本和07版本的依赖
时间格式化和测试工具

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zkd</groupId><artifactId>poi</artifactId><version>1.0-SNAPSHOT</version><dependencies>
<!--        xls(03)--><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.9</version></dependency><!--        xlsx(07)--><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.9</version></dependency><!--日期格式化工具--><dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId><version>2.10.1</version></dependency><!--test--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies>
</project>

03版本(.xls)代码:

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.joda.time.DateTime;
import org.junit.Test;import java.io.FileOutputStream;public class Text {//文件生成路径String path="D:\\idea_gzkj\\poi\\";//03版本@Testpublic void text03() throws  Exception{//1.创建一个工作簿Workbook workbook=new HSSFWorkbook();//2.创建一个工作表Sheet sheet = workbook.createSheet("统计表");//列:x 行:y//(0,0)//3.创建一行 0代表第一行Row row1=sheet.createRow(0);//4.创建一个单元格Cell cell01=row1.createCell(0);cell01.setCellValue("姓名");//(1,0)第一行第二列Cell cell02=row1.createCell(1);cell02.setCellValue("时间");//(0,1)Row row2=sheet.createRow(1);//行Cell cell03=row2.createCell(0);//列cell03.setCellValue("张三");//(1,1)Cell cell04=row2.createCell(1);String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");cell04.setCellValue(time);//生成一张表(IO流) 03版本使用xls结尾:FileOutputStream fileOutputStream=new FileOutputStream(path+"统计文件.xls");//写workbook.write(fileOutputStream);//关闭fileOutputStream.close();System.out.println("03版本写入完毕");}
}

效果:

在这里插入图片描述

07版本(.xlsx)代码

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.joda.time.DateTime;
import org.junit.Test;import java.io.FileOutputStream;public class Text {//文件生成路径String path="D:\\idea_gzkj\\poi\\";//07版本@Testpublic void text07() throws  Exception{//1.创建一个工作簿  07XSSFWorkbookWorkbook workbook=new XSSFWorkbook();//2.创建一个工作表Sheet sheet = workbook.createSheet("统计表");//列:x 行:y//(0,0)//3.创建一行 0代表第一行Row row1=sheet.createRow(0);//4.创建一个单元格Cell cell01=row1.createCell(0);cell01.setCellValue("姓名");//(1,0)第一行第二列Cell cell02=row1.createCell(1);cell02.setCellValue("时间");//(0,1)Row row2=sheet.createRow(1);//行Cell cell03=row2.createCell(0);//列cell03.setCellValue("张三");//(1,1)Cell cell04=row2.createCell(1);String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");cell04.setCellValue(time);//生成一张表(IO流) 03版本使用xlsx结尾:FileOutputStream fileOutputStream=new FileOutputStream(path+"统计文件07版本.xlsx");//写workbook.write(fileOutputStream);//关闭fileOutputStream.close();System.out.println("07版本写入完毕");}
}

效果:
在这里插入图片描述
poi写入大数据到Excel及调优:
https://blog.csdn.net/moerduo0/article/details/113835059


http://www.ppmy.cn/news/747655.html

相关文章

使用Git对Word或Excel文件进行版本管理

我们在使用Word编辑文档&#xff0c;或者使用Excel表格计算时&#xff0c;时常想&#xff0c;要是有个版本管理工具能把我们的文档或表格管理起来就好了&#xff0c;好让我们可以“昨日重现”&#xff0c;而不至人为失误而后悔不迭。 git就是一个使用便捷&#xff0c;功能强大…

阿里的Easyexcel读取Excel文件(最新版本)

本篇文章主要介绍一下使用阿里开源的Easyexcel工具处理读取excel文件&#xff0c;因为之前自己想在网上找一下这个简单的立即上手的博客&#xff0c;发现很多文章的教程都针对比较旧的版本的Easyexcel&#xff0c;没有使用新版本的方法&#xff0c;导致很多方法都标志过期了或者…

Delphi 操作Excel方法大全

Delphi操作Excel大全 原文地址&#xff1a;http://blog.csdn.net/lailai186/article/details/6664110 Delphi 控制Excel (一) 使用动态创建的方法 首先创建 Excel 对象&#xff0c;使用ComObj: var ExcelApp: Variant; ExcelApp : CreateOleObject( ‘Excel.Application’ ); …

Excel(Office)哪一版最好用?

目录 一、Excel哪版最好用 二、Excel基本使用手册 1.如何知道自己的Office版本 2.ctrlS是保存&#xff0c;另存为的快捷键是什么&#xff1f; 3.如何一打开就是自己常用的字体的和字号&#xff0c;不用每次都设置 4.右上角只能有【保存】【撤销】【重做】三个按钮吗&#xff1f…

计算机excel还原,Excel文件恢复方法

" 假如你不小心将Excel文件删除了&#xff0c;那这个时候你会采取哪些办法来找回被误删的Excel文件呢&#xff1f;下面我们就来说说这个问题。 一、电脑端操作 1、回收站 第一种找回Excel文件的方法是使用电脑上的回收站功能&#xff0c;只要我们打开回收站&#xff0c;运…

Linux-vim与gdb与make/makefile

三个模式&#xff1a;命令模式 文本模式 底行模式 yum :instell 安装 remove 卸载 gcc -o执行后生成文件命名 gcc 1.c -o fst.out -E预编译 -S汇编 -c生成机器码 Linux 中 静态库&#xff1a;.a&#xff1b;动态库&#xff1a;.so Linux默认动态库&#xff0c;…

excel power Query

一、power query Power query是微软从Excel 到 PowerBI 的中间产物&#xff0c;它既是PowerBi取数部分的内核&#xff0c;又能够在Excel中使用。 在excel中使用Power query处理数据的能力要高出用excel本身处理数据的性能数倍。power query的操作方法不仅有图形化界面&#xff…

Mac版本EXCEL导入数据 (Excel真香)

跟爬虫相爱相杀了那么久&#xff0c;突然发现Excel居然还有直接导入网站数据的功能&#xff0c;针对一些小的网页爬取请求&#xff0c;Excel的输出格式简明&#xff0c;方法简单&#xff0c;是真的香啊&#xff01;&#xff01; 但是mac版本中有些Excel的功能并不完全&#xff…