Poi方式解析excel

最近项目需要实现几个excel导入导出的功能、最开始用的jxl、不过jxl不能解析07以上的文档、感觉不是很方便、感觉apache官方的API对微软的文档支持还不错、便试着用了一下、比jxl稍微复杂了一点点、不过好在功能比较齐全、适用性比较强、

这里只用Poi解析和创建了excel文档、像word、ppt之类的后面研究一下、

依赖包:

poi-3.7-20101029.jar

poi-ooxml-3.7-20101029.jar 

poi-ooxml-schemas-3.7-20101029.jar 

poi-scratchpad-3.7-20101029.jar 

xmlbeans-2.3.0.jar 

geronimo-stax-api_1.0_spec-1.0.jar 

dom4j-1.6.1.jar

后面几个包不能缺、不然解析07的会有异常

  1. 解析excel
    /**
    	 * 解析excel文件 03/07
    	 * 
    	 * @param file
    	 * @return
    	 * @throws Exception
    	 */
    	public static List<Map<String, Object>> import2Excel(File file)
    			throws Exception {
    		// 数据头map
    		Map<String, String> headerMap = new HashMap<String, String>();
    		List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
    		// 工作簿
    		Workbook wb = null;
    		FileInputStream is = null;
    
    		is = new FileInputStream(file);
    		wb = WorkbookFactory.create(is);
    		// 读取行和列
    		Sheet sheet = wb.getSheetAt(0);
    		int row = sheet.getPhysicalNumberOfRows();
    		// 读取头
    		Row header = sheet.getRow(0);
    		// 列数
    		int column = header.getPhysicalNumberOfCells();
    		for (int i = 0; i < column; i++) {// 读取头
    			Cell tempCell = header.getCell(i);
    			String content = tempCell.getStringCellValue();
    			headerMap.put(String.valueOf(i), content);
    		}
    		// 读取记录
    		for (int i = 1; i < row; i++) {
    			Map<String, Object> tempMap = new HashMap<String, Object>();
    			Row tempRow = sheet.getRow(i);
    			for (int j = 0, length = tempRow.getPhysicalNumberOfCells(); j < length; j++) {
    				Cell tempCell = tempRow.getCell(j);
    				Object content = new Object();
    				switch (tempCell.getCellType()) {
    				case Cell.CELL_TYPE_FORMULA:
    					content = tempCell.getCellFormula();
    					break;
    				case Cell.CELL_TYPE_NUMERIC:
    					if (HSSFDateUtil.isCellDateFormatted(tempCell)) {
    						content = tempCell.getDateCellValue();
    					} else {
    						content = tempCell.getNumericCellValue();
    					}
    					break;
    				case Cell.CELL_TYPE_STRING:
    					content = tempCell.getStringCellValue();
    					break;
    				case Cell.CELL_TYPE_BOOLEAN:
    					content = tempCell.getBooleanCellValue();
    					break;
    				default:
    					content = "";
    				}
    				tempMap.put(headerMap.get(String.valueOf(j)), content);
    			}
    			returnList.add(tempMap);
    		}
    
    		return returnList;
    	}
     
  2. 创建excel
    /**
    	 * 创建07工作簿
    	 * 
    	 * @param title
    	 * @param dataSet
    	 * @param sheetname
    	 * @return
    	 * @throws Exception
    	 */
    	public static Workbook export2Excel07(Map<String, String> title,
    			List<Map<String, Object>> dataSet, String sheetname)
    			throws Exception {
    		// 创建工作簿
    		Workbook wb = new XSSFWorkbook();
    		Sheet sheet = wb.createSheet(sheetname);
    		// 输入头
    		Row headerRow = sheet.createRow(0);
    		int columnCount = 0;
    		Map<String, String> covertMap = new HashMap<String, String>();
    		for (Map.Entry<String, String> entry : title.entrySet()) {
    			String value = entry.getValue();
    			covertMap.put(String.valueOf(columnCount), entry.getKey());
    			headerRow.createCell(columnCount).setCellValue(value);
    			columnCount++;
    		}
    		// 输入记录
    		for (int i = 0; i < dataSet.size(); i++) {
    			Row tempRow = sheet.createRow(i + 1);
    			for (int j = 0; j < columnCount; j++) {
    				tempRow.createCell(j).setCellValue(
    						""
    								+ dataSet.get(i).get(
    										covertMap.get(String.valueOf(j))));
    			}
    		}
    		return wb;
    	}
     创建03的差不多、接口都一样,实现类不同一点、、、
    /**
    	 * 创建03工作簿
    	 * 
    	 * @param title
    	 * @param dataSet
    	 * @param sheetname
    	 * @return
    	 * @throws Exception
    	 */
    	public static HSSFWorkbook export2Excel03(Map<String, String> title,
    			List<Map<String, Object>> dataSet, String sheetname)
    			throws Exception {
    		// 创建工作簿
    		HSSFWorkbook wb = new HSSFWorkbook();
    		HSSFSheet sheet = wb.createSheet(sheetname);
    		// 输入头
    		Row headerRow = sheet.createRow(0);
    		int columnCount = 0;
    		Map<String, String> covertMap = new HashMap<String, String>();
    		for (Map.Entry<String, String> entry : title.entrySet()) {
    			String value = entry.getValue();
    			covertMap.put(String.valueOf(columnCount), entry.getKey());
    			headerRow.createCell(columnCount).setCellValue(value);
    			columnCount++;
    		}
    		// 输入记录
    		for (int i = 0; i < dataSet.size(); i++) {
    			Row tempRow = sheet.createRow(i + 1);
    			for (int j = 0; j < columnCount; j++) {
    				tempRow.createCell(j).setCellValue(
    						""
    								+ dataSet.get(i).get(
    										covertMap.get(String.valueOf(j))));
    			}
    		}
    		return wb;
    	}
     设置格式什么的就没有弄了、大概看看文档设置起来也容易、、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值