How i will print excel file in java

hi,

I have generated an excel file using "jexcelapi_2_5_9" tool. Now want to print this file from java.Now i cannot do it. How this possible? Is there anybody can help me? I have send my code :

package selec_image;

import jxl.Workbook;

import jxl.write.*;

import java.io.*;

publicclass Excel{

public Excel(String name){

try{

Workbook wb= Workbook.getWorkbook(new File("report.xls"));

WritableWorkbook copy=Workbook.createWorkbook(new File("Workbook.xls"),wb);

WritableSheet sheet = copy.getSheet(0);

Label label =new Label(0,1,"Bina");

sheet.addCell(label);

Label label1 =new Label(1,1,"187");

sheet.addCell(label1);

WritableImage image =new WritableImage(2,3,2,6,new File(name));

sheet.addImage(image);

copy.write();

copy.close();

}

catch(Exception ex){

ex.printStackTrace();

}

}

}

Now i want to print Workbook.xls. But i cannot do it. Plaese help me.

With regards

bina

[1863 byte] By [bina_jatnoa] at [2007-10-2 18:27:05]
# 1

The java print service has a pretty detailed manual that has source code examples: http://java.sun.com/j2se/1.4.2/pdf/JPS_PDF.pdf.

I think if you convert your excel file to a tab delimited byte stream, you could print it using DocFlavor.INPUT_STREAM.TEXT_PLAIN_ASCII. You might have to play around with the formatting if your excel sheet has too many columns.

Micchael222a at 2007-7-13 19:48:15 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hi,

I have used following code for printing an Excel file. But it shows error:

un.print.PrintJobFlavorException: invalid flavor

at sun.print.Win32PrintJob.print(Win32PrintJob.java:296)

at print_doc.Print_doc.<init>(Print_doc.java:28)

at print_doc.Print_doc.main(Print_doc.java:41)

package print_doc;

import javax.print.attribute.standard.*;

import javax.print.*;

import java.io.*;

import javax.print.attribute.*;

import javax.print.event.*;

public class Print_doc {

public Print_doc() {

DocFlavor format = DocFlavor.INPUT_STREAM.TEXT_PLAIN_US_ASCII;

//DocFlavor format = new DocFlavor("text/plain", "java.io.FileInputStream");

PrintRequestAttributeSet aset =new HashPrintRequestAttributeSet();

aset.add(MediaSizeName.ISO_A4);

aset.add(new Copies(1));

//aset.add(Sides.ONE_SIDED);

//aset.add(Finishings.NONE);

PrintService pservices=PrintServiceLookup.lookupDefaultPrintService();

System.out.println(pservices.getName());

DocPrintJob djob=pservices.createPrintJob();

try{

FileInputStream input =new FileInputStream("report.xls");

Doc doc = new SimpleDoc(input,format,null);

djob.print(doc,aset);

}

catch(FileNotFoundException exc){

exc.printStackTrace();

System.out.println("File not found");

}

catch(PrintException e){

e.printStackTrace();

System.out.println("Printer not found");

}

}

public static void main(String[] args) {

Print_doc ps = new Print_doc();

}

}

And My printer is network printer--\\Maos10\Lexmark X215. So what will i do?Please give me solution.

Thanks

Bina

bina_jatnoa at 2007-7-13 19:48:15 > top of Java-index,Desktop,Core GUI APIs...