java解析Excel文件的方法
来源:互联网 发布:js遍历dom元素 编辑:程序博客网 时间:2024/05/01 11:14
package com.hjlz.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.log4j.Logger;
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;
public class ExcelSheetParser {
//log4j
private Logger logger = Logger.getLogger(ExcelSheetParser.class);
private HSSFWorkbook workbook;//工作簿
public ExcelSheetParser(File file){
try {
//获取工作薄workbook
workbook = new HSSFWorkbook(new FileInputStream(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List getDatasInSheet(int sheetNumber){
List<List> result = new ArrayList<List>();
//获得指定的sheet
HSSFSheet sheet = workbook.getSheetAt(sheetNumber);
//获得sheet总行数
int rowCount = sheet.getLastRowNum();
logger.info("found excel rows count:" + rowCount);
if(rowCount < 1){
return result;
}
//遍历行row
for (int rowIndex = 0; rowIndex <= rowCount; rowIndex++) {
//获得行对象
HSSFRow row = sheet.getRow(rowIndex);
if(null != row){
List<Object> rowData = new ArrayList<Object>();
//获得本行中单元格的个数
int cellCount = row.getLastCellNum();
//遍历列cell
for (short cellIndex = 0; cellIndex < cellCount; cellIndex++) {
HSSFCell cell = row.getCell(cellIndex);
//获得指定单元格中的数据
Object cellStr = this.getCellString(cell);
rowData.add(cellStr);
}
result.add(rowData);
}
}
return result;
}
private Object getCellString(HSSFCell cell) {
// TODO Auto-generated method stub
Object result = null;
if(cell != null){
//单元格类型:Numeric:0,String:1,Formula:2,Blank:3,Boolean:4,Error:5
int cellType = cell.getCellType();
switch (cellType) {
case HSSFCell.CELL_TYPE_STRING:
result = cell.getRichStringCellValue().getString();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA:
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
result = cell.getBooleanCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
result = null;
break;
case HSSFCell.CELL_TYPE_ERROR:
result = null;
break;
default:
System.out.println("枚举了所有类型");
break;
}
}
return result;
}
//test
public static void main(String[] args) {
File file = new File("E://my.xls");
ExcelSheetParser parser = new ExcelSheetParser(file);
List<List> datas = parser.getDatasInSheet(0);
for (int i = 0; i < datas.size(); i++) {
List row = datas.get(i);
for (short j = 0; j < row.size(); j++) {
Object value = row.get(j);
String data = String.valueOf(value);
System.out.println(data + "/t");
}
System.out.println();
}
}
}
- java解析Excel文件的方法
- java解析excel文件
- Java 解析 Excel 文件
- java 解析excel文件
- java解析excel文件
- JAVA解析EXCEL文件
- java解析excel文件
- java EXCEL 文件解析
- java解析excel方法
- java对excel文件的解析操作
- java 解析Excel 与常用的xml解析方法
- 利用JAVA操作EXCEL文件的方法
- Java读取Excel文件的方法
- java+poi解析excel文件
- java 解析excel文件内容
- Java解析本地文件Excel文件
- android解析word,excel等文件的方法
- Excel大文件解析: Java POI SAX解析Excel 文件
- Flex使用ActionScript
- JSP(HTML)中((br/))的应用
- Form对象的方法
- 常用算法----递推法
- libpcap包头部信息
- java解析Excel文件的方法
- C# struct代替c++ 的union
- ORACLE常用命令
- windows 2003下安装 sql server 2000的方法
- struts2 文件上传的拦截器问题
- 鲫鱼豆腐汤
- ABAP数据库操作(学习SAP程序设计的整理-数据库)
- 只要笑一笑,没有什么过不了
- 谁动了你的隐私