【java】详解java读取excel内容,
分享于 点击 9583 次 点评:161
【java】详解java读取excel内容,
本例主要讲解通过java程序打印出excel文件中内容。直接贴源码,然后再整理原理:
package dec;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
*
* <p>
* Title: JAVA读取Excel测试代码
* </p>
*
* <p>
* Description: 示例 业务类
* </p>
*
* <p>
* Copyright: Copyright (c) 2012
* </p>
*
*
* @author dml@2012-12-10
* @version 1.0
*/
public class ExcelReader {
private HSSFWorkbook wb = null;// book [includes sheet]
private HSSFSheet sheet = null;
private HSSFRow row = null;
private int sheetNum = 0; // 第sheetnum个工作表
private int rowNum = 0;
private FileInputStream fis = null;
private File file = null;
public ExcelReader() {
}
public ExcelReader(File file) {
this.file = file;
}
public void setRowNum(int rowNum) {
this.rowNum = rowNum;
}
public void setSheetNum(int sheetNum) {
this.sheetNum = sheetNum;
}
public void setFile(File file) {
this.file = file;
}
/**
* 读取excel文件获得HSSFWorkbook对象
*/
public void open() throws IOException {
fis = new FileInputStream(file);
wb = new HSSFWorkbook(new POIFSFileSystem(fis));
fis.close();
}
/**
* 返回sheet表数目
*
* @return int
*/
public int getSheetCount() {
int sheetCount = -1;
sheetCount = wb.getNumberOfSheets();
return sheetCount;
}
/**
* sheetNum下的记录行数
*
* @return int
*/
public int getRowCount() {
if (wb == null)
System.out.println("=============WorkBook为空==============");
HSSFSheet sheet = wb.getSheetAt(this.sheetNum);
int rowCount = -1;
rowCount = sheet.getLastRowNum();
return rowCount;
}
/**
* 读取指定sheetNum的rowCount
*
* @param sheetNum
* @return int
*/
public int getRowCount(int sheetNum) {
HSSFSheet sheet = wb.getSheetAt(sheetNum);
int rowCount = -1;
rowCount = sheet.getLastRowNum();
return rowCount;
}
/**
* 得到指定行的内容
*
* @param lineNum
* @return String[]
*/
public String[] readExcelLine(int lineNum) {
return readExcelLine(this.sheetNum, lineNum);
}
/**
* 指定工作表和行数的内容
*
* @param sheetNum
* @param lineNum
* @return String[]
*/
public String[] readExcelLine(int sheetNum, int lineNum) {
if (sheetNum < 0 || lineNum < 0)
return null;
String[] strExcelLine = null;
try {
sheet = wb.getSheetAt(sheetNum);
row = sheet.getRow(lineNum);
int cellCount = row.getLastCellNum();
strExcelLine = new String[cellCount + 1];
for (int i = 0; i <= cellCount; i++) {
strExcelLine[i] = readStringExcelCell(lineNum, i);
}
} catch (Exception e) {
e.printStackTrace();
}
return strExcelLine;
}
/**
* 读取指定列的内容
*
* @param cellNum
* @return String
*/
public String readStringExcelCell(int cellNum) {
return readStringExcelCell(this.rowNum, cellNum);
}
/**
* 指定行和列编号的内容
*
* @param rowNum
* @param cellNum
* @return String
*/
public String readStringExcelCell(int rowNum, int cellNum) {
return readStringExcelCell(this.sheetNum, rowNum, cellNum);
}
/**
* 指定工作表、行、列下的内容
*
* @param sheetNum
* @param rowNum
* @param cellNum
* @return String
*/
public String readStringExcelCell(int sheetNum, int rowNum, int cellNum) {
if (sheetNum < 0 || rowNum < 0)
return "";
String strExcelCell = "";
try {
sheet = wb.getSheetAt(sheetNum);
row = sheet.getRow(rowNum);
if (row.getCell((short) cellNum) != null) { // add this condition
// judge
switch (row.getCell((short) cellNum).getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
strExcelCell = "FORMULA ";
break;
case HSSFCell.CELL_TYPE_NUMERIC: {
strExcelCell = String.valueOf(row.getCell((short) cellNum)
.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_STRING:
strExcelCell = row.getCell((short) cellNum)
.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
strExcelCell = "";
break;
default:
strExcelCell = "";
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return strExcelCell;
}
// 主函数用于测试,一般用这里面的函数来进行操作
public static void main(String args[]) {
// 标示文件路径
File file = new File("D:\\testExcel.xls");
ExcelReader readExcel = new ExcelReader(file);
// 打开文件
try {
readExcel.open();
} catch (IOException e) {
e.printStackTrace();
}
readExcel.setSheetNum(0); // 设置读取索引为0的工作表
// 总行数
int count = readExcel.getRowCount();
// 循环读取Excel文件中的内容
for (int i = 0; i <= count; i++) {
String[] rows = readExcel.readExcelLine(i);
for (int j = 0; j < rows.length; j++) {
System.out.print(rows[j] + " ");
System.out.print("|");
}
System.out.print("\n");
}
}
}
执行结果:
D:\\testExcel.xls中内容是什么就打印什么。
所需jar包:
commons-io-1.1.jar
excelutils.jar
poi-3.0.1.jar
tm-extractors-0.4.jar
这些已经打包上传到了poi.rar,自行下载引入到工程中即可。
实现这个功能使用了Apache POI,下面是百度百科中介绍:
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
结构:
HSSF - 提供读写Microsoft Excel格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。
HWPF - 提供读写Microsoft Word格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读写Microsoft Visio格式档案的功能。
Apache基金会开源出很多项目,作为java程序员,应该或多或少熟悉一些
抽空了解一下这些项目的内容,可以迅速提升自身软件设计能力。
p.s.Java程序员切勿实现了功能就认为任务完毕,完成任务之后,稍微整理一下研发过程中的弯路,会收获很多。
dml@2012.12.10
相关文章
- 暂无相关文章
用户点评