|
|
@@ -0,0 +1,123 @@
|
|
|
+package com.tld.excel;
|
|
|
+
|
|
|
+import cn.afterturn.easypoi.excel.ExcelExportUtil;
|
|
|
+import cn.afterturn.easypoi.excel.entity.ExportParams;
|
|
|
+import com.tld.model.Print;
|
|
|
+import com.tld.model.PrintModel;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
+import org.iherus.codegen.qrcode.QrcodeGenerator;
|
|
|
+import org.iherus.codegen.qrcode.SimpleQrcodeGenerator;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.BufferedOutputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * excel工具类
|
|
|
+ */
|
|
|
+public class ExcelUtil {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出excel
|
|
|
+ *
|
|
|
+ * @param response HttpServletResponse
|
|
|
+ * @param fileName 文件名字
|
|
|
+ * @param workbook 通过exportPicture()方法获取
|
|
|
+ * @author zhy
|
|
|
+ */
|
|
|
+ public static void writeExcel(HttpServletResponse response, String fileName, Workbook workbook) {
|
|
|
+ // 判断数据
|
|
|
+ if (workbook == null) {
|
|
|
+ System.out.println("错误!");
|
|
|
+ }
|
|
|
+ // 重置响应对象
|
|
|
+ response.reset();
|
|
|
+ try {
|
|
|
+ OutputStream outputStream = getOutputStream(fileName, response);
|
|
|
+ BufferedOutputStream bufferedOutPut = new BufferedOutputStream(outputStream);
|
|
|
+ workbook.write(bufferedOutPut);
|
|
|
+ bufferedOutPut.flush();
|
|
|
+ bufferedOutPut.close();
|
|
|
+ outputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出文件时为Writer生成OutputStream
|
|
|
+ *
|
|
|
+ * @param fileName 文件名字
|
|
|
+ * @param response response
|
|
|
+ * @return 输出流
|
|
|
+ * @author zhy
|
|
|
+ */
|
|
|
+ private static OutputStream getOutputStream(String fileName, HttpServletResponse response) {
|
|
|
+ try {
|
|
|
+ fileName = URLEncoder.encode(fileName, "UTF-8");
|
|
|
+ response.setContentType("application/vnd.ms-excel");
|
|
|
+ response.setCharacterEncoding("utf8");
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
|
|
|
+ response.setHeader("Pragma", "public");
|
|
|
+ response.setHeader("Cache-Control", "no-store");
|
|
|
+ response.addHeader("Cache-Control", "max-age=0");
|
|
|
+
|
|
|
+ response.setHeader("Content-disposition", "attachment;filename=" + fileName+ ".xls;filename*=utf-8''"+fileName+".xls"); //兼容不同浏览器的中文乱码问题
|
|
|
+ response.setContentType("application/msexcel");// 定义输出类型
|
|
|
+ response.setCharacterEncoding("UTF-8");//
|
|
|
+
|
|
|
+ return response.getOutputStream();
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.out.println("导出excel表格失败!");
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取Workbook对象
|
|
|
+ *
|
|
|
+ * @param pigList excel中添加的数据
|
|
|
+ * @return 文件下载
|
|
|
+ */
|
|
|
+ public static Workbook exportPicture(List<Print> pigList) {
|
|
|
+ List<PrintModel> list = new ArrayList<>();
|
|
|
+ pigList.forEach(item -> {
|
|
|
+ PrintModel e = new PrintModel();
|
|
|
+ e.setEarTag(item.getEarTag());
|
|
|
+ String s = item.getEarTag();
|
|
|
+
|
|
|
+ QrcodeGenerator generate = new SimpleQrcodeGenerator().generate( s);
|
|
|
+ BufferedImage image = generate.getImage();
|
|
|
+ byte[] pngs = imageToBytes(image);
|
|
|
+ e.setQrCode(pngs);
|
|
|
+ list.add(e);
|
|
|
+ });
|
|
|
+ return ExcelExportUtil.exportExcel(new ExportParams(), PrintModel.class, list);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * BufferedImage转byte[]
|
|
|
+ *
|
|
|
+ * @param bImage BufferedImage对象
|
|
|
+ * @return byte[]
|
|
|
+ */
|
|
|
+ private static byte[] imageToBytes(BufferedImage bImage) {
|
|
|
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
+ try {
|
|
|
+ ImageIO.write(bImage, "png", out);
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ }
|
|
|
+ return out.toByteArray();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|