• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic