当前位置: 代码迷 >> Web前端 >> 应用poi操作excel
  详细解决方案

应用poi操作excel

热度:291   发布时间:2012-09-29 10:30:01.0
使用poi操作excel
   import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.poi.hssf.record.formula.functions.Cell;
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.hssf.util.CellReference;
import org.apache.poi.hssf.util.Region;

import com.mocha.ps.psaf.util.properties.InitPropertiesUtil;

public class ExcelUtil {

	/**
	 * 通过excel标题获取对应表中的列名
	 * 
	 * @param cells
	 * @return
	 */
	public static List getColumnName(HSSFRow row) {
		if (row != null) {
			List keys = new ArrayList();
			Iterator cellIterator = row.cellIterator();
			while (cellIterator.hasNext()) {
				HSSFCell cell = (HSSFCell) cellIterator.next();
				String cellName = ExcelUtil.getCellContent(cell);
				String keysName = InitPropertiesUtil.getProperty(cellName);
				keys.add(keysName);
			}
			return keys;
		} else {
			return null;
		}

	}

	public static String getCellContent(HSSFCell cell) {
		String values = "";
		switch (cell.getCellType()) {
		case HSSFCell.CELL_TYPE_BOOLEAN:
			values = String.valueOf(cell.getBooleanCellValue());
			break;
		case HSSFCell.CELL_TYPE_BLANK:
			values = "";
			break;
		case HSSFCell.CELL_TYPE_ERROR:
			values = "";
			break;
		case HSSFCell.CELL_TYPE_NUMERIC:
			values = String.valueOf(cell.getNumericCellValue());
			break;
		case HSSFCell.CELL_TYPE_STRING:
			values = cell.getStringCellValue();
			break;
		}
		return values;
	}

	/**
	 * 读取单元,组织参数
	 * 
	 * @param cells
	 * @param keys
	 * @return
	 */
	public static Map readCells(HSSFRow row, List keys) {

		Map values = new HashMap();
		Iterator cellIterator = row.cellIterator();
		int i = 0;
		while (cellIterator.hasNext()) {
			HSSFCell cell = (HSSFCell) cellIterator.next();
			String cellContent = ExcelUtil.getCellContent(cell);
			values.put(keys.get(i), cellContent);
			i++;
		}

		return values;
	}

	/**
	 * 创建工作簿
	 * 
	 * @param file
	 * @return
	 */

	public static HSSFWorkbook createWorkbook(InputStream file) {
		HSSFWorkbook wbs = null;
		try {
			wbs = new HSSFWorkbook(file);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return wbs;
	}

	private static Map cellMap = new HashMap();
	public static void main(String[] args) {
			try {
				HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
						"D:\\123.xls"));
				ExcelUtil.getMergedCellValue(workbook);
				
				int sheetCounts = workbook.getNumberOfSheets();
				for (int i = 0; i < sheetCounts; i++) {
					HSSFSheet sheet = workbook.getSheetAt(i);
					int rows = sheet.getPhysicalNumberOfRows();
					for (int j = 0; j < rows; j++) {
						HSSFRow row = sheet.getRow(j);
						Iterator iterator = row.cellIterator();
						String rowValue = "第"+j+"行:";
						while(iterator.hasNext()){
							HSSFCell cell = (HSSFCell) iterator.next();
							int cellIndex = cell.getColumnIndex();
							int rowIndex = cell.getRowIndex();
							String key = cellIndex+"_"+rowIndex;
							String cellValue = ExcelUtil.getValue(key);
							if(cellValue == null || cellValue.equals("")){
								cellValue = ExcelUtil.getCellContent(cell);
							}
							rowValue +=cellValue+"\t";
							
							
						}
						System.out.println(rowValue);
					}
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
	}
	
	public static void getMergedCellValue(HSSFWorkbook workbook){
		int sheetCounts = workbook.getNumberOfSheets();
		for (int i = 0; i < sheetCounts; i++) {
			HSSFSheet sheet = workbook.getSheetAt(i);
			if(sheet != null){
				int mergedCounts = sheet.getNumMergedRegions();
				for (int j = 0; j < mergedCounts; j++) {
					Region region = sheet.getMergedRegionAt(j);
					if (region != null) {
						short cFrom = region.getColumnFrom();//合并单元格的cell的x开始坐标
						short cTo = region.getColumnTo();//合并单元格的cell的x结束坐标
						int rowFrom = region.getRowFrom();//合并单元格的cell的y开始坐标
						int rowTo = region.getRowTo();//合并单元格的cell的y结束坐标
				        HSSFCell cell = sheet.getRow(rowFrom).getCell(cFrom);
				        String value = ExcelUtil.getCellContent(cell);
				        ExcelUtil.getXY(rowFrom, rowTo, cFrom, cTo, value);
					}

				}
			}
		}
	}
	/**
	 * 保存所有合并的单元的数据
	 * @param xFrom
	 * @param xTo
	 * @param yFrom
	 * @param yTo
	 * @param value
	 * @return
	 */
	public  static  Map getXY(int xFrom, int xTo, int yFrom, int yTo, String value){
		for(int i = xFrom; i <= xTo; i++){
			for(int j = yFrom; j <= yTo; j++){
				cellMap.put(j + "_" + i, value); //cellIndex_rowIndex
			}
		}
		return cellMap;
	}
	
	/**
	 * 判断该单元格是否属于合并单元格,如果是返回该单元格的值,如果不是返回空
	 * @param key
	 * @return
	 */
	public static String getValue(String key){
		return (String) cellMap.get(key);
	}
	
	

}


  相关解决方案