1. 常用的POI格式设置以及使用POI生成EXCEL文件并转为MultipartFile类型上传到服务器
Integer dataCount = purGoodsModelDtoMap.size() + 12;
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("表格");
Integer width = 1820;
sheet.setColumnWidth(0, 600);
sheet.setColumnWidth(1, width);
PrintSetup ps = sheet.getPrintSetup();
ps.setPaperSize(PrintSetup.A4_PAPERSIZE);
sheet.setAutobreaks(true);
POIUtil.createAllRow(sheet, 0, dataCount, false);
int rowIndex = 0;
int colIndex = 0;
HSSFRow row = sheet.getRow(rowIndex);
row.setHeight((short) 700);
HSSFCell cell = row.createCell(colIndex + 1);
HSSFCellStyle cellStyle = workbook.createCellStyle();
Font baseFont = workbook.createFont();
baseFont.setFontName("华文中宋");
baseFont.setFontHeightInPoints((short)20);
baseFont.setBold(true);
cellStyle.setFont(baseFont);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setWrapText(true);
cell.setCellStyle(cellStyle);
POIUtil.setCellValue("aa\r\nbb", cell);
sheet.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex + 2, colIndex + 1, colIndex + 6));
HSSFCellStyle cellStyleCommon = workbook.createCellStyle();
HSSFCellStyle cellStyleCommonRight = workbook.createCellStyle();
Font baseFontCommon = workbook.createFont();
baseFontCommon.setFontName("华文宋体");
baseFontCommon.setFontHeightInPoints((short)11);
baseFontCommon.setBold(false);
cellStyleCommon.setFont(baseFontCommon);
cellStyleCommon.setAlignment(HorizontalAlignment.LEFT);
cellStyleCommonRight.setFont(baseFontCommon);
cellStyleCommonRight.setAlignment(HorizontalAlignment.RIGHT);
HSSFCellStyle cellStyleTable = workbook.createCellStyle();
Font tableFontCommon = workbook.createFont();
tableFontCommon.setFontName("华文宋体");
tableFontCommon.setFontHeightInPoints((short)12);
tableFontCommon.setBold(false);
cellStyleTable.setFont(tableFontCommon);
cellStyleTable.setAlignment(HorizontalAlignment.LEFT);
cellStyleTable.setBorderBottom(BorderStyle.MEDIUM);
cellStyleTable.setBorderLeft(BorderStyle.MEDIUM);
cellStyleTable.setBorderTop(BorderStyle.MEDIUM);
cellStyleTable.setBorderRight(BorderStyle.MEDIUM);
String fileName = storeOut.getStoreId().toString() + ".xls";
try {
uploadFile(fileName, workbook, storeOut);
} catch (IOException e) {
log.info("上传附件失败 ->{}",e);
}
private void uploadFile(String fileName, HSSFWorkbook workbook, PurStoreOut storeOut) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
FileItemFactory factory = new DiskFileItemFactory(16, null);
FileItem fileItem = factory.createItem("textField", "text/plain", true, fileName);
OutputStream os = fileItem.getOutputStream();
workbook.write(os);
os.close();
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
HttpEntity reqEntity =
MultipartEntityBuilder.create().addPart("token", new StringBody(fileUploadToken, ContentType.TEXT_PLAIN))
.addPart("resize", new StringBody("false", ContentType.TEXT_PLAIN))
.addPart("file", new ByteArrayBody(multipartFile.getBytes(), fileName)).build();
HttpPost post = new HttpPost(fileUploadUrl + "/file_upload");
post.setEntity(reqEntity);
post.setConfig(RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(30000).build());
HttpResponse response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
post.releaseConnection();
log.info("文件上传结果={}", result);
JSONObject res = JSON.parseObject(result);
FileServiceResp uploadResp = JSONObject.toJavaObject(res, FileServiceResp.class);
if (res.getInteger("code") == 1) {
log.info("上传成功, url->{}", uploadResp.getUrl());
}
} catch (Exception e) {
log.info("文件生成异常->{}",e);
} finally {
httpClient.close();
}
}