POI操作Excel

一. Apache 的 POI 可以操作ole对象,对Excel的操作更是方便。

 

http://poi.apache.org/hssf/quick-guide.html

 

quick-guide中介绍了基本的对Excel的控制,可以用JUnit逐项测试,直观的了解下功能。

 

功能项如下:

How to create a new workbook How to create a sheet How to create cells How to create date cells Working with different types of cells Iterate over rows and cells Text Extraction Aligning cells Working with borders Fills and color Merging cells Working with fonts Custom colors Reading and writing Use newlines in cells. Create user defined data formats Fit Sheet to One Page Set print area for a sheet Set page numbers on the footer of a sheet Shift rows Set a sheet as selected Set the zoom magnification for a sheet Create split and freeze panes Repeating rows and columns Headers and Footers Drawing Shapes Styling Shapes Shapes and Graphics2d Outlining Images Named Ranges and Named Cells How to set cell comments How to adjust column width to fit the contents Hyperlinks

 

二. 对合并单元格的格式化

 

for (int i = region.getRowFrom(); i <= region.getRowTo(); i++) {

	HSSFRow row = HSSFCellUtil.getRow(i, sheet);

	for (int j = region.getColumnFrom(); j <= region.getColumnTo(); j++) {

		HSSFCell cell = HSSFCellUtil.getCell(row, (short) j);

		//给合并的单元格加边框
		HSSFCellStyle style = wb.createCellStyle();
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style.setBottomBorderColor(HSSFColor.BLACK.index);
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style.setLeftBorderColor(HSSFColor.BLACK.index);
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style.setRightBorderColor(HSSFColor.BLACK.index);
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style.setTopBorderColor(HSSFColor.BLACK.index);

		cell.setCellStyle(style);
	}
}

 

三. 输出文件流

 

//获取到HSSFWorkbook实例(你生成的Excel)
HSSFWorkbook wb = yourManager.generateExcel();

//处理汉字文件名
		String fileName = "统计表";
		fileName = new String(fileName.getBytes("gb2312"), "iso8859-1") + ".xls";

		try {
			response.reset();
			response.setHeader("content-disposition", "attachment;filename=" + fileName);
			response.setContentType("APPLICATION/msexcel");
			wb.write(response.getOutputStream());
		} catch (Exception e) {
			System.out.print(e + "生成Excel统计报表失败!");
		}

  

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值