我得业务角度是 生成对应的二维码,导出这个二维码关联的数据以及这一个二维码.
@ApiOperation(value = "测试", produces = MediaType
.APPLICATION_OCTET_STREAM_VALUE)
@GetMapping("test")
public void test(HttpServletRequest request, HttpServletResponse response) throws IOException, WriterException {
String fileName = "测试.jpg";//图片名称
JSONObject jsonObject = new JSONObject();
jsonObject.put("methodCode", "1");
jsonObject.put("data", ""123456789"");
String content = jsonObject.toString();//内容
int height = 200;
int width = 200;
String format = "jpg";//格式
Map<EncodeHintType,Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");//设置编码格式
hints.put(EncodeHintType.MARGIN,1);
BitMatrix bitMatrix = new MultiFormatWriter()
.encode(content, BarcodeFormat.QR_CODE,width,height,hints);
Path path = FileSystems.getDefault().getPath("D://", fileName);
MatrixToImageWriter.writeToPath(bitMatrix,format,path);//输出图片
UserAddressExample userAddressExample = new UserAddressExample();
userAddressExample.or().andMeasAddrIdEqualTo(measAddrId).andStateEqualTo(1);
//数据
List<Object> list= XXXMapper.selectByExample(XXXExample);
try {
//设置表头
String[] title = {"姓名", "工号"};
// 创建图片页
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("测试");
Row row;
Cell cell;
// 输出表头
row = sheet.createRow(0);
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
}
for (int i = 0; i < list.size(); i++) {
HSSFRow row1 = sheet.createRow(i+1);
HSSFCell cell1 = row1.createCell(0);
cell1.setCellValue(list.get(i).getUsername());
HSSFCell cell2 = row1.createCell(1);
cell2.setCellValue(list.get(i).getNo());
}
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
// 将图片写入流中
BufferedImage bufferImg = ImageIO.read(new File(String.valueOf(path)));
ImageIO.write(bufferImg, "jpg", outStream);
// 利用HSSFPatriarch将图片写入EXCEL
HSSFPatriarch patri = sheet.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,
(short) 0, userAddresses.size()+2, (short) (title.length), userAddresses.size()+(title.length*4)+1);
patri.createPicture(anchor, workbook.addPicture(
outStream.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
sendResponseHeader(response,"测试.xls");
OutputStream os = response.getOutputStream();
workbook.write(os);
os.flush();
os.close();
return;
} catch (Exception e) {
final String message = e.toString();
e.printStackTrace();
}
}
//发送响应流方法
private void sendResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes("gbk"),"ISO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("application/octet-stream");
//response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}