1. 程式人生 > >Java工具類(解析excl表格)

Java工具類(解析excl表格)

使用場景:匯入excl表格

package com.devframe.common.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

/**

  • Excel 匯入資料相關工具類的使用
  • @author chenhang

*/
public class ExcelReader1 {

/**
 * 獲取單元格的值
 * 
 * @param cell
 * @return
 */
public static String getCellValue(Cell cell) {
	if (cell == null)
		return "";
	if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
		return cell.getStringCellValue();
	} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
		return String.valueOf(cell.getBooleanCellValue());
	} else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
		return cell.getCellFormula();
	} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
		return String.valueOf(cell.getNumericCellValue());
	}
	return "";
}

public static String getCellValue1(Cell cell) {
	String result = new String();
	switch (cell.getCellType()) {
	case HSSFCell.CELL_TYPE_NUMERIC:// 數字型別
		if (HSSFDateUtil.isCellDateFormatted(cell)) {// 處理日期格式、時間格式
			SimpleDateFormat sdf = null;
			if (cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")) {
				sdf = new SimpleDateFormat("HH:mm");
			} else {// 日期
				sdf = new SimpleDateFormat("yyyy-MM-dd");
			}
			Date date = cell.getDateCellValue();
			result = sdf.format(date);
		} else if (cell.getCellStyle().getDataFormat() == 58) {
			// 處理自定義日期格式:m月d日(通過判斷單元格的格式id解決,id的值是58)
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			double value = cell.getNumericCellValue();
			Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
			result = sdf.format(date);
		} else {
			HSSFDataFormatter dataFormatter = new HSSFDataFormatter();
			result = dataFormatter.formatCellValue(cell);
			/*
			 * double value = cell.getNumericCellValue(); CellStyle style =
			 * cell.getCellStyle(); DecimalFormat format = new
			 * DecimalFormat(); String temp = style.getDataFormatString();
			 * // 單元格設定成常規 if (temp.equals("General")) {
			 * format.applyPattern("#"); } result = format.format(value);
			 */
		}
		break;
	case HSSFCell.CELL_TYPE_STRING:// String型別
		result = cell.getRichStringCellValue().toString();
		break;
	case HSSFCell.CELL_TYPE_BLANK:
		result = "";
	default:
		result = "";
		break;
	}
	return result;
}

/**
 * 合併單元格處理,獲取合併行
 * 
 * @param sheet
 * @return List<CellRangeAddress>
 */
public List<CellRangeAddress> getCombineCell(Sheet sheet) {
	List<CellRangeAddress> list = new ArrayList<CellRangeAddress>();
	// 獲得一個 sheet 中合併單元格的數量
	int sheetmergerCount = sheet.getNumMergedRegions();
	// 遍歷所有的合併單元格
	for (int i = 0; i < sheetmergerCount; i++) {
		// 獲得合併單元格儲存進list中
		CellRangeAddress ca = sheet.getMergedRegion(i);
		list.add(ca);
	}
	return list;
}

private int getRowNum(List<CellRangeAddress> listCombineCell, Cell cell, Sheet sheet) {
	int xr = 0;
	int firstC = 0;
	int lastC = 0;
	int firstR = 0;
	int lastR = 0;
	for (CellRangeAddress ca : listCombineCell) {
		// 獲得合併單元格的起始行, 結束行, 起始列, 結束列
		firstC = ca.getFirstColumn();
		lastC = ca.getLastColumn();
		firstR = ca.getFirstRow();
		lastR = ca.getLastRow();
		if (cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR) {
			if (cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC) {
				xr = lastR;
			}
		}
	}
	return xr;
}

/**
 * 判斷單元格是否為合併單元格,是的話則將單元格的值返回
 * 
 * @param listCombineCell
 *            存放合併單元格的list
 * @param cell
 *            需要判斷的單元格
 * @param sheet
 *            sheet
 * @return
 */
public String isCombineCell(List<CellRangeAddress> listCombineCell, Cell cell, Sheet sheet) throws Exception {
	int firstC = 0;
	int lastC = 0;
	int firstR = 0;
	int lastR = 0;
	String cellValue = null;
	for (CellRangeAddress ca : listCombineCell) {
		// 獲得合併單元格的起始行, 結束行, 起始列, 結束列
		firstC = ca.getFirstColumn();
		lastC = ca.getLastColumn();
		firstR = ca.getFirstRow();
		lastR = ca.getLastRow();
		if (cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR) {
			if (cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC) {
				Row fRow = sheet.getRow(firstR);
				Cell fCell = fRow.getCell(firstC);
				cellValue = getCellValue(fCell);
				break;
			}
		} else {
			cellValue = "";
		}
	}
	return cellValue;
}

/**
 * 獲取合併單元格的值
 * 
 * @param sheet
 * @param row
 * @param column
 * @return
 */
public static String getMergedRegionValue(Sheet sheet, int row, int column) {
	int sheetMergeCount = sheet.getNumMergedRegions();

	for (int i = 0; i < sheetMergeCount; i++) {
		CellRangeAddress ca = sheet.getMergedRegion(i);
		int firstColumn = ca.getFirstColumn();
		int lastColumn = ca.getLastColumn();
		int firstRow = ca.getFirstRow();
		int lastRow = ca.getLastRow();

		if (row >= firstRow && row <= lastRow) {
			if (column >= firstColumn && column <= lastColumn) {
				Row fRow = sheet.getRow(firstRow);
				Cell fCell = fRow.getCell(firstColumn);
				return getCellValue(fCell);
			}
		}
	}
	return null;
}

/**
 * 判斷指定的單元格是否是合併單元格
 * 
 * @param sheet
 * @param row
 *            行下標
 * @param column
 *            列下標
 * @return
 */
public static boolean isMergedRegion(Sheet sheet, int row, int column) {
	int sheetMergeCount = sheet.getNumMergedRegions();
	for (int i = 0; i < sheetMergeCount; i++) {
		CellRangeAddress range = sheet.getMergedRegion(i);
		int firstColumn = range.getFirstColumn();
		int lastColumn = range.getLastColumn();
		int firstRow = range.getFirstRow();
		int lastRow = range.getLastRow();
		if (row >= firstRow && row <= lastRow) {
			if (column >= firstColumn && column <= lastColumn) {
				return true;
			}
		}
	}
	return false;
}

}