Java解析xml

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 {
	private Logger logger = Logger.getLogger(ExcelSheetParser.class);

	private HSSFWorkbook workbook;

	public ExcelSheetParser(File excelFile) throws FileNotFoundException, IOException {

		workbook = new HSSFWorkbook(new FileInputStream(excelFile));
	}

	/**
	 * 获得表中的数据
	 * 
	 * @param sheetNumber
	 *            表格索引(EXCEL 是多表文档,所以需要输入表索引号)
	 * @return 由LIST构成的行和表
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public List<List> getDatasInSheet(int sheetNumber) throws FileNotFoundException, IOException {
		List<List> result = new ArrayList<List>();

		// 获得指定的表
		HSSFSheet sheet = workbook.getSheetAt(sheetNumber);

		// 获得数据总行数
		int rowCount = sheet.getLastRowNum();
		logger.info("found excel rows count: " + rowCount);
		if (rowCount < 0) {
			return result;
		}

		// 逐行读取数据
		for (int rowIndex = 0; rowIndex <= rowCount; rowIndex++) {

			// 获得行对象
			HSSFRow row = sheet.getRow(rowIndex);

			if (row != null) {

				List<Object> rowData = new ArrayList<Object>();

				// 获得本行中单元格的个数
				int columnCount = row.getLastCellNum();

				// 获得本行中各单元格中的数据
				for (short columnIndex = 0; columnIndex < columnCount; columnIndex++) {
					HSSFCell cell = row.getCell(columnIndex);

					cell.setCellType(HSSFCell.CELL_TYPE_STRING);
					// 获得指定单元格中数据
					Object cellStr = this.getCellString(cell);

					rowData.add(cellStr);

				}

				result.add(rowData);
			}
		}
		return result;
	}

	/**
	 * 获得单元格中的内容
	 * 
	 * @param cell
	 * @return
	 */
	protected Object getCellString(HSSFCell cell) {
		Object result = null;
		if (cell != null) {

			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_ERROR:
				result = null;
				break;
			case HSSFCell.CELL_TYPE_BOOLEAN:
				result = cell.getBooleanCellValue();
				break;
			case HSSFCell.CELL_TYPE_BLANK:
				result = null;
				break;
			}
		}
		return result;
	}

	public static void main(String[] args) throws Exception {
		File file = new File("D:/tmp/sss.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 n = 0; n < row.size(); n++) {
				Object value = row.get(n);
				String data = String.valueOf(value);
				System.out.print(data + "\t");
			}
			System.out.println();
		}
	}

}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值