poi4设置字体格式

1 篇文章 0 订阅
			List<Map<String, String>> labelInformations = new ArrayList();			

			HSSFWorkbook workbook = new HSSFWorkbook();
			// 创建工作表sheeet
			HSSFSheet sheet = workbook.createSheet();
			HSSFCellStyle cellStyle = workbook.createCellStyle();
			// cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			cellStyle.setBorderBottom(BorderStyle.THIN);
			cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
			cellStyle.setBorderLeft(BorderStyle.THIN);
			cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
			cellStyle.setBorderRight(BorderStyle.THIN);
			cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
			cellStyle.setBorderTop(BorderStyle.THIN);
			cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
			
			cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
			
			HSSFFont font = workbook.createFont(); //设置字体的样式
			font.setFontHeightInPoints((short)11); //字体大小只能是整数
			//如果想将字体变成11.5
			font.setFontHeight((short)(11.5*20));//这才是11.5号字体
		
			//font.setBold(true);//加粗
			//font.setFontName("simsun"); //字体
			//font.setFontName("微软雅黑"); //字体
			font.setFontName("宋体"); //字体
			cellStyle.setFont(font);
			
			// sheet.setColumnWidth(0, 256*width+184);
			sheet.setColumnWidth(0, 27 * 256 + 184);
			sheet.setColumnWidth(1, 600);
			sheet.setColumnWidth(2, 27 * 256 + 184);
			sheet.setColumnWidth(3, 600);
			sheet.setColumnWidth(4, 27 * 256 + 184);
			// sheet.setDefaultRowHeightInPoints((float) 20*256);无效
			// cell.setCellStyle(cellStyle);
			ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
			HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
			// 追加数据
			for (int i = 0; i <= labelInformations.size() / 3; i++) {
				HSSFRow row1 = sheet.createRow(3 * i);
				HSSFRow row2 = sheet.createRow(3 * i + 1);
				HSSFRow row3 = sheet.createRow(3 * i + 2);
				row1.setHeightInPoints((float) 20);
				row2.setHeightInPoints((float) 20);
				row3.setHeightInPoints((float) 12);
				for (int a = 0; a < 3; a++) {
					if (3 * i + a >= labelInformations.size())
						break;
					HSSFCell cell1 = row1.createCell(a * 2);
					HSSFCell cell2 = row2.createCell(a * 2);
					HSSFCell cell3 = row3.createCell(a * 2);
					cell1.setCellStyle(cellStyle);
					cell2.setCellStyle(cellStyle);
					cell3.setCellStyle(workbook.createCellStyle());
					Map<String, String> labelInformation = labelInformations.get(3 * i + a);
					// 添加图片-
					String barcodepath = SHORTBARCODE_PATH + "noNumber" + labelInformation.get("product_code") + ".png";
					BarcodeUtil.generateByCODE128(labelInformation.get("product_code"), barcodepath, 0.4, 10, 0);
					HSSFClientAnchor anchor = new HSSFClientAnchor(650, 80, 990, 200, (short) (2 * a), 3 * i,
							(short) (2 * a), 3 * i);
					BufferedImage bufferImg = ImageIO.read(new File(barcodepath));
					ImageIO.write(bufferImg, "png", byteArrayOut);
					byte[] imgtypes = byteArrayOut.toByteArray();
					int puctureIndex = workbook.addPicture(imgtypes, Workbook.PICTURE_TYPE_PNG);
					patriarch.createPicture(anchor, puctureIndex);
					cell1.setCellValue(
							labelInformation.get("sterilized_lot_number") + "/" + labelInformation.get("product_code"));
					cell2.setCellValue(
							labelInformation.get("bracket_model") + " " + labelInformation.get("bracket_spec"));
				}
			}
			// 创建一个文件
			String timestamp = System.currentTimeMillis() + "";
			File file = new File(IDENTIFICATIONBAR_PATH + timestamp + ".xls");
			file.createNewFile();
			FileOutputStream stream = new FileOutputStream(file);
			workbook.write(stream);
			stream.close();
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值