Abstract
This time to share with you is how to import excel data, some fields to generate base64 – bit TWO-DIMENSIONAL code.
rendering
The background and development
1. Introduction of POM.xml related JARS
2, code,
@PostMapping("/importData")
@ResponseBody
public AjaxResult importData(MultipartFile file, boolean updateSupport, String code) throws Exception
{
System.out.println(code);
ExcelUtil<SqJcitem> util = new ExcelUtil<SqJcitem>(SqJcitem.class);
List<SqJcitem> userList = util.importExcel(file.getInputStream());
Date date= new Date(a); SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
String time=dateFormat.format(date);
System.out.println("-"+time);
for(int i=0; i<userList.size(); i++) {if(StringUtils.isNotBlank(userList.get(i).getCode()))
{
SqJcitem n=userList.get(i);
List<SqJcitem> sqjcitemList= sqjcitemDao.listByCode(n.getCode());
if(sqjcitemList.size()==0)
{
SqJcitem sqjcitem=new SqJcitem();
sqjcitem.setCode(code+"-"+n.getCode()+"-"+n.getJcode()+"-"+time);
sqjcitem.setItems(n.getItems());
sqjcitem.setItemsname(n.getItemsname());
sqjcitem.setMethodcode(n.getMethodcode());
sqjcitem.setMethondname(n.getMethondname());
sqjcitem.setFxitems(n.getFxitems());
sqjcitem.setImg(QrCodeUtils.creatRrCode(sqjcitem.getCode(), 200.200));// Generate a base64 QR codesqjcitemDao.insert(sqjcitem); }}}return AjaxResult.success("");
}
Copy the code
Utility class QrCodeUtils
package com.stylefeng.guns.modular.util;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Hashtable;
public class QrCodeUtils {
public static String creatRrCode(String contents, int width, int height) {
String binary = null;
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(
contents, BarcodeFormat.QR_CODE, width, height, hints);
// 1, read the file into a byte array
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedImage image = toBufferedImage(bitMatrix);
// Convert IO streams to PNG format
ImageIO.write(image, "png", out);
byte[] bytes = out.toByteArray();
// 2. Convert the byte array to binary
BASE64Encoder encoder = new BASE64Encoder();
binary = encoder.encodeBuffer(bytes).trim();
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return binary;
}
/** * image **@author ianly* /
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); }}return image;
}
public static void main(String[] args) {
String binary = QrCodeUtils.creatRrCode("https://blog.csdn.net/ianly123".200.200); System.out.println(binary); }}Copy the code