File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes exporting negative numbers in red colour to an xls file from java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "exporting negative numbers in red colour to an xls file from java" Watch "exporting negative numbers in red colour to an xls file from java" New topic

exporting negative numbers in red colour to an xls file from java

maria stuart

Joined: Oct 13, 2005
Posts: 1
Hello everyone,
I want some data to come up in an Excel file from a servlet.The data includes +ve & -ve numbers.While exporting from servlet i need to set the -ve numbers in red colour.I heard its possible. Anybody plz help me ...
i am not using any java-excel apis.
hre i am attaching the code also....

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

BufferedInputStream fileInputStream = null;
ServletOutputStream out = null;
// Read a file from the file system and stream it to the browser.
try {
out = resp.getOutputStream ();
}catch(IOException e) {}

// Set the output data's mime type based on the document's extension.
//resp.setContentType( "application/x-msexcel" ); // MIME type for EXCEL doc
//resp.setContentType( "application/");

try {
// Use Buffered Stream for reading/writing.
BufferedInputStream bis = null;
BufferedOutputStream bos = null;

// Read in a request parameter to determine if the file is on the file system
// or created on the fly.
String fileName = req.getParameter("file");
if (fileName.equals("xls") {

//get formatted output from session and then clear it from session
String outputDocument = (String) req.getSession().getAttribute("exportString");

String exportFileName = null;

if (fileName.equals("xls")) {
resp.setContentType( "application/");
exportFileName = "export.xls";
resp.setHeader("Content-disposition","attachment; " + // For launching in a separate window."filename=" +exportFileName);
bis = new BufferedInputStream(new ByteArrayInputStream(outputDocument.getBytes()));

bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;

// Simple read/write loop.
while(-1 != (bytesRead =, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
System.out.println("Writing byte by byte");

} catch(Exception e) {}
finally {
// Close the input stream.
if (fileInputStream != null)
try {
catch (IOException e) {/* Can't do much. */e.printStackTrac(); }

WHOLE DATA IS THR IN "outputdocument".It is an arraylist..It contains -ve numbers also..I need to display tht numbers in red colour in the xls file

Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3640
I don’t know by you way it is possible or not.

But I have achieved same using POI-HSSF - Java API To Access Microsoft Excel Format Files.

[ October 13, 2005: Message edited by: Chetan Parekh ]
[ October 13, 2005: Message edited by: Chetan Parekh ]

My blood is tested +ve for Java.
Andreas Ecker

Joined: Jun 13, 2002
Posts: 4
What kind of data is your "outputdocument"? Ist it CSV?
I think your writing a simple text file, not a generic Excel file. So it might be a hint to go the opposite way: Save a Excel table as some sort of text file and look, if it has got some information about colouring in it.
A solution might be to use HTML format.

Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

CSV-files are allways plain content, without font-type or -color-definitions.

Perhaps you may create an indirect-sheet document, which defines formats like negative numbers in red, and take the data-source from your csv-file?
I agree. Here's the link:
subject: exporting negative numbers in red colour to an xls file from java
It's not a secret anymore!