导出打印二维码demo

打印二维码demo,由于打印格式,区域等需要设置,所以先设置好模板

	@Override
	public void pcExportWebsiteQrcode(ZxQrcodeWebsite zxQrcodeWebsite, HttpServletResponse response) {
		// excel导出模板路径
		String xlsxPath = HttpUtil.getTemplate("zxqrcode") + "二维码导出模板.xlsx";
		Workbook workbook = null;
		OutputStream out = null;
		try {
			workbook = WorkbookFactory.create(new FileInputStream(xlsxPath));
			Sheet sheet = workbook.getSheetAt(0);
			// 设置列宽
			sheet.setColumnWidth(3, (short) (18.13 * 256));
			// 设置样式
			CellStyle borderStyle = workbook.createCellStyle();
			// 设置单元格内容水平对其方式 居中对齐
			borderStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
			// 设置单元格内容垂直对其方式 中对齐
			borderStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
			// borderStyle.setRotation((short)-90);//旋转-90
			// 文本换行
			borderStyle.setWrapText(true);
			// 画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
			Drawing patriarch = sheet.createDrawingPatriarch();
			// 设置上下二维码间隔基数
			int j = 0;
			// 获取数据
			List<ZxQrcodeWebsite> zxQrcodeWebsiteList = zxQrcodeWebsiteMapper.pcGetWebsiteList(zxQrcodeWebsite);
			for (int i = 0; i < zxQrcodeWebsiteList.size(); i++) {
				// 第一行(占位)
				Row row = sheet.createRow(i + j);
				// 设置行高
				row.setHeightInPoints((float) 18);
				// 第二行
				row = sheet.createRow(i + j + 1);
				// 设置行高
				row.setHeightInPoints((float) 35.25);
				// 设置单元格合并
				sheet.addMergedRegion(new CellRangeAddress(i + j + 1, i + j + 1, 0, 2));
				Cell cell = row.createCell(0);
				cell.setCellValue("标题: " + zxQrcodeWebsiteList.get(i).getViewTitle());
				cell.setCellStyle(borderStyle);

				// 第三行(占位)
				row = sheet.createRow(i + j + 2);
				// 设置行高
				row.setHeightInPoints((float) 35.25);
				// 第四行
				row = sheet.createRow(i + j + 3);
				// 设置行高
				row.setHeightInPoints((float) 35.25);
				// 设置单元格合并
				sheet.addMergedRegion(new CellRangeAddress(i + j + 3, i + j + 3, 0, 2));
				cell = row.createCell(0);
				cell.setCellValue("描述: " + zxQrcodeWebsiteList.get(i).getViewDesc());
				cell.setCellStyle(borderStyle);

				// 第五行(占位)
				row = sheet.createRow(i + j + 4);
				// 设置行高(占位)
				row.setHeightInPoints((float) 18);
				// 第六行
				row = sheet.createRow(i + j + 5);
				// 设置行高
				// row.setHeightInPoints((float) 41.25);
				row.setHeightInPoints((float) 42.75);
				// 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
				ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
				BufferedImage bufferImg = ImageIO.read(
						new File(HttpUtil.getUploadPath("zxqrcode") + zxQrcodeWebsiteList.get(i).getQrcodeName()));
				ImageIO.write(bufferImg, "jpg", byteArrayOut);
				// anchor主要用于设置图片的属性
				// 这里dx1、dy1定义了该图片在开始cell的起始位置,dx2、dy2定义了在终cell的结束位置。col1、row1定义了开始cell、col2、row2定义了结束cell。
				// HSSFClientAnchor(int dx1,int dy1,int dx2,int dy2,short
				// col1,int row1,short col2, int row2);
				XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) 3, i + j + 1, (short) 4, i + j + 4);
				anchor.setAnchorType(3);
				// 插入图片
				patriarch.createPicture(anchor,
						workbook.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG));
				j += 5;
			}
			// 设置response下载弹窗
			response.reset();
			// 加载xls文件
			// response.setContentType("application/vnd.ms-excel;charset=utf-8");
			// 加载xlsx文件
			response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
			// 没有后缀
			// response.setContentType("application/msexcel;charset=utf-8");
			// 后缀也可以在filename后面指定
			// 跨域访问
			response.setHeader("Access-Control-Allow-Origin", "*");
			// 重命名
			response.setHeader("Content-Disposition",
					"attachment; filename=\"" + new String("二维码打印列表.xlsx".getBytes("utf-8"), "iso-8859-1") + "\"");
			out = response.getOutputStream();
			workbook.write(out);
			out.flush();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

还有如果不用模板则用下面这个

@Override
	public void pcExportWebsiteQrcode2(ZxQrcodeWebsite zxQrcodeWebsite, HttpServletResponse response) {
		// 创建excel(2007以上版本)
		Workbook workbook = new XSSFWorkbook();
		// 新建工作表
		Sheet sheet = workbook.createSheet("二维码打印列表");
		// 画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
		Drawing patriarch = sheet.createDrawingPatriarch();
		// 设置第三列列宽
		sheet.setColumnWidth(3, (short) (18.13 * 256));
		// 设置样式
		CellStyle style = workbook.createCellStyle();
		// 文本换行
		style.setWrapText(true);
		// 设置单元格内容水平对其方式 居中对齐
		style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
		// 设置单元格内容垂直对其方式 居中对齐
		style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
		// 强制设置字体格式为文本(否则默认是常规)
		style.setDataFormat(workbook.createDataFormat().getFormat("@"));
		// // 设置上边框
		// dataStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
		// // 设置右边框
		// dataStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
		// // 设置下边框
		// dataStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);

		List<ZxQrcodeWebsite> zxQrcodeWebsiteList = zxQrcodeWebsiteMapper.pcGetWebsiteList(zxQrcodeWebsite);
		if (zxQrcodeWebsiteList.size() > 0) {
			int j = 0;
			for (int i = 0; i < zxQrcodeWebsiteList.size(); i++) {
				// 第一行(占位)
				Row row = sheet.createRow(i + j);
				// 设置行高
				row.setHeightInPoints((float) 18);
				// 第二行
				row = sheet.createRow(i + j + 1);
				// 设置行高
				row.setHeightInPoints((float) 35.25);
				// 设置单元格合并
				sheet.addMergedRegion(new CellRangeAddress(i + j + 1, i + j + 1, 0, 2));
				// 标题
				Cell dataCell = row.createCell(0);
				dataCell.setCellValue("标题: " + zxQrcodeWebsiteList.get(i).getViewTitle());
				dataCell.setCellStyle(style);

				// 第三行(占位)
				row = sheet.createRow(i + j + 2);
				// 设置行高
				row.setHeightInPoints((float) 35.25);
				// 第四行
				row = sheet.createRow(i + j + 3);
				// 设置行高
				row.setHeightInPoints((float) 35.25);
				// 设置单元格合并
				sheet.addMergedRegion(new CellRangeAddress(i + j + 3, i + j + 3, 0, 2));
				dataCell = row.createCell(0);
				// 描述
				dataCell = row.createCell(0);
				dataCell.setCellValue("描述: " + zxQrcodeWebsiteList.get(i).getViewDesc());
				dataCell.setCellStyle(style);

				// 第五行(占位)
				row = sheet.createRow(i + j + 4);
				// 设置行高
				row.setHeightInPoints((float) 18);
				// 第六行(占位)
				row = sheet.createRow(i + j + 5);
				// 设置行高
				row.setHeightInPoints((float) 42.75);

				ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
				BufferedImage bufferImg;
				try {
					bufferImg = ImageIO.read(
							new File(HttpUtil.getUploadPath("zxqrcode") + zxQrcodeWebsiteList.get(i).getQrcodeName()));
					ImageIO.write(bufferImg, "jpg", byteArrayOut);
				} catch (IOException e) {
					e.printStackTrace();
				}
				// anchor主要用于设置图片的属性
				XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) 3, i + j + 1, (short) 4, i + j + 4);
				anchor.setAnchorType(3);
				// 插入图片
				patriarch.createPicture(anchor,
						workbook.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG));
				j += 5;
			}
		}
		// // 下载到服务器返回访问路径
		// FileOutputStream out = null;
		// try {
		// out = new FileOutputStream(exprotExcelPath + exprotExcelName);
		// workbook.write(out);
		// out.flush();
		// } catch (FileNotFoundException e) {
		// e.printStackTrace();
		// } catch (IOException e) {
		// e.printStackTrace();
		// } finally {
		// try {
		// if (out != null) {
		// out.close();
		// }
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		// }
		// return repEntity.ok(HttpUtil.getExportTemplateWeb(request, "zzmap") +
		// exprotExcelName);

		// response.setContentType("application/force-download");// 设置强制下载不打开
		// response.addHeader("Content-Disposition", "attachment;fileName=" +
		// downName);// 设置文件名
		// response.setHeader("X-Accel-Charset", "utf-8");
		//
		// response.setHeader("Content-disposition", String.format("attachment;
		// filename=\"%s\"", formFileName));
		// response.setContentType("application/vnd.ms-excel;charset=utf-8");
		// response.setCharacterEncoding("UTF-8");

		// 设置response下载弹窗
		response.reset();
		// 加载xls文件
		// response.setContentType("application/vnd.ms-excel;charset=utf-8");
		// 加载xlsx文件
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
		// 没有后缀
		// response.setContentType("application/msexcel;charset=utf-8");
		// 后缀也可以在filename后面指定
		OutputStream out = null;
		try {
			// 跨域访问
			response.setHeader("Access-Control-Allow-Origin", "*");
			// 重命名
			response.setHeader("Content-Disposition",
					"attachment; filename=\"" + new String("二维码打印列表.xlsx".getBytes("utf-8"), "iso-8859-1") + "\"");
			out = response.getOutputStream();
			workbook.write(out);
			out.flush();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值