java 读写Excel (支持office 2007)

/**
* EXCEL文档解析工具类 该工具能将EXCEL文档中的表解析为由JAVA基础类构成的数据集合 整个EXCEL表由多个行组成.每行用一个LIST表示.
* EXCEL中的行由一个LIST表示,各列的数据索引从0开始一一对齐存放在这个LIST中; 多个行构成整个表,由一个LIST存放多个行.
*
*/
public class ExcelSheetParserNoVersion {

// private Workbook workbook;
//
// private boolean isXLS; // 判断是否是{-xls}
//
// private final static String XLSX = ".xlsx";
//
// private static String filePath;
//
// private Sheet sheet;
//
// private Row row;
//
// private Cell cell;
//
// public ExcelSheetParserNoVersion() {
//
// }
//
// public Workbook getWorkBook(File excelFile) throws FileNotFoundException,
// IOException {
// InputStream is = new FileInputStream(excelFile);
// if (null == workbook) {
//
// if(isXLS){
// workbook = new XSSFWorkbook(is);
// }else{
// workbook = new HSSFWorkbook(is);
// }
// }
// is.close();
// return workbook;
// }
//
// public Workbook getWorkBook(InputStream is) throws FileNotFoundException,
// IOException {
// if (null == workbook) {
// if(isXLS){
// workbook = new XSSFWorkbook(is);
// }else{
// workbook = new HSSFWorkbook(is);
// }
// }
// is.close();
// return workbook;
// }
//
// public void isXLS(String filePath) {
// if (-1 < filePath.indexOf(XLSX)) {
// isXLS = true;
// return;
// }
// isXLS = false;
// }
//
// public List getDatasInSheet(int sheetNumber) throws FileNotFoundException,
// IOException {
// List result = new ArrayList();
// sheet = workbook.getSheetAt(sheetNumber);
// int rowCount = sheet.getLastRowNum();
// if (rowCount < 1) {
// return result;
// }
// for (int rowIndex = 0; rowIndex <= rowCount; rowIndex++) {
// row = sheet.getRow(rowIndex);
// if (row != null) {
// List rowData = new ArrayList();
// int columnCount = row.getLastCellNum();
// for (short columnIndex = 0; columnIndex < columnCount; columnIndex++) {
// cell = row.getCell(columnIndex);
// Object cellStr = this.getCellString(cell);
// rowData.add(cellStr);
// }
// result.add(rowData);
// }
// }
// return result;
// }
//
// /**
// * 获得单元格中的内容
// *
// * @param cell
// * @return
// */
// protected Object getCellString(Cell cell) {
// Object result = null;
// if (cell != null) {
//
// int cellType = cell.getCellType();
//
// switch (cellType) {
//
// case HSSFCell.CELL_TYPE_STRING:
// result = cell.getRichStringCellValue();
// break;
// case HSSFCell.CELL_TYPE_NUMERIC:
// // 日期
// if (HSSFDateUtil.isCellDateFormatted(cell)) {
// //result = cell.getDateCellValue().toLocaleString();
// result = cell.getDateCellValue().toString();
// }
// // 其他格式的数据
// else {
// result = new BigDecimal(cell.getNumericCellValue());
// }
// break;
// case HSSFCell.CELL_TYPE_FORMULA:
// result = new BigDecimal(cell.getNumericCellValue());
// break;
// case HSSFCell.CELL_TYPE_ERROR:
// result = null;
// break;
// case HSSFCell.CELL_TYPE_BOOLEAN:
// result = new Boolean(cell.getBooleanCellValue());
// break;
// case HSSFCell.CELL_TYPE_BLANK:
// result = null;
// break;
// }
// }
// return result;
// }
//
// public static void main(String[] args) throws Exception {
// filePath = "g:\\test.xlsx";
// File file = new File(filePath);
// ExcelSheetParserNoVersion parser = new ExcelSheetParserNoVersion();
// parser.isXLS(filePath);
// parser.getWorkBook(file);
// List datas = parser.getDatasInSheet(0);
//
// for (int i = 0; i < datas.size(); i++) {// 显示数据
// List row = (List) datas.get(i);
// for (short n = 0; n < row.size(); n++) {
// Object value = row.get(n);
// String data = String.valueOf(value);
// System.out.print(data + "\t");
// }
// System.out.println();
// }
//
// }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值