`
jie_bosshr
  • 浏览: 144107 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

poi导出excel合并单元格(包括列合并、行合并)

阅读更多
1 工程所需jar包如下:
commons-codec-1.5.jar
commons-logging-1.1.jar
log4j-1.2.13.jar
junit-3.8.1.jar
poi-3.9-20121203.jar

2 Code:
	/**
	 * 导出设备信息Excel
	 * @param form 	    和 HTTP 请求相关的表格对象
	 * @param resources 信息资源对象
	 * @param locale    本地化对象
	 * @param session   HTTP 会话对象
	 * @param request   HTTP 请求对象
	 * @param response  HTTP 响应对象
	 * @return
	 */
	public String exportExcel(DynaBean form, MessageResources resources,
			Locale locale, HttpSession session, HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		int iLanguage = (locale.getLanguage().indexOf("en")>=0)?0:1;
		response.reset();
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition","attachment;filename="+java.net.URLEncoder.encode(resources.getMessage(locale, "device.details")+".xls","UTF-8"));
		OutputStream sos = response.getOutputStream();
		List<DeviceVO> deviceList = dao.getAllDevice();
		HSSFWorkbook wb = new HSSFWorkbook();
		Map<String, CellStyle> styles = createStyles(wb);
		// 创建sheet页
		Sheet sheet = wb.createSheet("Sheet");
		PrintSetup printSetup = sheet.getPrintSetup();
		printSetup.setLandscape(true);
		sheet.setFitToPage(true);
		sheet.setHorizontallyCenter(true);
		/**
		*合并单元格的行或者列
		*/
		sheet.addMergedRegion(CellRangeAddress.valueOf("$F$1:$H$1"));
		sheet.addMergedRegion(CellRangeAddress.valueOf("$M$1:$P$1"));
		sheet.addMergedRegion(CellRangeAddress.valueOf("$Q$1:$S$1"));
		sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$A$2"));
		sheet.addMergedRegion(CellRangeAddress.valueOf("$B$1:$B$2"));
		sheet.addMergedRegion(CellRangeAddress.valueOf("$C$1:$C$2"));
		sheet.addMergedRegion(CellRangeAddress.valueOf("$D$1:$D$2"));
		sheet.addMergedRegion(CellRangeAddress.valueOf("$E$1:$E$2"));
		sheet.addMergedRegion(CellRangeAddress.valueOf("$I$1:$I$2"));
		sheet.addMergedRegion(CellRangeAddress.valueOf("$J$1:$J$2"));
		sheet.addMergedRegion(CellRangeAddress.valueOf("$K$1:$K$2"));
		sheet.addMergedRegion(CellRangeAddress.valueOf("$L$1:$L$2"));
		sheet.addMergedRegion(CellRangeAddress.valueOf("$T$1:$T$2"));
		sheet.addMergedRegion(CellRangeAddress.valueOf("$U$1:$U$2"));
		sheet.addMergedRegion(CellRangeAddress.valueOf("$V$1:$V$2"));
		sheet.addMergedRegion(CellRangeAddress.valueOf("$W$1:$W$2"));
		sheet.addMergedRegion(CellRangeAddress.valueOf("$X$1:$X$2"));
		sheet.addMergedRegion(CellRangeAddress.valueOf("$Y$1:$Y$2"));
		sheet.addMergedRegion(CellRangeAddress.valueOf("$Z$1:$Z$2"));
		sheet.addMergedRegion(CellRangeAddress.valueOf("$AA$1:$AA$2"));
		sheet.addMergedRegion(CellRangeAddress.valueOf("$AB$1:$AB$2"));
		

		
		// 创建表头
		Row headerRow = sheet.createRow(0);
		headerRow.setHeightInPoints(30);
		Cell headerCell;
		
		headerCell = headerRow.createCell(0);
		headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.number")); //设备编号
		headerCell.setCellStyle(styles.get("header"));

		headerCell = headerRow.createCell(1);
		headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.qrcode")); //设备二维码
		headerCell.setCellStyle(styles.get("header"));

		headerCell = headerRow.createCell(2);
		headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.customerbase")); //客户群
		headerCell.setCellStyle(styles.get("header"));

		headerCell = headerRow.createCell(3);
		headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.customertype")); //客户类别
		headerCell.setCellStyle(styles.get("header"));

		headerCell = headerRow.createCell(4);
		headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.customername")); //客户名称
		headerCell.setCellStyle(styles.get("header"));

		headerCell = headerRow.createCell(5);
		headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.area")); //设备区域
		headerCell.setCellStyle(styles.get("header"));

		headerCell = headerRow.createCell(8);
		headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.itemname")); //所属项目名称
		headerCell.setCellStyle(styles.get("header"));

		headerCell = headerRow.createCell(9);
		headerCell.setCellValue(resources.getMessage(locale, "device.category")); //设备类别
		headerCell.setCellStyle(styles.get("header"));

		headerCell = headerRow.createCell(10);
		headerCell.setCellValue(resources.getMessage(locale, "device.name")); //设备名称
		headerCell.setCellStyle(styles.get("header"));

		headerCell = headerRow.createCell(11);
		headerCell.setCellValue(resources.getMessage(locale, "device.no")); //设备信息编号
		headerCell.setCellStyle(styles.get("header"));

		headerCell = headerRow.createCell(12);
		headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.baseinfomation")); //设备基本信息
		headerCell.setCellStyle(styles.get("header"));
		
		headerCell = headerRow.createCell(16);
		headerCell.setCellValue(resources.getMessage(locale, "device.location")); //设备位置
		headerCell.setCellStyle(styles.get("header"));
		
		headerCell = headerRow.createCell(19);
		headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.enabledate")); //设备启用日期
		headerCell.setCellStyle(styles.get("header"));
		
		headerCell = headerRow.createCell(20);
		headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.backendload")); //后端负载
		headerCell.setCellStyle(styles.get("header"));
		
		headerCell = headerRow.createCell(21);
		headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.operationtips")); //操作提示
		headerCell.setCellStyle(styles.get("header"));
		
		headerCell = headerRow.createCell(22);
		headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.maintenancepeople")); //维护责任人
		headerCell.setCellStyle(styles.get("header"));
		
		headerCell = headerRow.createCell(23);
		headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.usetime")); //巡检时间
		headerCell.setCellStyle(styles.get("header"));
		
		headerCell = headerRow.createCell(24);
		headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.intervaltime")); //巡检下台设备时间间隔
		headerCell.setCellStyle(styles.get("header"));
		
		headerCell = headerRow.createCell(25);
		headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.patrolpath")); //巡检路径
		headerCell.setCellStyle(styles.get("header"));
		
		headerCell = headerRow.createCell(26);
		headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.enable")); //是否启用巡检路径
		headerCell.setCellStyle(styles.get("header"));
		
		headerCell = headerRow.createCell(27);
		headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.other")); //其他信息
		headerCell.setCellStyle(styles.get("header"));
		
		
		Row headerRowRegion = sheet.createRow(1);
		headerRowRegion.setHeightInPoints(15);
		Cell headerCellRegion;
		headerCellRegion = headerRowRegion.createCell(5);
		headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.province")); //省份
		headerCellRegion.setCellStyle(styles.get("header"));
		
		headerCellRegion = headerRowRegion.createCell(6);
		headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.region")); //市
		headerCellRegion.setCellStyle(styles.get("header"));
		
		headerCellRegion = headerRowRegion.createCell(7);
		headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.county")); //县/区
		headerCellRegion.setCellStyle(styles.get("header"));
		
		headerCellRegion = headerRowRegion.createCell(12);
		headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.brand")); //品牌
		headerCellRegion.setCellStyle(styles.get("header"));
		
		headerCellRegion = headerRowRegion.createCell(13);
		headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.model")); //型号
		headerCellRegion.setCellStyle(styles.get("header"));
		
		headerCellRegion = headerRowRegion.createCell(14);
		headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.capacity")); //容量
		headerCellRegion.setCellStyle(styles.get("header"));
		
		headerCellRegion = headerRowRegion.createCell(15);
		headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.systemtype")); //系统类型
		headerCellRegion.setCellStyle(styles.get("header"));
		
		headerCellRegion = headerRowRegion.createCell(16);
		headerCellRegion.setCellValue(resources.getMessage(locale, "device.build")); //楼号
		headerCellRegion.setCellStyle(styles.get("header"));
		
		headerCellRegion = headerRowRegion.createCell(17);
		headerCellRegion.setCellValue(resources.getMessage(locale, "device.floor")); //楼层
		headerCellRegion.setCellStyle(styles.get("header"));
		
		headerCellRegion = headerRowRegion.createCell(18);
		headerCellRegion.setCellValue(resources.getMessage(locale, "device.room")); //房间号
		headerCellRegion.setCellStyle(styles.get("header"));
		
		for(int i=0;i<deviceList.size();i++){
			DeviceVO device = deviceList.get(i);
			Cell rowCell;
			Row cellRow = sheet.createRow(i + 2);
			
			rowCell = cellRow.createCell(0);
			rowCell.setCellValue(device.getDeviceId());
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(1);
			rowCell.setCellValue(device.getQrcode());
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(2);
			//判断是否是中文
			if(iLanguage==1){
				rowCell.setCellValue(device.getItemId().getCustomerBase().substring(device.getItemId().getCustomerBase().indexOf(":")+1,device.getItemId().getCustomerBase().length()));
			}else if(iLanguage==0){ //判断是否是英文
				rowCell.setCellValue(device.getItemId().getCustomerBase().substring(0,device.getItemId().getCustomerBase().indexOf(":")));
			}
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(3);
			//判断是否是中文
			if(iLanguage==1){
				rowCell.setCellValue(device.getItemId().getCustomerType().substring(device.getItemId().getCustomerType().indexOf(":")+1,device.getItemId().getCustomerType().length()));
			}else if(iLanguage==0){ //判断是否是英文
				rowCell.setCellValue(device.getItemId().getCustomerType().substring(0,device.getItemId().getCustomerType().indexOf(":")));
			}
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(4);
			rowCell.setCellValue(device.getItemId().getCustomerName());
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(5);
			rowCell.setCellValue(device.getItemId().getProvince());
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(6);
			rowCell.setCellValue(device.getItemId().getCity());
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(7);
			rowCell.setCellValue(device.getItemId().getCounty());
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(8);
			//判断是否是中文
			if(iLanguage==1){
				rowCell.setCellValue(device.getItemId().getItemName());
			}else if(iLanguage==0){ //判断是否是英文
				rowCell.setCellValue(device.getItemId().getForShort());
			}
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(9);
			//判断是否是中文
			if(iLanguage==1){
				rowCell.setCellValue(device.getZequipId().getZequipGroup().getNameLoc());
			}else if(iLanguage==0){ //判断是否是英文
				rowCell.setCellValue(device.getZequipId().getZequipGroup().getNameEn());
			}
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(10);
			rowCell.setCellValue(device.getDeviceName());
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(11);
			rowCell.setCellValue(device.getDeviceNo());
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(12);
			rowCell.setCellValue(device.getDeviceBrand());
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(13);
			rowCell.setCellValue(device.getEquipDriveId().getModel());
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(14);
			rowCell.setCellValue(device.getDeviceCapacity());
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(15);
			rowCell.setCellValue(device.getSystemType());
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(16);
			rowCell.setCellValue(device.getStairsNo());
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(17);
			rowCell.setCellValue(device.getFloor());
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(18);
			rowCell.setCellValue(device.getRoomNo());
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(19);
			rowCell.setCellValue(device.getEnableDate()!=null ? new SimpleDateFormat("yyyy-MM-dd").format(device.getEnableDate()) : "");
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(20);
			rowCell.setCellValue(device.getBackendLoad());
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(21);
			rowCell.setCellValue(device.getOperationTips());
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(22);
			rowCell.setCellValue(device.getPersonId().getUserName());
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(23);
			rowCell.setCellValue(device.getInspecTime()+resources.getMessage(locale, "device.export.excel.minute"));
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(24);
			rowCell.setCellValue(device.getIntervalTime()+resources.getMessage(locale, "device.export.excel.minute"));
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(25);
			rowCell.setCellValue(device.getDevicePath());
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(26);
			rowCell.setCellValue(device.getValidityNr() == 0 ? resources.getMessage(locale, "mmc.soft.person.disabled") : resources.getMessage(locale, "mmc.soft.person.enable"));
			rowCell.setCellStyle(styles.get("cell"));
			
			rowCell = cellRow.createCell(27);
			rowCell.setCellValue(device.getOtherInfo() != null ? device.getOtherInfo() : "");
			rowCell.setCellStyle(styles.get("cell"));
		}
		wb.write(sos);
		sos.flush();
		sos.close();
		return null;
	}
	
	//excel样式
	private Map<String, CellStyle> createStyles(Workbook wb)
	{
		Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
		CellStyle style;
		Font titleFont = wb.createFont();
		titleFont.setFontHeightInPoints((short) 18);
		titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
		style = wb.createCellStyle();
		style.setAlignment(CellStyle.ALIGN_CENTER);
		style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		style.setFont(titleFont);
		styles.put("title", style);

		style = wb.createCellStyle();
		style.setAlignment(CellStyle.ALIGN_CENTER);
		style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		style.setWrapText(true);
		styles.put("header", style);

		style = wb.createCellStyle();
		style.setAlignment(CellStyle.ALIGN_CENTER);
		style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		style.setWrapText(true);
		styles.put("cell", style);

		style = wb.createCellStyle();
		style.setAlignment(CellStyle.ALIGN_CENTER);
		style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
		style.setDataFormat(wb.createDataFormat().getFormat("0.00"));
		styles.put("formula", style);

		style = wb.createCellStyle();
		style.setAlignment(CellStyle.ALIGN_CENTER);
		style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
		style.setDataFormat(wb.createDataFormat().getFormat("0.00"));
		styles.put("formula_2", style);

		return styles;
	}
分享到:
评论
1 楼 贝塔ZQ 2017-01-04  
poi生成excel文件,之前也用过,就是感觉代码好多啊。后来查了查 ,网上很多插件也是不错的,pageoffice插件就不错,Sheet sheet = wb.openSheet("Sheet1");sheet.openTable("B4:B6").merge();
Cell cB4 = sheet.openCell("B4");
cB4.setValue("数值1");
//设置水平对齐方式
cB4.setHorizontalAlignment(XlHAlign.xlHAlignCenter);
//设置垂直对齐方式
cB4.setVerticalAlignment(XlVAlign.xlVAlignCenter); 可以看看的

相关推荐

    POI操作Excel合并单元格边框问题解决方法

    POI操作Excel 合并单元格 边框问题解决方法,这个方法是我亲自测试,并运用于项目中的,可以放心使用,还一并写出了很多注释,其中有写poi的bug的解决方法

    利用poi进行excel相同内容合并单元格并转成流输出

    利用poi进行excel相同内容合并单元格,只摘要了传入部分功能,请关注addMergedRegion方法

    POI导出Excel工具类,自动设置标题 列名 文件名,可插入图片,合并单元格

    在这个场景中,我们关注的是如何使用POI来创建一个功能丰富的Excel导出工具类,它能够自动设置标题、列名、文件名,并且支持插入图片以及合并单元格。下面将详细介绍这些功能的实现。 首先,要创建一个Excel工作簿...

    easyuiPoi导出Excel工具类封装(支持合并单元格)

    EasyUIPoI是一款基于Apache POI的Java工具库,专门用于简化Excel的导出操作,尤其在处理模板和合并单元格方面提供了便利。这个库主要适用于那些需要在Web应用中生成Excel报告或导出数据的场景,比如数据分析、报表...

    POI合并单元格

    java POI导出Excel文件多表头文件

    poi生成Excel及合并单元格示例

    单元格合并是通过 `Sheet` 对象的 `mergeRegion` 方法实现的,该方法接受两个参数,分别表示要合并的区域的起始和结束单元格索引: ```java // 合并从第二列到第四列的单元格(索引从0开始) sheet.mergeRegions...

    excel poi工具类 导入 导出 合并单元格 计算公式

    以上就是关于使用Apache POI进行Excel操作的核心知识点,包括导入、导出、合并单元格以及处理计算公式的方法。在实际项目中,可以根据需求进行更复杂的功能定制,如设置单元格格式、读取数据验证规则等。

    Excel报表导出,复杂Excel模板导出(带单元格合并),jxls2

    总结起来,这个项目通过`jxls2`库提供了一种高效、灵活的Excel报表导出解决方案,支持单元格合并和复杂的模板设计,能够满足多样化的需求,对于提升企业级应用的数据处理能力有着显著的作用。开发者只需花费较少的...

    将HtmlTable 导出为Execl文件,支持合并单元格、合并行,无需Office支持

    总之,将HTML表格导出为Excel并支持合并单元格和合并行是一项实用的技术,它扩展了网页应用的功能,使用户能够方便地处理和分享结构化的数据。通过理解和实现这样的功能,开发者可以提升用户体验,特别是在数据报告...

    NPOI读取模板导出Excel(合并单元格)

    ### NPOI读取模板导出Excel(合并单元格) #### 概述 在实际工作中,经常需要将数据导出到Excel文件中,并且在某些情况下还需要对单元格进行合并处理,以达到美观或者特定需求的目的。NPOI是.NET平台上一个强大的...

    poi导入/导出Excel表格,合并单元格的读取和设置

    poi导入/导出Excel表格,合并单元格的读取和设置

    关于java EXCEL poi 合并后无法显示或无法设置边框问题

    关于java EXCEL poi 合并后无法显示或无法设置边框问题 【本人亲测】 excel poi 新手! 坑了1.5天!

    JAVAeasypoi根据Excel模板导出 循环遍历合并单元格处理

    【JAVA】easypoi根据Excel模板导出 循环遍历合并单元格处理

    POI实战总结

    10、添加区域,合并单元格 11、保存Excel文件 12、根据单元格不同属性返回字符串数值 13、常用单元格边框格式 14、设置字体和内容位置 15、插入图片 16、调整工作表位置 17、设置打印区域 18、标注脚注 19...

    excel合并单元格导入

    标题“Excel合并单元格导入”涉及的是在编程环境中处理Excel数据时,如何处理那些具有合并单元格的表格。在Excel中,合并单元格通常用于格式化或者突出显示重要的信息,但在进行数据导入或分析时,这可能会带来挑战...

    POI中设置Excel单元格格式

    以上就是关于使用 POI 设置 Excel 单元格格式的详细说明,包括背景色、边框、居中、字体、列宽、自动换行以及合并单元格等操作。这些技巧可以帮助开发者更加灵活地控制 Excel 文件的格式,从而提升数据的可读性和...

    导出Excel多个单元格合并及图片插入

    ### 导出Excel多个单元格合并及图片插入 #### 问题描述 在安全生产项目中,为了更好地管理和展示巡检记录,通常需要将这些记录导出为Excel文件,并且每个巡检项都应附带相应的抓图。这不仅要求能够有效地处理数据...

    java实现合并单元格的同时并导出excel示例

    Java实现合并单元格的同时并导出Excel示例 Java实现合并单元格的同时并导出Excel是软件开发中常见的一种需求,特别是在报表系统和数据处理中。本文将详细介绍Java实现合并单元格的同时并导出Excel的相关知识点,并...

    Freemarker实现excel导出功能

    在提供的压缩包`freemarker总结-例子`中,可能包含了一个简单的示例项目,展示如何使用Freemarker和POI实现Excel导出。这个例子通常包括以下部分: 1. 一个`.ftl`模板文件,描述了Excel表格的布局。 2. Java代码,...

    Java实现POI导出Excel

    Java实现POI导出Excel是Java开发者常用的一种技术,用于生成和操作Microsoft Office Excel文件。在Java中,Apache POI库提供了对微软Office文档格式的支持,包括读取和写入Excel文件。这篇博客文章...

Global site tag (gtag.js) - Google Analytics