Results 1 to 5 of 5
- 03-21-2011, 07:10 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
- 03-21-2011, 07:47 AM #2
Does this question have anything to do with Java?
db
- 03-21-2011, 08:50 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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.
- 03-21-2011, 02:18 PM #4
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: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.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(); } } }
db
- 03-22-2011, 02:32 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
printing string backwards and printing every other
By droidus in forum New To JavaReplies: 22Last Post: 03-10-2011, 09:17 AM -
opening an URL -
By Sparky in forum New To JavaReplies: 3Last Post: 02-07-2011, 03:58 PM -
[HELP]JButton opening a browser and opening a website[HELP]
By Learnin in forum AWT / SwingReplies: 4Last Post: 10-07-2009, 09:14 AM -
Excel sheet opening in read-only mode
By swati.jyoti in forum Advanced JavaReplies: 2Last Post: 09-08-2009, 02:08 PM -
Opening excel from struts
By rekha in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 05-13-2009, 06:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks