JAVA-Excel文件操作
使用环境: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);
}
} }
JAVA-Excel文件操作的更多相关文章
- java常见文件操作
收集整理的java常见文件操作,方便平时使用: //1.创建文件夹 //import java.io.*; File myFolderPath = new File(str1); try { if ( ...
- 对Aspose.Cells Excel文件操作的扩展
工作中对Excel操作的需求很是常见,今天其他项目组的同事在进行Excel数据导入时,使用Aspose.Cells Excel 遇到了些问题. 刚好闲来不忙,回想自己用过的Excel文件操作,有NPO ...
- Java api 入门教程 之 JAVA的文件操作
I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程的一个基本 ...
- 【个人使用.Net类库】(3)Excel文件操作类(基于NPOI)
Web开发工作中经常要根据业务的需要生成对应的报表.经常采用的方法如下: 将DataTable导出至Excel文件; 读取模板Excel文件; 修改模板Excel文件对应的内容. 因此,便想到封装一个 ...
- java中文件操作的工具类
代码: package com.lky.pojo; import java.io.BufferedReader; import java.io.BufferedWriter; import java. ...
- java csv 文件 操作类
一个CSV文件操作类,功能比较齐全: package tool; import java.io.BufferedReader; import java.io.BufferedWriter; impor ...
- java中文件操作《一》
在日常的开发中我们经常会碰到对文件的操作,在java中对文件的操作都在java.io包下,这个包下的类有File.inputStream.outputStream.FileInputStream.Fi ...
- java IO文件操作简单基础入门例子,IO流其实没那么难
IO是JAVASE中非常重要的一块,是面向对象的完美体现,深入学习IO,你将可以领略到很多面向对象的思想.今天整理了一份适合初学者学习的简单例子,让大家可以更深刻的理解IO流的具体操作. 1.文件拷贝 ...
- Java常用文件操作-2
上篇文章记录了常用的文件操作,这里记录下通过SSH服务器操作Linux服务器的指定路径下的文件. 这里用到了第三方jar包 jsch-0.1.53.jar, jsch-api 1.删除服务器上指定路径 ...
- Java常用文件操作-1
在我们的实际工作中经常会用到的文件操作,再此,将工作中碰到的做一个记录,以便日后查看. 1.复制文件夹到新文件夹下 /** * 复制文件夹下所有文件到指定路径 *@param oldPath *@pa ...
随机推荐
- HDU 2058 The sum problem
传送门 Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequen ...
- 在linux下安装tesseract-ocr
1. 在ubuntu下可以自动安装 [html] 技术分享技术分享 sudo apt-get install tesseract-ocr 2.编译安装 a.编译环境: gcc gc ...
- python标准模块(一)
本文会涉及到的模块: time datetime sys os random re hashlib 模块,用若干代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能 ...
- 【项目】'NSRangeException', reason: '*** -[__NSArrayM removeObjectAtIndex:]: index 2 beyond bounds [0 .. 1]'
问题代码: [self.assetsArray objectAtIndex:indexPath.row] 问题解决思路:这里
- Linux学习之CentOS--CentOS6.下Mysql数据库的安装与配置
跟着配置,顺利配置完成 http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html
- MySQL------如何将SQLServer文件数据迁移到MySQL
转载: http://blog.csdn.net/zhangdaiscott/article/details/46412453
- 自然语言14_Stemming words with NLTK
https://www.pythonprogramming.net/stemming-nltk-tutorial/?completed=/stop-words-nltk-tutorial/ # -*- ...
- JAVA 的普通加法运算
利用Scanner类,其中数字输入的函数,利用while循环结构来得到输入的实数,当输入的是字母时,结束循环 源代码: package ccc;import java.util.*;public cl ...
- JAVA 5.17习题
1.编写并测试一个代表地址的Address类,地址信息由国家.省份.城市.街道.邮编组成,并可以返回完整的地址信息. //======================================= ...
- codeforces 712A. Memory and Crow
题目链接:http://codeforces.com/problemset/problem/712/A 题目大意: 给你一个数字系列,求其满足条件的一个序列. 条件为: ai = bi - bi + ...