Apache POI 实现Excel文件单元格合并、冻结和文件导出

                  //经常用到poi导出excel操作,所以留几行代码,以备他日之需
Workbook wb = new HSSFWorkbook();
		Sheet sheet = wb.createSheet("sheet1");
		Row row = null;
		Cell cell = null;
		//创建表头单元格样式
		CellStyle cs_header = wb.createCellStyle();
		Font boldFont = wb.createFont();
		boldFont.setFontName("Consolas");
		boldFont.setFontHeightInPoints((short)14);
		cs_header.setFont(boldFont);
		cs_header.setBorderBottom((short)1);
		cs_header.setBorderLeft((short)1);
		cs_header.setBorderRight((short)1);
		cs_header.setBorderTop((short)1);
		//第一行,时间
		row = sheet.createRow((short)0);
		row.setHeightInPoints((short)24);
		createMultiCell(row,cell,cs_header,0,31,"访问日期:"+time1+" to "+time2);
		//第二行,大标题
		row = sheet.createRow((short)1);
		row.setHeightInPoints((short)24);
		createMultiCell(row,cell,cs_header,0,6," ");
		createMultiCell(row,cell,cs_header,6,25,"访问路径");
		//第三行,列标题
		
		row = sheet.createRow((short)2);
		row.setHeightInPoints((short)24);
		String[] headers = new String[]{"访问时间","地域","来源","关键字","进入页","停留时间","访问页数",
				"1时间","1停留","1页面","2时间","2停留","2页面","3时间","3停留","3页面","4时间","4停留","4页面",
				"5时间","5停留","5页面","6时间","6停留","6页面","7时间","7停留","7页面","8时间","8停留","8页面"};
		for(int i=0; i<headers.length; i++){
			cell = row.createCell((short)i);cell.setCellValue(headers[i]);cell.setCellStyle(cs_header);
		}
		//创建文本单元格样式
		CellStyle cs_text = wb.createCellStyle();
		Font textFont = wb.createFont();
		textFont.setFontName("Consolas");
		textFont.setFontHeightInPoints((short)10);
		cs_text.setFont(textFont);
		cs_text.setBorderBottom((short)1);
		cs_text.setBorderLeft((short)1);
		cs_text.setBorderRight((short)1);
		cs_text.setBorderTop((short)1);
		//将数据写入表格
		for(int i=0; i<list.size(); i++){
			row = sheet.createRow((short)(i+3));
			Object[] rw = list.get(i);
			for(int j=0; j<rw.length; j++){
				cell = row.createCell((short)j);
				cell.setCellValue(rw[j].toString());
				cell.setCellStyle(cs_text);
			}
		}
		//合并第1行1-32列
		sheet.addMergedRegion(new CellRangeAddress((short)0, (short)0, (short)0, (short)31));
		//合并第2行1-6列
		sheet.addMergedRegion(new CellRangeAddress((short)1, (short)1, (short)0, (short)6));
		//合并第2行7-32行
		sheet.addMergedRegion(new CellRangeAddress((short)1, (short)1, (short)7, (short)31));
		//冻结7X3(宽,高)区域中的单元格
		sheet.createFreezePane(7, 3);
		try {
			//将workbook写到输入流(下载时候,这个输出流可能是ServletOutStream,写入文件是FileOutputStream,等等)
			wb.write(os);
		} catch (IOException e) {
			e.printStackTrace();
		}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dyyaries

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值