使用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:保存应用运 ...
随机推荐
- POJ-3159_Candies
Candies Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse ...
- 云原生生态周报 Vol. 2
摘要: Cloud Native Weekly China Vol. 2 业界要闻 Kubernetes External Secrets 近日,世界上最大的域名托管公司 Godaddy公司,正式宣布 ...
- QQ 聊天机器人API
QQ机器人是腾讯陆续推出的的人工智能聊天机器人的总称. 都说小Q妹妹聪明好学,我们能够教她说话.也能够请他帮忙查询邮编.手机号,或者解释成语.翻译成语,据说她还会查询手机号码归属地.应用科学计算器. ...
- 一个项目看java TCP/IP Socket编程
前一段时间刚做了个java程序和网络上多台机器的c程序通讯的项目,遵循的是TCP/IP协议,用到了java的Socket编程.网络通讯是java的强项,用TCP/IP协议可以方便的和网络上的其他程序互 ...
- php 正则表达式怎么匹配标签里面的style?
$str = '<div style="margin:0px;text-align:left;padding:0px;">任意内容</div>'; $reg ...
- ORA-01089: 即時シャットダウン処理中 - 操作はできません
一:当时的情景 SQL> shutdown immediate --无任何返回结果 二:问题定位过程 1.查询相关进程只有ORACLE的关键进程存在 ps -ef |grep ora_ soad ...
- Tenka1 Programmer Beginner Contest D IntegerotS(补)
当时没做出来,官方题解没看懂,就看别人提交的代码,刚对着别人代码调了几组数据,才发现,思路差不多,不过,原来是这样实现啊,果然我还是很菜 思路:题目要求是选取的这些数字全部进行OR运算,结果<= ...
- hdu 3374 String Problem (字符串最小最大表示 + KMP求循环节)
Problem - 3374 KMP求循环节. http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html 循环节推导的证明相当 ...
- NIO 中文乱码自我解决的简单DEMO
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStrea ...
- 使C# WebApi返回Json
找到Global.asax文件,在Application_Start()方法中添加一句: protected void Application_Start() { AreaRegistration.R ...