Results 1 to 5 of 5
  1. #1
    bihuboliya is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default Printing excel document without opening

    hi all,

    I have generated an excel document which I need to print from the UI using a button without opening the file. Could anyone help me in this? thanks in advance.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default

    Does this question have anything to do with Java?

    db

  3. #3
    bihuboliya is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Hello, thanks for replying.

    Well, I have written a program in Java that would generate an excel sheet based on the contents in an xml document. I parsed the xml file using JDOM and generated the excel using POI. In the UI, I have controls for browsing the source xml file and choosing the destination folder, some modes of generation comboboxes, some buttons like 'clear', 'generate', 'generate and print' etc. All the components are swing components. I want the print feature in the 'generate and print' button. I require help in sending the generated excel document to the printer in the actionPerformed method of 'generate and print' button.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default

    I can see three approaches:

    1. Obtain a commercial package, there are several that support Excel printing from Java code. Search the net.

    2. Use JNA to manipulate an Excel instance.

    3. Easiest (IMO) but may have some 'gotchas' -- write out a VBScript file and execute it. Here's an abbreviated example:
    Java Code:
    import java.io.File;
    import java.io.FileWriter;
    import javax.swing.JFileChooser;
    import javax.swing.filechooser.FileNameExtensionFilter;
    
    public class PrintExcel {
    
      public static void main(String[] args) {
        new PrintExcel().init();
      }
    
      public void init() {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileFilter(new FileNameExtensionFilter("Excel", "xls"));
        int retVal = chooser.showOpenDialog(null);
        if (retVal == JFileChooser.APPROVE_OPTION) {
          printFile(chooser.getSelectedFile());
        }
      }
    
      private void printFile(File file) {
        try {
          String vbs = "Dim AppExcel\r\n"
                  + "Set AppExcel = CreateObject(\"Excel.application\")\r\n"
                  + "AppExcel.Workbooks.Open(\"" + file.getPath() + "\")\r\n"
                  + "appExcel.ActiveWindow.SelectedSheets.PrintOut\r\n"
                  + "Appexcel.Quit\r\n"
                  + "Set appExcel = Nothing";
          File vbScript = File.createTempFile("vbScript", ".vbs");
          vbScript.deleteOnExit();
          FileWriter fw = new java.io.FileWriter(vbScript);
          fw.write(vbs);
          fw.close();
          Process p = Runtime.getRuntime().exec("cscript //NoLogo " + vbScript.getPath());
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
    Before using anything like this, be sure to go through all four sections of When Runtime.exec() won't - JavaWorld amd implement ALL the recommendations. Also note that the FileWriter should be closed in a finally block.

    db

  5. #5
    bihuboliya is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Wow... did not think of this approach :) Thats solves my problem... Thanks a dozen Darryl... :)

Similar Threads

  1. printing string backwards and printing every other
    By droidus in forum New To Java
    Replies: 22
    Last Post: 03-10-2011, 09:17 AM
  2. opening an URL -
    By Sparky in forum New To Java
    Replies: 3
    Last Post: 02-07-2011, 03:58 PM
  3. Replies: 4
    Last Post: 10-07-2009, 09:14 AM
  4. Excel sheet opening in read-only mode
    By swati.jyoti in forum Advanced Java
    Replies: 2
    Last Post: 09-08-2009, 02:08 PM
  5. Opening excel from struts
    By rekha in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 05-13-2009, 06:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •