内容文本生成二维码用excel表格导出(java)

//若有问题可留言
效果如下:
在这里插入图片描述

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;

public class ExportQr {
	public static void main(String ags[]) {
		String excelName = "D://QrExcel.xls";
		HSSFWorkbook workBook = new HSSFWorkbook();
		HSSFSheet sheet = workBook.createSheet();
		sheet.setColumnWidth(2, 4800);

		CellStyle headerStyle = workBook.createCellStyle();
		headerStyle.setAlignment(CellStyle.ALIGN_CENTER);
		headerStyle.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
		headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		Font headerFont = workBook.createFont();
		headerFont.setFontName("Arial");
		headerFont.setFontHeightInPoints((short) 10);
		headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
		headerFont.setColor(IndexedColors.WHITE.getIndex());
		headerStyle.setFont(headerFont);

		HSSFRow titleRow = sheet.createRow(0);
		setCell(titleRow.createCell(0), headerStyle, "内容");
		setCell(titleRow.createCell(1), headerStyle, "二维码");
		HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
		List<String> contentList = Arrays.asList("大河向东流啊", "天上的星星参北斗呀", "嘿嘿嘿嘿呦....");
		try {
			for (int index = 0; index < contentList.size(); index++) {
				String content = contentList.get(index);
				HSSFRow contentRowNext = sheet.createRow(index + 1);
				contentRowNext.createCell(0).setCellValue(content);

				ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
				Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
				hints.put(EncodeHintType.MARGIN, 0);
				hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
				MatrixToImageWriter.writeToStream(
						new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300, hints), "PNG",
						outputStream);
				contentRowNext.setHeight((short) 1000);

				HSSFClientAnchor anchor = new HSSFClientAnchor(20, 10, 1003, 245, (short) 1, index + 1, (short) 1,
						index + 1);
				patriarch.createPicture(anchor,
						workBook.addPicture(outputStream.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG));

			}

			FileOutputStream outputStream = new FileOutputStream(excelName);
			workBook.write(outputStream);
			outputStream.close();
			System.out.println("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void setCell(HSSFCell hSSFCell, CellStyle style, String content) {
		hSSFCell.setCellStyle(style);
		hSSFCell.setCellValue(content);
		return;
	}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值