Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
maria stuart
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/vnd.ms-excel");

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/vnd.ms-excel");
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 = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
System.out.println("Writing byte by byte");
bos.flush();
}

} catch(Exception e) {}
finally {
// Close the input stream.
if (fileInputStream != null)
try {
fileInputStream.close();
}
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

HERE EXPORTING PART IS WORKING FINE.BUT I DON'T KNOW HOW TO SET THE COLOUR.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Andreas Ecker
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Andreas
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic