java中excel的读取操作
2018-03-01 09:15
381 查看
在很多时候,需要批量上传excel文件里的内容,这个时候就需要相应的操作。但是excel有两种,一种xls,另外一种xlsx。
(1):首先引入jar包。主要引入maven的内容如下:
<!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl -->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.10</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
主要是运用poi来执行excel的操作。
(2):读取excel的内容,代码如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Test {
public static Object[][] getTestData(String filePath) throws FileNotFoundException, IOException {
File excelFile = new File(filePath);
/*
* 判断给定文件的类型; 1.如果是xls的问价那类型就创建XSSFWorkBook ;
* 2.如果是xlsx的文件类型就创建HSSFWorkBook ;
*/
String xls = filePath.substring(filePath.indexOf('.'));
System.out.println("传入文件的后缀是:" + xls + " ;");
if (xls.equals(".xls")) {
HSSFWorkbook xlswb = new HSSFWorkbook(new FileInputStream(excelFile));
HSSFSheet sheet = xlswb.getSheetAt(0);
int rowcount = sheet.getLastRowNum() - sheet.getFirstRowNum();
List<Object[]> list = new ArrayList<Object[]>();
// System.out.println("---------该sheet总共有 :" + rowcount + " ;");
Row row;
Cell cell;
for (int i = 0; i < rowcount + 1; i++) {
row = sheet.getRow(i);
if(row.getCell(i)==null){
continue ;
}else{
}
Object[] obj = new Object[row.getLastCellNum()];
/*
* System.ou
4000
t.println("当前行是:" + (row.getRowNum() + 1) +
* " ;当前行的第一个单元格是:" + row.getFirstCellNum() + " ; 当前前的最后一个单元格是:"
* + row.getLastCellNum() + "; ");
*/
// System.out.println("obj 数组的长度是 :" + obj.length + " ;");
for (int j = 0; j < row.getLastCellNum(); j++) {
cell = row.getCell(j);
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
obj[j] = cell.getRichStringCellValue().getString();
/*
* System.out.print(cell.getRichStringCellValue().
* getString()); System.out.print("|");
*/ break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
obj[j] = cell.getDateCellValue();
// System.out.print(String.valueOf(cell.getDateCellValue()));
} else {
obj[j] = cell.getNumericCellValue();
// System.out.print(cell.getNumericCellValue());
}
// System.out.print("|");
break;
case Cell.CELL_TYPE_BOOLEAN:
obj[j] = cell.getBooleanCellValue();
/*
* System.out.print(cell.getBooleanCellValue());
* System.out.print("|");
*/ break;
default:
}
}
list.add(obj);
// System.out.println();
}
// System.out.println("list.size()===" + list.size());
Object[][] object = new Object[list.size()][];
for (int i = 0; i < object.length; i++) {
object[i] = list.get(i);
}
return object;
} else if (xls.equals(".xlsx")) {
XSSFWorkbook wbxlsx = new XSSFWorkbook(new FileInputStream(excelFile));
XSSFSheet sheet = wbxlsx.getSheetAt(0);
int rowcount = sheet.getLastRowNum() - sheet.getFirstRowNum();
List<Object[]> list = new ArrayList<Object[]>();
// System.out.println("---------该sheet总共有 :" + rowcount + " ;");
Row row;
Cell cell;
for (int i = 0; i < rowcount + 1; i++) {
row = sheet.getRow(i);
/*
* System.out.println("当前行是:" + (row.getRowNum() + 1) +
* " ;当前行的第一个单元格是:" + row.getFirstCellNum() + " ; 当前前的最后一个单元格是:"
* + row.getLastCellNum() + "; ");
*/
String orderCode=null;
String shipmentNo=null;
Object[] obj = new Object[row.getLastCellNum()];
// System.out.println("obj 数组的长度是 :" + obj.length + " ;");
for (int j = 0; j < row.getLastCellNum(); j++) {
cell = row.getCell(j);
if(j==0){
orderCode=cell.toString();
}else if(j==1){
shipmentNo=cell.toString();
}
}
if(orderCode!=null&&shipmentNo!=null){
System.out.println("orderCode=="+orderCode+",shipmentNo="+shipmentNo);
}
}
// System.out.println("list.size()===" + list.size());
Object[][] object = new Object[list.size()][];
for (int i = 0; i < object.length; i++) {
object[i] = list.get(i);
}
return object;
} else {
System.out.println("指定的文件不是excle文件!");
}
return null;
}
public static void main(String[] args) throws FileNotFoundException, IOException {
//读取excel文件的位置
String filePathxlsx = "C:\\Users\\user\\Desktop\\different.xlsx";
Object[][] objxlsx = Test.getTestData(filePathxlsx);
for(int i=0;i<10;i++){
try {
System.out.println("i=="+i);
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/*
* for (int i = 0; i < objxlsx.length; i++) {
* System.out.println(objxlsx[i]); }
*/
}
}
(3):总结
读取excel的方式有很多种方式,因人而异。
(1):首先引入jar包。主要引入maven的内容如下:
<!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl -->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.10</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
主要是运用poi来执行excel的操作。
(2):读取excel的内容,代码如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Test {
public static Object[][] getTestData(String filePath) throws FileNotFoundException, IOException {
File excelFile = new File(filePath);
/*
* 判断给定文件的类型; 1.如果是xls的问价那类型就创建XSSFWorkBook ;
* 2.如果是xlsx的文件类型就创建HSSFWorkBook ;
*/
String xls = filePath.substring(filePath.indexOf('.'));
System.out.println("传入文件的后缀是:" + xls + " ;");
if (xls.equals(".xls")) {
HSSFWorkbook xlswb = new HSSFWorkbook(new FileInputStream(excelFile));
HSSFSheet sheet = xlswb.getSheetAt(0);
int rowcount = sheet.getLastRowNum() - sheet.getFirstRowNum();
List<Object[]> list = new ArrayList<Object[]>();
// System.out.println("---------该sheet总共有 :" + rowcount + " ;");
Row row;
Cell cell;
for (int i = 0; i < rowcount + 1; i++) {
row = sheet.getRow(i);
if(row.getCell(i)==null){
continue ;
}else{
}
Object[] obj = new Object[row.getLastCellNum()];
/*
* System.ou
4000
t.println("当前行是:" + (row.getRowNum() + 1) +
* " ;当前行的第一个单元格是:" + row.getFirstCellNum() + " ; 当前前的最后一个单元格是:"
* + row.getLastCellNum() + "; ");
*/
// System.out.println("obj 数组的长度是 :" + obj.length + " ;");
for (int j = 0; j < row.getLastCellNum(); j++) {
cell = row.getCell(j);
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
obj[j] = cell.getRichStringCellValue().getString();
/*
* System.out.print(cell.getRichStringCellValue().
* getString()); System.out.print("|");
*/ break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
obj[j] = cell.getDateCellValue();
// System.out.print(String.valueOf(cell.getDateCellValue()));
} else {
obj[j] = cell.getNumericCellValue();
// System.out.print(cell.getNumericCellValue());
}
// System.out.print("|");
break;
case Cell.CELL_TYPE_BOOLEAN:
obj[j] = cell.getBooleanCellValue();
/*
* System.out.print(cell.getBooleanCellValue());
* System.out.print("|");
*/ break;
default:
}
}
list.add(obj);
// System.out.println();
}
// System.out.println("list.size()===" + list.size());
Object[][] object = new Object[list.size()][];
for (int i = 0; i < object.length; i++) {
object[i] = list.get(i);
}
return object;
} else if (xls.equals(".xlsx")) {
XSSFWorkbook wbxlsx = new XSSFWorkbook(new FileInputStream(excelFile));
XSSFSheet sheet = wbxlsx.getSheetAt(0);
int rowcount = sheet.getLastRowNum() - sheet.getFirstRowNum();
List<Object[]> list = new ArrayList<Object[]>();
// System.out.println("---------该sheet总共有 :" + rowcount + " ;");
Row row;
Cell cell;
for (int i = 0; i < rowcount + 1; i++) {
row = sheet.getRow(i);
/*
* System.out.println("当前行是:" + (row.getRowNum() + 1) +
* " ;当前行的第一个单元格是:" + row.getFirstCellNum() + " ; 当前前的最后一个单元格是:"
* + row.getLastCellNum() + "; ");
*/
String orderCode=null;
String shipmentNo=null;
Object[] obj = new Object[row.getLastCellNum()];
// System.out.println("obj 数组的长度是 :" + obj.length + " ;");
for (int j = 0; j < row.getLastCellNum(); j++) {
cell = row.getCell(j);
if(j==0){
orderCode=cell.toString();
}else if(j==1){
shipmentNo=cell.toString();
}
}
if(orderCode!=null&&shipmentNo!=null){
System.out.println("orderCode=="+orderCode+",shipmentNo="+shipmentNo);
}
}
// System.out.println("list.size()===" + list.size());
Object[][] object = new Object[list.size()][];
for (int i = 0; i < object.length; i++) {
object[i] = list.get(i);
}
return object;
} else {
System.out.println("指定的文件不是excle文件!");
}
return null;
}
public static void main(String[] args) throws FileNotFoundException, IOException {
//读取excel文件的位置
String filePathxlsx = "C:\\Users\\user\\Desktop\\different.xlsx";
Object[][] objxlsx = Test.getTestData(filePathxlsx);
for(int i=0;i<10;i++){
try {
System.out.println("i=="+i);
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/*
* for (int i = 0; i < objxlsx.length; i++) {
* System.out.println(objxlsx[i]); }
*/
}
}
(3):总结
读取excel的方式有很多种方式,因人而异。
相关文章推荐
- Java操作Excel使用jxl.jar--读取Excel
- java 操作 Excel (读取Excel2003 2007,Poi实现)
- 读取excel文件(jxl.jar包简介/java操作excel jxl.jar下载地址)
- Java 操作 Excel (读取Excel2003 2007,Poi实现)
- 用Java POI操作Excel,读取数据导入DB2数据库
- java 操作 poi 解析、读取 Excel 文件
- java操作excel 插入 读取 超链接
- Java操作Excel之理解JXL--读取Excel
- java读取excel并操作数据
- Java 操作 Excel (读取Excel2007,Poi实现)
- POI对Excel操作——java读取Excel数据
- Java对Excel的读取操作
- java操作excel之读取excel的内容
- java 对excel操作 读取、写入、修改数据;导出数据库数据到excel
- Java 操作 Excel (读取Excel2003 2007,Poi实现)
- Java操作Excel之理解JXL——读取Excel
- java操作office和pdf文件java读取word,excel和pdf文档内容
- Java 操作 Excel (读取Excel2007,Poi实现)
- Java 操作 Excel (读取Excel2003 2007,Poi实现)