内容文本生成二维码用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;
}
}