java 读写 excle 完整版
pom.xml
<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>
<!-- 处理excel和上面功能是一样的-->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.10</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.baozun.util.DecryptAndEncrypt</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass></mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.5.RELEASE</version>
<configuration>
<mainClass>com.bj.util.ExcelReaderWrite</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
部分参考:http://www.codepub.cn/2017/06/13/Maven-introduces-local-dependency-jar-to-executable-jar-packages/
https://www.cnblogs.com/523823-wu/p/7635358.html
代码部分
package com.baozun.util; import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class ExcelReaderWrite {
private static final String EXCEL_XLS = "xls";
private static final String EXCEL_XLSX = "xlsx";
private static String filePath=null;
private static String sheetName=null;
private static Workbook workBook;
private static Sheet sheet;
private Object[][] results;
private List<List<String>> listData;
private static FileInputStream in ; public ExcelReaderWrite(String filePath, String sheetName) {
this.filePath = filePath;
this.sheetName = sheetName;
innit();
} public void innit() {
workBook = getWorkbok(filePath);
sheet = workBook.getSheet(sheetName);
} public Workbook getWorkbok(String filePath) {
try {
File file = new File(filePath);
if (file.exists()) {
in = new FileInputStream(file);
if (file.getName().endsWith(EXCEL_XLS)) { //Excel 2003 return new HSSFWorkbook(in);
} else if (file.getName().endsWith(EXCEL_XLSX)) { // Excel 2007/2010
return new XSSFWorkbook(in);
}
} else {
System.out.println(filePath + "不存在 !");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
} public void setCellData(int rowNum, int colNum, String content) {
rowNum -= 1;
colNum -= 1;
FileOutputStream out = null;
if (null == sheet.getRow(rowNum)) {
Row row = sheet.createRow(rowNum);
if (null == row.getCell(colNum)) {
row.createCell(colNum).setCellValue(content);
} else {
row.getCell(colNum).setCellValue(content);
}
} else {
sheet.getRow(rowNum).createCell(colNum).setCellValue(content);
} try {
out = new FileOutputStream(filePath);
workBook.write(out);
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try { out.flush();
out.close();
in.close();
innit(); } catch (Exception e) {
e.printStackTrace();
}
} }
private String getCellValue(Cell cell) {
String cellValue = "";
DataFormatter formatter = new DataFormatter();
if (cell != null) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
cellValue = formatter.formatCellValue(cell);
} else {
double value = cell.getNumericCellValue();
int intValue = (int) value;
cellValue = value - intValue == 0 ? String.valueOf(intValue) : String.valueOf(value);
}
break;
case Cell.CELL_TYPE_STRING:
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
cellValue = String.valueOf(cell.getCellFormula());
break;
case Cell.CELL_TYPE_BLANK:
cellValue = "";
break;
case Cell.CELL_TYPE_ERROR:
cellValue = "";
break;
default:
cellValue = cell.toString().trim();
break;
}
}
return cellValue.trim();
}
public String getCellData(String sheetName, int rowNum, int colNum) {
if (rowNum <= 0 || colNum <= 0) {
return null;
} else if (workBook.getSheet(sheetName) == null || workBook.getSheet(sheetName).getRow(rowNum - 1) == null) { return filePath + " 中 " + sheetName + " 不存在,或者" + rowNum + " 行不存在 ! ";
} else {
return getCellValue(workBook.getSheet(sheetName).getRow(rowNum - 1).getCell(colNum - 1));
} }
public String getCellData(int rowNum, int colNum) {
if (rowNum <= 0 || colNum <= 0) {
return null;
} else if (workBook.getSheet(sheetName) == null || workBook.getSheet(sheetName).getRow(rowNum - 1) == null) { return filePath + " 中 " + sheetName + " 不存在,或者" + rowNum + " 行不存在 ! ";
} else {
return getCellValue(workBook.getSheet(sheetName).getRow(rowNum - 1).getCell(colNum - 1));
} }
private List<List<String>> getSheetData() {
listData = new ArrayList<List<String>>();
int numOfRows = sheet.getLastRowNum()+1;
System.out.println("sheet.getLastRowNum()="+sheet.getLastRowNum());
for (int i = 0; i < numOfRows; i++) {
Row row = sheet.getRow(i);
Map<String, String> map = new HashMap<String, String>();
List<String> list = new ArrayList<String>();
if (row != null) {
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
list.add(this.getCellValue(cell));
}
}
listData.add(list);
} return listData ;
} public void printSheetData() {
// 测试数据excel数据用 ; List<List<String>> list = getSheetData();
System.out.println("总共有 "+list.size()+" 行!");
for (int i = 0; i < list.size(); i++) {
System.out.println("第 "+(i+1)+" 行有 "+list.get(i).size()+" 单元格有值 : "+list.get(i).toString());
}
} public static void main(String[] args) {
// String filePath_1 = "D:/writeExcel.xlsx";
String filePath_2 = "D:/writeExcel97.xls";
String sheetName = "Sheet1";
/* int lastNum_1 = new ExcelReaderWrite(filePath_1, sheetName).getWorkbok().getSheet(sheetName).getLastrowNum();
int lastNum_2 = new ExcelReaderWrite(filePath, sheetName).getWorkbok().getSheet(sheetName).getLastrowNum();
System.out.println(lastNum_1);
System.out.println(lastNum_2);
for (int i = 0; i <10 ; i++) {
new ExcelReaderWrite(filePath_1, sheetName).setCellData(i,3,filePath_1+"_"+String.valueOf(System.currentTimeMillis()));
new ExcelReaderWrite(filePath_2, sheetName).setCellData(i,3,filePath_2+"_"+String.valueOf(System.currentTimeMillis()));
}
String dataValue= new ExcelReaderWrite(filePath_1, sheetName).getCellData(sheetName, 1, 1);
System.out.println(dataValue);
new ExcelReaderWrite(filePath_1, sheetName).printSheetData();
new ExcelReaderWrite(filePath_2, sheetName).printSheetData();
*/
ExcelReaderWrite eh= new ExcelReaderWrite(filePath_2, sheetName);
eh.setCellData(1,1,"1");
eh.setCellData(1,2,"1_2");
eh.printSheetData();
eh.setCellData(2,1,"22_1");
eh.setCellData(2,2,"22_2"); eh.printSheetData();
System.out.println("2row2col="+eh.getCellData(2, 2));
// eh.setCellData(1,3,String.valueOf(System.currentTimeMillis()));
// eh.setCellData(1,4,String.valueOf(System.currentTimeMillis()));
// eh.setCellData(1, 5, String.valueOf(System.currentTimeMillis()));
// eh.setCellData(2,5,String.valueOf(System.currentTimeMillis()));
// eh.setCellData(2,5,String.valueOf(System.currentTimeMillis()));
// eh.setCellData(2,5,String.valueOf(System.currentTimeMillis()));
// eh.setCellData(2,5,String.valueOf(System.currentTimeMillis())); // eh.setCellData(3,2,String.valueOf(System.currentTimeMillis()));
// eh.setCellData(3,3,String.valueOf(System.currentTimeMillis()));
//s eh.setCellData(3,4,String.valueOf(System.currentTimeMillis())); } }
解决了 一个对象重复写入时的错误 ;
java 读写 excle 完整版的更多相关文章
- java 读写excle
2014-04-16 20:38:20 java读写excel 晚上打算研究如何c来编写
- java环境变量完整版
jdk默认安装 Key: JAVA_HOME(新建) Value: C:\Program Files\Java\jdk1.8.0_25 Key: Path(编辑) Value: %JAVA_HOME% ...
- 详解介绍Selenium常用API的使用--Java语言(完整版)
参考:http://www.testclass.net/selenium_java/ 一共分为二十个部分:环境安装之Java.环境安装之IntelliJ IDEA.环境安装之selenium.sele ...
- java读取文件完整版
public static void readFileByBytes(String fileName) { File file = new File(fileName); InputStream in ...
- JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载
JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载JDK(Java Development Kit,Java开发包,Java开发工具)是一个写Java的applet和 ...
- MySQL5.6 Replication主从复制(读写分离) 配置完整版
MySQL5.6 Replication主从复制(读写分离) 配置完整版 MySQL5.6主从复制(读写分离)教程 1.MySQL5.6开始主从复制有两种方式: 基于日志(binlog): 基于GTI ...
- Java编程思想(第4版) 中文清晰PDF完整版
Java编程思想(第4版) 中文清晰PDF完整版 [日期:2014-08-11] 来源:Linux社区 作者:Linux [字体:大 中 小] <Java编程思想>这本书赢得了全 ...
- JAVA在线观看视频教程完整版
今天给大家介绍一下JAVA在线观看视频教程完整版,我们知道Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语 ...
- 剑指offer】Java版代码(完整版)
转自:剑指offer]Java版代码(完整版) 转自:[剑指offer] JAVA版题解(完整版)
随机推荐
- Linux命令——磁盘管理
Linux命令--磁盘管理 命令df 作用:查看已挂载磁盘的总容量.使用容量.剩余容量等 参数:-i,查看inodes的使用状况 参数:-h,使用合适的单位显示(推荐) 命令du 作用:查看某个目录或 ...
- 分享一个excel根据文件超链接获取链接文档的最后更新时间
#获取制定单元格内超链接对应的链接地址Sub geturi() For Each cell In Range("E3:E43") If cell.Hyperlinks.Count ...
- 打印lua中全局变量的一段代码
function printTableItem(k, v, level) , level do io.write(" ") end io.write(tostring(k), &q ...
- php判断一个数组是否为另一个数组子集的方法
原文地址http://www.jbxue.com/article/14703.html // 快速的判断$a数组是否是$b数组的子集 $a = array(135,138); $b = array ...
- mysql的查询使用explain的讲解
摘自:http://www.jb51.net/article/33736.htm 在 explain的帮助下,您就知道什么时候该给表添加索引,以使用索引来查找记录从而让select 运行更快.如果由于 ...
- Docker学习笔记 — 开启Docker远程访问
默认情况下,Docker守护进程会生成一个socket(/var/run/docker.sock)文件来进行本地进程通信,而不会监听任何端口,因此只能在本地使用docker客户端或者使用Docker ...
- 「PKUSC2018」最大前缀和 LOJ#6433&BZOJ5369
分析: 这个题非常的棒,目测如果去了能AC... 我们考虑一个序列是如何构成的——一个后缀>0的序列,和一个前缀<0的序列 问题可以简化为求出当前缀和为状态S的所有数的和的时候,S满足后缀 ...
- Flutter - 添加从左向右滑动,返回上一个页面
很多App比如微信.IT之家等都支持从屏幕左侧向右滑动,来返回上一个页面. 很多iOS上的App也都支持. 那么这个神奇的手势滑动是怎么实现的呢? 其实非常简单,只需要添加一句话即可. platfor ...
- c# 无边框窗体的边框阴影
Windows API: using System; using System.Collections.Generic; using System.ComponentModel; using Syst ...
- 20155339 Exp7 网络欺诈防范
20155339 Exp7 网络欺诈防范 .基础问题回答 (1)通常在什么场景下容易受到DNS spoof攻击 当连接局域网的时候应该最容易被攻击,比如说连接了一些不清楚是什么的WiFi其实是很容易收 ...