poi excel

1. 包描述

         HSSF - 提供读写Microsoft Excel XLS格式档案的功能。

         XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。

         HWPF - 提供读写Microsoft Word DOC格式档案的功能。

         HSLF - 提供读写Microsoft PowerPoint格式档案的功能。

         HDGF - 提供读Microsoft Visio格式档案的功能。

         HPBF - 提供读Microsoft Publisher格式档案的功能。

         HSMF - 提供读Microsoft Outlook格式档案的功能。

 

2,相关java测试代码

/**
	 * excel
	 */
	private static void exportExcel(String fileName) {
		//1.声明一个工作薄
		HSSFWorkbook workbook = new HSSFWorkbook();
		//2.生成一个表格
		HSSFSheet sheet = workbook.createSheet("sheet_title");
		
		//3.表格设置
		// 3.1 合并单元格  cell1(row,column),cell2(row,column)
		sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
		// 3.2 列自适应
		sheet.autoSizeColumn(0);
		// 3.3 数据验证
		// sheet.addValidationData(dataValidation)
		// 3.4设置表格默认列宽度为15个字节
		sheet.setDefaultColumnWidth((short) 15);

		// 3.5 设置列宽,对具体每一列进行设值
		// sheet.setColumnWidth(0, 256 * 13);
		// sheet.setColumnWidth(1, 4000);

		//4 样式设置
		// 4.1 生成一个样式
		HSSFCellStyle style = workbook.createCellStyle();
		// 4.2 背景颜色
		style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);// 填充颜色
		style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);// 填充方式
		/**
		 * 自定义背景颜色 
		 * HSSFPalette palette = workbook.getCustomPalette(); //wb
		 * HSSFWorkbook对象 palette.setColorAtIndex((short) 9, (byte) (181),
		 * (byte) (193), (byte) (230)); style.setFillForegroundColor((short) 9);
		 */

		// 4.3 设置上下左右边框
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);

		// 4.4 设置居中对齐
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 4.5 设置垂直居中
		style.setVerticalAlignment((short) 1);

		// 4.6 自动换行
		style.setWrapText(true);
		// cell.setCellValue("row \r\n one");

		//4.7 字体
		HSSFFont fontBoldBlack12 = workbook.createFont();
		fontBoldBlack12.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
		fontBoldBlack12.setColor(HSSFColor.BLACK.index);// 字体颜色
		fontBoldBlack12.setFontHeightInPoints((short) 12);// 字体大小

		style.setFont(fontBoldBlack12);

		// 4.8声明一个画图的顶级管理器
		HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
		// 定义注释的大小和位置,详见文档
		HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0,
				0, 0, 0, (short) 4, 2, (short) 6, 5));
		// 设置注释内容
		comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));
		// 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容.
		comment.setAuthor("leno");

		// 4.9  对一个cell设置多个字体样式,可以用 HSSFRichTextString
		HSSFRichTextString text = new HSSFRichTextString("header1");
		// text.applyFont(startIndex, endIndex, font)
		// cell.setCellValue(text);

		// 创建行
		Row row1 = sheet.createRow(0);
		// 创建列
		Cell cell1 = row1.createCell(0);
		// 设置样式
		cell1.setCellStyle(style);
		// 设置值
		cell1.setCellValue("cell value");
		// 设置类型
		cell1.setCellType(Cell.CELL_TYPE_STRING);
		cell1.setCellComment(comment);
		// cell1.setCellErrorValue(arg0)
		
		//4.10   创建超链接  
	    HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);  
	    link.setAddress("http://www.baidu.com");  
	    cell1.setCellValue("百度");  
	    cell1.setHyperlink(link);// 设定单元格的链接  


		// 有图片时,设置行高为60px;
		// row1.setHeightInPoints(60);
		// 设置图片所在列宽度为80px,注意这里单位的一个换算
		// sheet.setColumnWidth(1, (short) (35.7 * 80));
		// sheet.autoSizeColumn(i);
		// byte[] bsValue = (byte[]) value;
		// HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0,
		// 1023, 255, (short) 6, index, (short) 6, index);
		// anchor.setAnchorType(2);
		// patriarch.createPicture(anchor, workbook.addPicture(
		// bsValue, HSSFWorkbook.PICTURE_TYPE_JPEG));

		// 将文件存到指定位置
		try {
			FileOutputStream fout = new FileOutputStream(fileName);
			workbook.write(fout);
			fout.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 如果采用 spring mvc, control如下:
		// HSSFWorkbook wb = service.export(list);
		// response.setContentType("application/vnd.ms-excel");
		// response.setHeader("Content-disposition",
		// "attachment;filename=excelName.xls");
		// OutputStream ouputStream = response.getOutputStream();
		// wb.write(ouputStream);
		// ouputStream.flush();
		// ouputStream.close();
	}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值