1、思路
第一步生成圖片字節(jié)數(shù)組輸出流
第二步字節(jié)數(shù)組輸出流存入excel
2、詳細過程
1.引入依賴
使用的是easyexcel和hutool工具便捷快速開發(fā)
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.15</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.1</version>
</dependency>
<!--阿里巴巴easyexcel工具-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel-core</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.3.2</version>
</dependency>
2.導出數(shù)據(jù)
導出類信息@ContentRowHeight(100) @ColumnWidth(100/6)
這兩個注解是條件excel行列大小,可以自行調整
@Getter
@Setter
@EqualsAndHashCode
@ContentRowHeight(100)
@ColumnWidth(100/6)
public class ImageDemoData{
private byte[] byteArray;
private String shortDesc;
/**
* 根據(jù)文件導出 并設置導出的位置。
*/
@ExcelIgnore
private WriteCellData<Void> writeCellDataFile;
}
EasyExcel會根據(jù)這個實體類來生成表格,可以去官網(wǎng)查看注解注釋掉其中的某個屬性,每一個屬性代表每列的信息。文章來源:http://www.zghlxwxcb.cn/news/detail-702283.html
@Test
public void imageWrite() throws Exception {
String fileName = TestFileUtil.getPath() + "imageWrite" + System.currentTimeMillis() + ".xlsx";
List<ImageDemoData> list = ListUtils.newArrayList();
ImageDemoData imageDemoData = new ImageDemoData();
list.add(imageDemoData);
// 創(chuàng)建字節(jié)數(shù)組輸出流
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// 調用 QrCodeUtil.generate() 生成二維碼,并將結果寫入輸出流
QrCodeUtil.generate("helloworld", 300, 300, ImgUtil.IMAGE_TYPE_PNG, outputStream);
// 從輸出流中獲取生成的二維碼圖像數(shù)據(jù)
byte[] qrCodeData = outputStream.toByteArray();
imageDemoData.setByteArray(qrCodeData);
imageDemoData.setShortDesc("helloworld");
// 關閉輸出流
outputStream.close();
// 寫入數(shù)據(jù)
EasyExcel.write(fileName, ImageDemoData.class).sheet().doWrite(list);
}
利用 hutools 工具簡化開發(fā),生成字符串對應的字節(jié)數(shù)組輸出流,傳入easyexcel完成二維碼的生成。文章來源地址http://www.zghlxwxcb.cn/news/detail-702283.html
到了這里,關于Java便捷生成二維碼并使用Excel的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!