简体   繁体   English

在自动生成Excel文件中添加颜色

[英]Add color in a automatic generate Excel file

I have a question about adding color in a Excel file. 我有一个关于在Excel文件中添加颜色的问题。 I wonder if it's possible to do this with the methode ByteArrayOutputStream. 我想知道是否可以使用方法ByteArrayOutputStream执行此操作。

I've search but I didn't find anything. 我搜索但我没有找到任何东西。

/**
 * 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();
}

The 'string line = ' is the heading of the file and all the get in the loop put the info in the Excel file. 'string line ='是文件的标题,循环中的所有get都将信息放入Excel文件中。 I just don't get how to insert color. 我只是不知道如何插入颜色。

Any idea ? 任何想法 ?

For more advanced Excel sheet modifications from Java, you will probably have to use JXL, the Java Excel API 对于Java中更高级的Excel工作表修改,您可能必须使用JXL,即Java Excel API

There, you can quite easily do coloring, eg 在那里,你可以很容易地做着色,例如

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();

As you see in this example, JXL lets you write to any OutputStream . 正如您在此示例中看到的那样,JXL允许您写入任何OutputStream Therefore, you should be able to use it in your webservice. 因此,您应该能够在您的Web服务中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM