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版题解(完整版)
随机推荐
- Spark项目之电商用户行为分析大数据平台之(十一)JSON及FASTJSON
一.概述 JSON的全称是”JavaScript Object Notation”,意思是JavaScript对象表示法,它是一种基于文本,独立于语言的轻量级数据交换格式.XML也是一种数据交换格式, ...
- day2-课堂代码
# 字符串常用属性和操作 # a = 'aa' # b = 'bb' # c = 'cc' # d = a + b + c # print(d) # age = 50 # print('田老师的年龄是 ...
- uniform_tree以及其变体
//判断一棵树是不是uniform-tree bool uniform_tree(TreeNode* root){ if(root == NULL) return true; return unifo ...
- <数据结构与算法分析>读书笔记--利用Java5泛型实现泛型构件
一.简单的泛型类和接口 当指定一个泛型类时,类的声明则包括一个或多个类型参数,这些参数被放入在类名后面的一对尖括号内. 示例一: package cn.generic.example; public ...
- JAVA框架 Spring 调用jdbcsuport简化开发
一)使用DAO的jdbcsuport来简化开发 首先来清楚一个概念: 我们在进行配置文件来进行依赖注入的时候,主要是通过set方法来进行设置的. 正常我们使用spring的jdbctemplate的时 ...
- Android 给CheckBox设置背景
一般来说我们给控件(Button,LinearLayout,ImageView,TextView等)设这背景的时候只需要设置这些控件的android:background即可, 但是在给CheckBo ...
- 2017-2018-2 20155333 《网络对抗技术》 Exp1 PC平台逆向破解
2017-2018-2 20155333 <网络对抗技术> Exp1 PC平台逆向破解 1. 逆向及Bof基础实践说明 1.1 实践目标 本次实践的对象是一个名为pwn1的linux可执行 ...
- 汇编 ADD指令
知识点: 加法汇编指令ADD 一.加法指令 ADD(Addition) 格式 格式: ADD A,B //A=A+B; 功能: 两数相加 . OPRD1为任一通用寄存器或存储器操作数,可以是任意一个 ...
- 编译安装php时遇到virtual memory exhausted: Cannot allocate memory
有时候用vps建站时需要通过编译的方式来安装主机控制面板.对于大内存的VPS来说一般问题不大,但是对于小内存,比如512MB内存的godaddy VPS来说,很有可能会出现问题,因为编译过程是一个内存 ...
- 【干货】YUM安装PHP 7版本后,增加phalcon框架的报错解决
目录 1.yum安装php 7.x版本,此处部署7.3版本 2.安装phalcon框架 2.1.PHP版本依赖关系 2.2.编译phalcon扩展模块 2.3.增加扩展文件 3.部署phalcon遇到 ...