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