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版题解(完整版)
随机推荐
- Node.js实战(七)之交互式解释器
Node.js REPL(Read Eval Print Loop:交互式解释器) 表示一个电脑的环境,类似 Window 系统的终端或 Unix/Linux shell,我们可以在终端中输入命令,并 ...
- WorldWind源码剖析系列:星球类World
星球类World代表通用的星球类,因为可能需要绘制除地球之外的其它星球,如月球.火星等.该类的类图如下. 需要说明的是,在WorldWind中星球球体的渲染和经纬网格的渲染时分别绘制的.经纬网格的渲染 ...
- DB-Engines Ranking
DB-Engines Ranking trend chart The DB-Engines Ranking ranks database management systems according to ...
- XML 的4种解析方式
在上一篇博客中,我们介绍了什么是 XML ,http://www.cnblogs.com/ysocean/p/6901008.html,那么这一篇博客我们介绍如何来解析 XML . 部分文档引用:ht ...
- MyBatis在Oracle中插入数据并返回主键的问题解决
引言: 在MyBatis中,希望在Oracle中插入数据之时,同一时候返回主键值,而非插入的条数... 环境:MyBatis 3.2 , Oracle. Spring 3.2 SQL Snipp ...
- CS229笔记:生成学习算法
在线性回归.逻辑回归.softmax回归中,学习的结果是\(p(y|x;\theta)\),也就是给定\(x\)的条件下,\(y\)的条件概率分布,给定一个新的输入\(x\),我们求出不同输出的概率, ...
- LeetCode Generate Parentheses (DFS)
题意 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 《HTTP权威指南》读书笔记:缓存
缓存的定义 Web缓存是可以自动保存常见文档副本的HTTP设备(包括浏览器?) 缓存的作用 减少冗余数据传输 缓解带宽瓶颈(很多网络为本地网络客户端提供的带宽比为远程服务器提供的带宽要宽) 缓解瞬时拥 ...
- 在 Azure 上部署 Asp.NET Core Web App
在云计算大行其道的时代,当你要部署一个网站时第一选择肯定是各式各样的云端服务.那么究竟使用什么样的云端服务才能够以最快捷的方式部署一个 ASP.NET Core的网站呢?Azure 的 Web App ...
- 基于约束的SQL攻击
前言 值得庆幸的是如今开发者在构建网站时,已经开始注重安全问题了.绝大部分开发者都意识到SQL注入漏洞的存在,在本文我想与读者共同去探讨另一种与SQL数据库相关的漏洞,其危害与SQL注入不相上下,但却 ...