使用poi读写excel、向excel追加数据等,包括.xls和.xlsx文档
1、使用maven引入jar包
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
实际引入的jar包为:

如果不使用maven方法引入jar包只需导入以下jar包

2、 向excel追加数据示例:
public static void dealExcel(String in, String out) throws Exception {
if (!in.equals(out)) {
copyFile(in, out);
}
FileInputStream input = new FileInputStream(new File(out));
Workbook wb = WorkbookFactory.create(input);
System.out.println(wb.getClass());
FileOutputStream output = new FileOutputStream(new File(out));
Sheet sheet = wb.getSheetAt(0);
int columnCount = -1;
int rowCount = 0;
for (Row row : sheet) {
if (columnCount == -1) {
columnCount = row.getLastCellNum();
}
if (row.getLastCellNum() == columnCount) {
//增加列
Cell last = row.createCell(columnCount);
if (rowCount == 0) {
last.setCellValue("Test");
} else {
last.setCellValue(rowCount);
}
rowCount++;
}
}
////添加行
//Row row = sheet.createRow(sheet.getLastRowNum());
//Cell cell = row.createCell(0);
//cell.setCellValue("Test");
output.flush();
wb.write(output);
wb.close();
output.close();
}
public static void copyFile(String in, String out) throws Exception {
InputStream inputStream = new FileInputStream(new File(in));
File copy = new File(out);
if (copy.exists()) {
copy.delete();
}
copy.createNewFile();
OutputStream outputStream = new FileOutputStream(copy);
byte[] buffer = new byte[1024 * 4];
while ((inputStream.read(buffer)) != -1) {
outputStream.write(buffer);
}
inputStream.close();
outputStream.close();
}
3、获取excel数据
public static String getCellData(Cell cell) {
Object value = null;
CellType cellType = cell.getCellTypeEnum();
if (cellType == CellType.STRING) {
value = cell.getStringCellValue();
} else if (cellType == CellType.BOOLEAN) {
value = cell.getBooleanCellValue();
} else if (cellType == CellType.NUMERIC) {
value = cell.getNumericCellValue();
} else if (cellType == CellType.FORMULA) {
value = cell.getCellFormula();
} else if (cellType == CellType.BLANK || cellType == CellType.ERROR) {
value = "error";
} else {
value = "";
}
return String.valueOf(value);
}
使用poi读写excel、向excel追加数据等,包括.xls和.xlsx文档的更多相关文章
- 使用poi或jxl,通过java读写xls、xlsx文档
package nicetime.com.baseutil; import jxl.Sheet;import jxl.Workbook;import jxl.read.biff.BiffExcepti ...
- POI加dom4j将数据库的数据按一定格式生成word文档
一:需求:将从数据库查处来的数据,生成word文档,并有固定的格式.(dom4j的jar包+poi的jar包) 二:解决:(1)先建立固定格式的word文档(2007版本以上),另存成为xml文件,作 ...
- NPOI(2.1.3)向excel中插入图片,xls文档图片插入成功,xlsx文档图片插入失败
众所周知,NPOI对xls和xlsx两个版本的excel文档的操作并没有一个统一的支持, 程序若想兼容这两个版本的操作,必须根据excel版本分别去调用HSSF和XSSF这两套操作库, 之前一直不明白 ...
- JAVA导入(读取)Excel中的数据(支持xls与xlsx文件)
一.导入jar包 poi-3.7.jarpoi-scratchpad-3.7.jarpoi-examples-3.7.jarpoi-ooxml-3.7.jarpoi-ooxml-schemas-3.7 ...
- C# DataGridView 导出 Excel(根据Excel版本显示选择不同后缀格式xls或xlsx)
/// <summary> /// DataGridView导出至Excel,解决问题:打开Excel文件格式与扩展名指定格式不一致 /// </summary> /// &l ...
- $用python处理Excel文档(2)——用xlsxwriter模块写xls/xlsx文档
Refer:<python自动化运维:技术与最佳实践> 更多用法参考xlsxwriter官方文档:http://xlsxwriter.readthedocs.io/ 本文主要总结一下如何使 ...
- $ 用python处理Excel文档(1)——用xlrd模块读取xls/xlsx文档
本文主要介绍xlrd模块读取Excel文档的基本用法,并以一个GDP数据的文档为例来进行操作. 1. 准备工作: 1. 安装xlrd:pip install xlrd 2. 准备数据集:从网上找到的1 ...
- Java读取Excel文件(包括xls和xlsx)的样例程序
样例程序如下所示,其中: parseXls()函数依赖于jxl,只能读取xls格式文件: parseExcel()函数依赖于apache poi,能够读取xls和xlsx两种格式的文件. jxl的依赖 ...
- Swift - 文件目录路径获取及数据储存(Home目录,文档目录,缓存目录)
iOS应用程序只能在自己的目录下进行文件的操作,不可以访问其他的存储空间,此区域被称为沙盒. 应用沙盒结构分析 1.应用程序包:包含了所有的资源文件和可执行文件 2.Documents:保存应用运 ...
随机推荐
- LeetCode59 Spiral Matrix II
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- Open Source Projects Released By Google
Open Source Projects Released By Google Google has released over 20 million lines of code and over 9 ...
- 测试代码的执行时间魔法方法%time和%timeit
对于规模更大.运行时间更长的数据分析应用程序,你可能会希望测试一下各个部分或函数调用或语句的执行时间.你可能会希望了解某个复杂计算过程中到底是哪些函数占用的时间最多.幸运的是,在开发和测试代码的过程中 ...
- 【原生JS】图片预加载之无序预加载
图片预加载之无序预加载 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset= ...
- laravel5.*安装使用Redis以及解决Class 'Predis\Client' not found和Fatal error: Non-static method Redis::set() cannot be called statically错误
https://phpartisan.cn/news/35.html laravel中我们可以很简单的使用Redis,如何在服务器安装Redis以及原创访问你们可以访问Ubuntu 设置Redis密码 ...
- vue 生成 二维码 qrCode 插件 使用 方法
首先安装方法:(--save 参数会改变package.json 推荐使用 下次直接install就行了) npm install --save qrcode 然后项目使用: import QRCod ...
- Python--day62--ORM的使用
4.Django里ORM的使用 1,手动创建数据库 2,在settings.py里面,配置数据库的连接信息 3,在项目/__init__.py告诉Django用pymysql模块代替MySQLdb(不 ...
- Codeforces Round #340 (Div. 2) B. Chocolate
题意:一段01串 分割成段 每段只能有一个1 问一段串有多少种分割方式 思路:两个1之间有一个0就有两种分割方式,然后根据分步乘法原理来做. (不过这里有一组0 1 0这种数据的话就不好直接处理,所以 ...
- Spring Data JPA查询指定列,并返回实体(改)
现有PostEntiy实力,包含各种属性,如: /** * @Auther: DingShuo * @Date: 2018/7/18 11:09 * @Description: */ @Entity ...
- 陈志生:德国信贷工厂风控模式对P2P的启发
上海合盘金融信息服务股份有限公司董事长陈志生 和讯银行消息 "2014中国金融论坛"于5月14-15日在北京召开,本次论坛主题为“全面深化金融体制改革与实体经济增长”.和讯网作为指 ...