目录
1、设置二维码的数据,以及生成路径,然后调用工具类方法进行生成二维码
关于这个业务问题也是客户要求才做的,起初想着容易,但是做了之后发现还是有很多问题,一个是我的导出模板并不是自己画的,而是简单的按照模板导出,但要进行excel插入图片,两个之间有很大的一个区别,他们调用的对象以及方法的不同,使我走了很多弯路,网上大多都是自己画的模板很少有按照模板导出的案例,也有可能是太简单了吧,没人记录,至此我写这篇文章记录一下。
我的业务是在导出的时候再导出一张二维码,所以得有一个生成二维码的工具类。
一、先准备二维码
1、设置二维码的数据,以及生成路径,然后调用工具类方法进行生成二维码
File f = new File(System.getProperty("user.dir") + File.separator+ ConstantUtil.NAME + "/qrcode/"+sm.format(new Date()));
if(!f.exists()){
f.mkdirs();
}
//调拨单号表数据
TaskAllot task = taskAllotService.getById(ids);
String msg = task.getAllotNumber();
//二维码的保存路径
String path = System.getProperty("user.dir") + File.separator + ConstantUtil.NAME + "/qrcode/" + sm.format(new Date())
+ File.separator + RandomFileName.getRandomFileName() + ".jpg";
// 生成二维码返回路径
qrCodePath = BarcodeUtil.getQRCode(msg, path, 60);
2、二维码工具类
/**
* 生成条形码工具(一维码)
*
* @author tangzz
* @createDate 2015年9月17日
*
*/
public class BarcodeUtil {
private static String format = "png";
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
public static void main(String[] args) {
String msg = "BTAG13181912150450";
String path = "d:/code.png";
getQRCode(msg,path,200);
}
/**
* 生成二维码(QR类型)
*
* @param content
* 二维码文本内容
* @param file
* 生成的路径(文件路径)
* @return 返回文件路径加文件全名称
*/
public static String getQRCode(String content, String file,Integer num) {
try {
int width = 40;
int height = 10;
HashMap<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.PDF417_COMPACT, "utf-8");
// 二维码的格式是BarcodeFormat.QR_CODE qr类型二维码 BarcodeFormat.DATA_MATRIX dm码
BitMatrix qrc = new MultiFormatWriter().encode(content, BarcodeFormat.PDF_417, width, height, hints);
qrc = deleteWhite(qrc);
File out = new File(file);
// 生成二维码图片
writeBitMatricToFile(qrc, format, out);
System.out.println(out.getAbsolutePath());
return out.getAbsolutePath();
} catch (Exception e) {
System.out.println(e.getMessage());
return null;
}
}
/**
* 生成条形码
*
* @param content
* 二维码文本内容
* @param file
* 生成的路径(文件路径)
* @return 返回文件路径加文件全名称
*/
public static String getBarCode(String content, String file) {
try {
int width = 100;
int height = 31;
HashMap<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
// 条形码的格式是 BarcodeFormat.EAN_13
BitMatrix bc = new MultiFormatWriter().encode(content, BarcodeFormat.CODE_128, width, height, hints);
File out = new File(file);
// 输出图片
writeBitMatricToFile(bc, format, out);
// 记录日志
System.out.println(out.getAbsolutePath());
return out.getAbsolutePath();
} catch (Exception e) {
System.out.println(e.getMessage());
return null;
}
}
public static void writeBitMatricToFile(BitMatrix bm, String format, File file) {
BufferedImage image = toBufferedImage(bm);
try {
if (!ImageIO.write(image, format, file)) {
throw new RuntimeException("Can not write an image to file" + file);
}
} catch (IOException e) {
System.err.println(e);
System.err.println(e);
e.printStackTrace();
}
}
private static BufferedImage toBufferedImage(BitMatrix bm) {
int width = bm.getWidth();
int height = bm.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
image.setRGB(i, j, bm.get(i, j) ? BLACK : WHITE);
}
}
return image;
}
/**
* 删除白边
*/
private static BitMatrix deleteWhite(BitMatrix matrix) {
int[] rec = matrix.getEnclosingRectangle();
int resWidth = rec[2] + 1;
int resHeight = rec[3] + 1;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
for (int i = 0; i < resWidth; i++) {
for (int j = 0; j < resHeight; j++) {
if (matrix.get(i + rec[0], j + rec[1]))
resMatrix.set(i, j);
}
}
return resMatrix;
}
}
二、插入图片方法
1、插入二维码
public String getPictrue(String fileurl2){
FileOutputStream fileOut = null;
BufferedImage bufferImg = null;
try {
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
//加载图片,qrCodePath是图片的地址,通过这个地址拿到图片
bufferImg = ImageIO.read(new File(qrCodePath));
ImageIO.write(bufferImg, "jpg", byteArrayOut);
//fileurl是模板地址,要进行插入必须有模板和图片
FileInputStream fileInputStream = new FileInputStream(fileurl);
XSSFWorkbook xwb = new XSSFWorkbook(fileInputStream);
XSSFSheet xSheet = xwb.getSheetAt(0); //获取excel表的第一个sheet
XSSFDrawing patriarch = xSheet.createDrawingPatriarch();
XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0,(short) 10, 0, (short) 14, 4);
//插入图片
patriarch.createPicture(anchor, xwb.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG));
OutputStream out = new FileOutputStream(fileurl2);
xwb.write(out);
out.flush();
out.close();
fileInputStream.close();
// 输出文件
// xwb.write(fileOut);
} catch (Exception e) {
e.printStackTrace();
}
返回一个地址
return fileurl2;
}
2、设置图片的位置
XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0,(short) 10, 0, (short) 14, 4);
参数设置以及导出效果如图所示 :