使用环境:JAVA 1.8

一、安装

1.下载Poi包

Apache POI

当前最新稳定版本为3.14。下载poi-bin-3.14.zip即可。

2.将下载下来的压缩包解压,将其中的所有jar文件,都复制到JRE路径中。

我的路径是D:\Program Files\Java\jdk1.8.0_40\jre\lib\ext

3.新建NetBeans程序,便可使用。

二、使用

具体使用可以参考压缩包中的doc文件夹里面的帮助文档。

这里简单列举一下应用:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JOptionPane;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelLog {
public ExcelLog(int index) {
createNewBook();
}
//新建工作表
private void createNewBook() {
try {
String path = "D:\\test.xlsx";
File f = new File(path);
File dir = f.getParentFile();
if (!dir.exists()) {//如果文件路径不存在,则创建路径
dir.mkdirs();
}
if (!f.exists()) {//如果文件不存在,则新建文件
Workbook wb = new XSSFWorkbook(); //.xlsx
Sheet sheet = wb.createSheet("Test Log"); //新建Sheet
Row row = sheet.createRow(0); //写一行数据
String[] header = {"COL1", "COL2", "COL3", "COL4", "COL5"};
for (int i = 0; i < header.length; i++) {
row.createCell(i).setCellValue(header[i]);
} //写入文件
try (FileOutputStream fileOut = new FileOutputStream(path)) {
wb.write(fileOut);
wb.close();
}
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "无法写入Excel文件:" + ex.getMessage());
System.exit(-1);
}
} //Sheet表的行数
private int getRowCount() {
try (FileInputStream fileInput = new FileInputStream(path)) {
Workbook wb = WorkbookFactory.create(fileInput);
Sheet sheet = wb.getSheetAt(0);
return sheet.getPhysicalNumberOfRows();
} catch (IOException | InvalidFormatException | EncryptedDocumentException ex) {
JOptionPane.showMessageDialog(null, "无法读取Excel文件:" + ex.getMessage());
System.exit(-1);
return 0;
}
} //写一行数据
private void writeLine(String[] items) {
try{
Workbook wb = WorkbookFactory.create(new FileInputStream(path));
Sheet sheet = wb.getSheetAt(0);
int rowIndex = this.getRowCount();
Row row = sheet.createRow(rowIndex);
for (int i = 0; i < items.length; i++) {
row.createCell(i).setCellValue(items[i]);
}
//写入文件
try(FileOutputStream fileOut = new FileOutputStream(path);){
wb.write(fileOut);
}
wb.close();
} catch (IOException | InvalidFormatException | EncryptedDocumentException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "无法写入Excel文件:" + ex.getMessage());
System.exit(-1);
}
} }

最新文章

  1. Atitit 软件架构方法的进化与演进cs bs soa roa &#160;msa&#160; attilax总结
  2. Java公众号推荐 - BeJavaGod
  3. ionic路由传值
  4. Chart图表
  5. Life Cycle of Thread – Understanding Thread States in Java
  6. MyBatis入门学习(一)
  7. 使用PL/SQL编写存储过程访问数据库
  8. mac安装django1.5.4
  9. webpack笔记_(3)_First_Project
  10. 使用Intent实现Activity的隐式跳转
  11. php定界符 &lt;&lt;&lt; 的作用及使用注意事项
  12. JAVA 读取图片储存至本地
  13. SpringMVC基础-@RequestMapping
  14. Hibernate中事务小案例
  15. MEF 基础简介 四
  16. ssm配置文件叙述
  17. Django 学习笔记(三) --- HTML 模版加载 css、js、img 静态文件
  18. servlet拦截器
  19. Python操作Excle
  20. Java Spring Bean相关配置

热门文章

  1. AC 自动机
  2. As环境下添加android support library依赖库
  3. Alpha版本十天冲刺——Day 2
  4. java 图像灰度化与二值化
  5. Linux上性能异常定位以及性能监控
  6. ES6之let(理解闭包)和const命令
  7. jsp action中附件下载的写法
  8. 对Java垃圾回收最大的误解是什么
  9. JSP 原理
  10. HDFS的Java操作