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下jdk&tomcat的安装
unbantu: 1.下载相应版本的jdk及tomcat:sudo wget ${url} 2.解压: tar zxvf jdk-7u79-linux-x64.tar.gz tar zxvf apa ...
- 由微软打造的深度学习开放联盟ONNX成立
导读 如今的微软已经一跃成为全球市值最高的高科技公司之一.2018年11月底,微软公司市值曾两次超越了苹果,成为全球市值最高的公司,之后也一直处于与苹果胶着的状态.市场惊叹微软是一家有能力改造自己并取 ...
- 关于 clock tree
1. create_clock 时,不要定义在 hierarchical pin 上,否则 cts 时会忽略这个 clock ,详见 CTS-811 Warning,解法是将其定义到实际存在的 p ...
- Java使用线程并发库模拟弹夹装弹以及发射子弹的过程
同样是从网上看到的一个需求,需求描述都在代码中. 不多说了,直接贴代码了.相信大家都能够看得懂的! package cn.yw.bore; import java.util.ArrayList; im ...
- PRML1-引言
本系列是根据<pattern recognition and machine learning>一书写的,算是读书笔记?算是吧.因为是从自己角度出发,所以其实很大程度上自己看得懂,估计别人 ...
- iOS开发之使用UIView-Positioning简化页面布局
使用过代码布局的人可能会有这样的感觉,给控件设置frame的时候比较繁琐.最 近在Github上看到有一个UIView的一个分类UIView-Positioning,这个分类提供了一些属性,比如lef ...
- 笔记:UITextView内容垂直居中方法
- (void)contentSizeToFit { //先判断一下有没有文字(没文字就没必要设置居中了) ) { //textView的contentSize属性 CGSize contentSiz ...
- 使用Fortify进行代码静态分析(系列文章)
BUG级别:低 Code Correctness(代码正确性) 1.Class does not Implement Equals(类未能实现Equals方法) Dead Code(死亡代码) 1.U ...
- noip 提高组 2010
T1:机器翻译 题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于每个英 ...
- 解决Git在添加ignore文件之前就提交了项目无法再过滤问题
由于未添加ignore文件造成提交的项目很大(包含生成的二进制文件).所以我们可以将编译生成的文件进行过滤,避免添加到版本库中了. 首先为避免冲突需要先同步下远程仓库 $ git pull 在本地项目 ...