【问题标题】:Add color in a automatic generate Excel file在自动生成的 Excel 文件中添加颜色
【发布时间】:2019-10-03 10:32:07
【问题描述】:

我有一个关于在 Excel 文件中添加颜色的问题。 我想知道是否可以使用方法 ByteArrayOutputStream 来做到这一点。

我已经搜索过,但没有找到任何东西。

/**
 * Export des incidents Jira
 * 
 * @return
 * @throws Exception
 */
@GET
@Path("exportJiraIncidents")
@Produces(MediaType.TEXT_PLAIN)
public Response exportJiraIncidents() throws Exception {

    // Get data base data
    final List<IncidentVo> data = 
jiraGroupamaResource.getJiraIncidents();

    // csv export
    final ByteArrayOutputStream outStream = new 
ByteArrayOutputStream();

    String line = "IssueId;Incident;Activité;DateCreation;Statut;"
+"RefIncJira;Intervenant;Titre;Gravité;resolution;" 
+ "updated;duedate;dateResolution;timeOriginalEstimate;timeEstimate;"
+ "timeSpent;workflow_id\r\n";

outStream.write(line.getBytes(Charset.forName("cp1252")));

    for (final IncidentVo vo : data) {

        line = addChampCsv(vo.getIssueId()) + 
addChampCsv(vo.getIncident()) + addChampCsv(vo.getActivite()) + 
addChampCsv(vo.getDateCreation())
                + addChampCsv(vo.getStatut()) + 
addChampCsv(vo.getRefIncJira()) + addChampCsv(vo.getIntervenant()) + 
addChampCsv(vo.getTitre())
                + addChampCsv(vo.getGravite()) + 
addChampCsv(vo.getResolution()) + addChampCsv(vo.getUpdated()) + 
addChampCsv(vo.getDueDate())
                + addChampCsv(vo.getDateResolution()) 
+ addChampCsv(vo.getTimeOriginalEstimate()) + 
addChampCsv(vo.getTimeEstimate())
                + addChampCsv(vo.getTimeSpent()) + 
addChampCsv(vo.getWorkflowId()) + "\r\n";

outStream.write(line.getBytes(Charset.forName("cp1252")));
    }

    return Response.ok(new 
ByteArrayInputStream(outStream.toByteArray())).header(STR_TYPE_HEADER, "attachment; filename=incidents.csv")
            .type(STR_TYPE_CSV).build();
}

'string line =' 是文件的标题,循环中的所有 get 都将信息放入 Excel 文件中。 我只是不明白如何插入颜色。

有什么想法吗?

【问题讨论】:

  • 我会利用Apache POI 使用Java 生成一个excel 文件

标签: java excel colors bytearrayoutputstream


【解决方案1】:

要从 Java 进行更高级的 Excel 工作表修改,您可能必须使用 JXL, the Java Excel API

在那里,您可以很容易地进行着色,例如

Workbook.createWorkbook(outputStream);
workbook.createSheet(sheetName, 0);

WritableCell cell = sheet.getWritableCell(0,0);

WritableCellFormat coloredCellFormat = new WritableCellFormat();

coloredCellFormat.setBackground(Colour.RED);

cell.setCellFormat(newFormat);

workbook.write(); // important to use
workbook.close();

正如您在此示例中看到的,JXL 允许您写入任何OutputStream。因此,您应该能够在您的网络服务中使用它。

【讨论】:

  • 感谢您的回答。不幸的是,我知道这一点,我需要像在帖子中那样使用...
猜你喜欢
  • 1970-01-01
  • 2015-05-06
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 2021-04-27
  • 2014-03-20
  • 2012-11-04
  • 1970-01-01
相关资源
最近更新 更多