springboot业务功能实战(十四)poi导出并以流方式保存excel
【摘要】
public class BarcodeExportlFlow implements IBarcodeExport { @Autowired BarcodeManageBatchSerivce barcodeManageBatchSerivce; @Override public OutputStream e...
-
public class BarcodeExportlFlow implements IBarcodeExport {
-
-
@Autowired
-
BarcodeManageBatchSerivce barcodeManageBatchSerivce;
-
-
@Override
-
public OutputStream exportData() throws IOException {
-
-
//查询批次码
-
BarcodeBatchManageBo input = new BarcodeBatchManageBo();
-
List<BarcodeBatchManageBo> middleList = barcodeManageBatchSerivce.selectBatch(input);
-
String[] headers = {"id", "条码批次码", "激活状态", "有效状态", "导入人", "导入时间", "激活人", "激活时间", "作废人", "作废时间", "本批次条数"};
-
HSSFWorkbook workbook = new HSSFWorkbook();
-
HSSFSheet sheet = workbook.createSheet();
-
//设置列宽
-
sheet.setDefaultColumnWidth((short) 18);
-
HSSFRow row = sheet.createRow(0);
-
for (short i = 0; i < headers.length; i++) {
-
//创建单元格,每行多少数据就创建多少个单元格
-
HSSFCell cell = row.createCell(i);
-
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
-
//给单元格设置内容
-
cell.setCellValue(text);
-
}
-
-
for (int j = 0; j < middleList.size(); j++) {
-
BarcodeBatchManageBo export = middleList.get(j);
-
//从第二行开始填充数据
-
row = sheet.createRow(j + 1);
-
List<String> datas = new ArrayList<>();
-
String id = export.getId().toString();
-
String batchCode = export.getBatchCode();
-
String activationStatus = export.getActivationStatus();
-
String effectiveStatus = export.getEffectiveStatus();
-
datas.add(id);
-
datas.add(batchCode);
-
datas.add(activationStatus);
-
datas.add(effectiveStatus);
-
for (int k = 0; k < datas.size(); k++) {
-
String string = datas.get(k);
-
HSSFCell cell = row.createCell(k);
-
HSSFRichTextString richString = new HSSFRichTextString(string);
-
HSSFFont font3 = workbook.createFont();
-
//定义Excel数据颜色,这里设置为蓝色
-
font3.setColor(HSSFColor.BLUE.index);
-
richString.applyFont(font3);
-
cell.setCellValue(richString);
-
}
-
}
-
FileOutputStream fos = new FileOutputStream("D:/wb.xls");
-
workbook.write(fos);
-
fos.close();
-
return null;
-
}
-
}
文章来源: baocl.blog.csdn.net,作者:小黄鸡1992,版权归原作者所有,如需转载,请联系作者。
原文链接:baocl.blog.csdn.net/article/details/83541421
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)