0

I have a question about adding color in a Excel file. I wonder if it's possible to do this with the methode 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. I just don't get how to insert color.

Any idea ?

1
  • I would make use of Apache POI to generate an excel file using Java
    – Logan Wlv
    May 16, 2019 at 8:10

1 Answer 1

0

For more advanced Excel sheet modifications from Java, you will probably have to use JXL, the Java Excel API

There, you can quite easily do coloring, e.g.

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. Therefore, you should be able to use it in your webservice.

1
  • Thanks for your answer. I know about this infortunatly, I need to use like I did in the post...
    – ISSOU
    May 16, 2019 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.