Java写Excel(不生成实体文件,写为流的形式)
java 写 Excel(不生成实体文件,写为流的形式)
public String exportReportExcel(String mediaCode, List<SimpleMediaResourceInfo> mediaResourceInfos) {
String scheduleURL = "";
// 1. 根据不同的媒介code查询对应的媒介细节内容
List<MediaContent> mediaContents = mediaContentMapper.selectByMediaCode(mediaCode);
// 2. 组装header
List<String> medias = new ArrayList<String>();
for (MediaContent mediaContent : mediaContents) {
medias.add(mediaContent.getAttrName());
}
String[] headers = new String[medias.size()];
headers = medias.toArray(headers);
// 3. 将内容写入对应的Excel
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
Workbook workbook = new XSSFWorkbook();
// 生成一个表格
Sheet sheet = workbook.createSheet();
sheet.setDefaultColumnWidth(13); // 调整列宽度
writeHeader(headers, workbook, sheet);
writeData(mediaResourceInfos, medias, sheet);// 遍历集合数据,产生数据行
workbook.write(os);
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
scheduleURL = JdStorageService.getInstance().upload(is, (int) os.size(), "export.xls");
os.flush();
os.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return scheduleURL;
}
private void writeHeader(String[] headers, Workbook workbook, Sheet sheet) {
CellStyle style = workbook.createCellStyle();
XSSFFont font = (XSSFFont) workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 粗体显示
style.setFont(font);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中
// style.setFillForegroundColor(IndexedColors.TEAL.getIndex());
// //设置背景色
// style.setFillPattern(CellStyle.SOLID_FOREGROUND);
Row row0 = sheet.createRow(0);
row0.setHeightInPoints(18);
Cell showTimeTitle = row0.createCell((short) 0);
showTimeTitle.setCellValue("投放时间段(必填)");
Cell showTime = row0.createCell((short) 1);
showTime.setCellValue(" ");// TODO 时间问题
Cell remark = row0.createCell((short) 2);
remark.setCellValue("整个媒介投放的时间区间,按照从投放第一天到投放最后一天,如中间有间隔,需要用\",\"分开填写");
// 产生表格标题行
Row row = sheet.createRow(1);
row.setHeightInPoints(21);
for (int i = 0; i < headers.length; i++) {
Cell cell = row.createCell((short) i);
RichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text.toString());
cell.setCellStyle(style);
}
}
private void writeData(List<SimpleMediaResourceInfo> mediaResourceInfos, List<String> medias, Sheet sheet) {
Row row;
if (!CollectionUtils.isEmpty(mediaResourceInfos)) {
int index = 2;
for (SimpleMediaResourceInfo rowData : mediaResourceInfos) {
row = sheet.createRow(index);
row.setHeightInPoints(21);
int cellIndex = 0;
if (!CollectionUtils.isEmpty(rowData.getMediaResourceTypes())
|| !CollectionUtils.isEmpty(rowData.getDetailContents())) {
for (String header : medias) {
for (SimpleMediaResourceType columnInfo : rowData.getMediaResourceTypes()) {
if (header.equals(columnInfo.getAttrName())) {
Cell cell = row.createCell((short) cellIndex);
cell.setCellValue(columnInfo.getAttrValue().toString());
}
}
for (SimpleMediaDetailContentItem columnInfo : rowData.getDetailContents()) {
if (header.equals(columnInfo.getAttrName())) {
Cell cell = row.createCell((short) cellIndex);
cell.setCellValue(columnInfo.getAttrValue().toString());
}
}
if (header.equals("时段")) {
Cell cell = row.createCell((short) cellIndex);
cell.setCellValue(ShowTimeHelper.transferFrom(rowData.getShowTimes()).toString());
}
cellIndex++;
}
index++;
}
}
}
}