Java——poi读取Excel文件
1.创建文件流,打开EXCEL文件
FileInputStream excelFile = new FileInputStream(excelPath);
XSSFWorkbook workbook = new XSSFWorkbook(excelFile);
2.切换到对应文件名
XSSFSheet excelSheet = workbook.getSheet(sheetName);
3.获取实际行数和列数
int rows = excelSheet.getPhysicalNumberOfRows(); //行数
int cols = excelSheet.getRow(0).getPhysicalNumberOfCells();//列数
4.读取数据
public static String ReadData(XSSFSheet excelSheet, int row, int col){
try{
String CellData= "";
XSSFCell cell = excelSheet.getRow(row).getCell(col);
if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
cell.setCellType(Cell.CELL_TYPE_STRING);
CellData = cell.getStringCellValue();
}
else if(cell.getCellType() == HSSFCell.CELL_TYPE_STRING){
CellData = cell.getStringCellValue();
}
else if(cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA){
CellData = cell.getCellFormula();
}
return CellData;
}catch(Exception e){
return "";
}
}

示例:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class Excel { public static void main(String[] args) throws IOException {
String excelPath = "F:\\login.xlsx";
String sheetName = "001";
try{
FileInputStream excelFile = new FileInputStream(excelPath);
XSSFWorkbook workbook = new XSSFWorkbook(excelFile);
XSSFSheet excelSheet = workbook.getSheet(sheetName);
int rows = excelSheet.getPhysicalNumberOfRows(); //行数
int cols = excelSheet.getRow(0).getPhysicalNumberOfCells();//列数 for(int row = 0;row< rows; ++row){
for (int col =0; col < cols; ++col){
System.out.print(ReadData(excelSheet, row, col) + ' ');
if(col ==1)
System.out.println();
}
}
workbook.close();
}catch (FileNotFoundException e ){
System.out.println("找不到该文件");
}
} public static String ReadData(XSSFSheet excelSheet, int row, int col){
try{
String CellData= "";
XSSFCell cell = excelSheet.getRow(row).getCell(col);
cell.setCellType(Cell.CELL_TYPE_STRING);
CellData = cell.getStringCellValue();
return CellData;
}catch(Exception e){
return "";
}
}
}
输出结果:

5.写入excel
同样我们一开始先来想下手工写入数据流程
1:打开EXCEL
2:指定Sheet
3: 指定行号
4:指定列号
5:写入数据
6:保存数据
了解了操作流程,我们就将思想转为成代码其实1,2,3,4
我们在读Excel时就已写过,但这里要注意下,我们在读Excel时是不是已有数据,那也就是说每个单元格中已有内容,我们用getRow(行号)不是空值对吗,如果我们要往一个没有值的单元格写值时一开始那个行号是空值,所以我们第三步应改为创建一个行号,第四步改为创一个列号创建行号
我们可以用CreateRow(行号)在创建行号时,我们要想一个问題,我们一开始创建了第一行,并写入一个值到单元格中,比如我想写入一个结果到第一行第二个单元格时也是先创建一行吗,如果再用创建方式生成第一行,那前一次写入的数据会不会不存在呢,实际上是会被删掉的,这个自已可以去试试
所以我们代码实现应是这样一开始用getrow(1)如果反回的值是空值,那就用CreateRow的方式,如果不是空值我就用GetRow(1)
public static void setData(int row, int col, String sheetName, String Data, String WriteExcelPath) throws IOException{
try{
FileInputStream excelFile = new FileInputStream(WriteExcelPath);
XSSFWorkbook workbook = new XSSFWorkbook(excelFile);
XSSFSheet excelSheet = workbook.getSheet(sheetName);
XSSFRow Row = excelSheet.getRow(row);
if(Row == null)
Row = excelSheet.createRow(row);
XSSFCell Cell = Row.getCell(col);
if(Cell == null )
Cell = Row.createCell(col);
Cell.setCellValue(Data);
FileOutputStream fileOut = new FileOutputStream(WriteExcelPath);
workbook.write(fileOut);
fileOut.close();
workbook.close();
}catch(Exception e){
System.out.println("数据写入错误");
}
}
Java——poi读取Excel文件的更多相关文章
- JAVA使用POI读取EXCEL文件的简单model
一.JAVA使用POI读取EXCEL文件的简单model 1.所需要的jar commons-codec-1.10.jarcommons-logging-1.2.jarjunit-4.12.jarlo ...
- 项目一:第四天 1、快递员的条件分页查询-noSession,条件查询 2、快递员删除(逻辑删除) 3、基于Apache POI实现批量导入区域数据 a)Jquery OCUpload上传文件插件使用 b)Apache POI读取excel文件数据
1. 快递员的条件分页查询-noSession,条件查询 2. 快递员删除(逻辑删除) 3. 基于Apache POI实现批量导入区域数据 a) Jquery OCUpload上传文件插件使用 b) ...
- 使用POI 读取 Excel 文件,读取手机号码 变成 1.3471022771E10
使用POI 读取 Excel 文件,读取手机号码 变成 1.3471022771E10 [问题点数:40分,结帖人xieyongqiu] 不显示删除回复 ...
- Java实现POI读取Excel文件,兼容后缀名xls和xlsx
1.引入所需的jar包: maven管理项目的话直接添加以下坐标即可: <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -- ...
- 使用jxl,poi读取excel文件
作用:在java后台添加一个方法,读取导入的excel内容,根据需要返回相应的sql语句,以完成对临时表的插入操作. 使用jxl读取excel文件 package com.sixthf.bi.sapp ...
- 使用POI读取excel文件内容
1.前言 项目中要求读取excel文件内容,并将其转化为xml格式.常见读取excel文档一般使用POI和JExcelAPI这两个工具.这里我们介绍使用POI实现读取excel文档. 2.代码实例: ...
- jspsmart(保存文件)+poi(读取excel文件)操作excel文件
写在前面: 项目环境:jdk1.4+weblogic 需求:能上传excel2003+2007 由于项目不仅需要上传excel2003,还要上传excel2007,故我们抛弃了jxl(只能上传exce ...
- 使用poi读取excel文件 Cannot get a text value from a numeric cell
我这样转换得到一个excel文本域的值 Cell cell = row.getCell(c); cell.setCellType(Cell.CELL_TYPE_STRING); String park ...
- spring boot 使用 POI 读取Excel文件
内容简介 本文主要介绍使用POI进行Excel文件的相关操作,涉及读取文件,获取sheet表格,对单元格内容进行读写操作,以及合并单元格的操作. Excel文件目录 Excel模板文件存了resour ...
随机推荐
- thinkphp3.2验证码在服务器上显示不出来
ThinPHP3.2 验证码 在本地服务器访问可以显示,上传到服务器就不能访问了 /**** * 验证码 */ function code() { $config=array( 'fontSize'= ...
- Linux命令详解-whatis
描述一个命令执行什么功能. 1.命令格式: whatis [ -M PathName ] Command ... 2.命令功能: 描述一个命令执行什么功能. 3.命令参数: -M PathNa ...
- VM虚拟机安装的XP如何全屏
首先安装install VMwear Tools..,如图:
- 位于/var/log目录下的20个Linux日志文件
位于/var/log目录下的20个Linux日志文件[译] from:http://buptguo.com/2014/01/16/linux-var-log-files/ 原文地址:20 Linux ...
- jdk1.8的lambda语法(转)
原文链接:http://www.jb51.net/article/115081.htm 代码: package com.jdk_8; import org.junit.Test; import jav ...
- 转 关于nvcc fatal : Value 'sm_20' is not defined for option 'gpu-architecture'的问题
原文地址: https://blog.csdn.net/Mao_Jonah/article/details/78965827 关于nvcc fatal : Value ‘sm_20’ is not d ...
- It is the courage
It is the reality that a society which becomes lower and becomes weak.Believe it or not,I think it i ...
- maven安装之后,或者升级之后遇到的问题:could not find or load main class org.codehaus.plexus.class.....
从maven2升级到maven3或者从maven3降级到maven2,M2_HOME环境变量改变后,在终端执行mvn -v,出现如下错误: Exception in thread "main ...
- 重新学习之spring第三个程序,整合struts2+spring
第一步:导入Struts2jar包+springIOC的jar包和Aop的Jar包 第二步:建立applicationContext.xml文件+struts.xml文件+web.xml文件 web. ...
- StreamSets Data Collector Edge 说明
Data Collector Edge 是不包含界面的agent 安装 下载包 https://streamsets.com/opensource tar xf streamsets-datacoll ...