How to maximize the opened Excel file using java? - java

There is an already opened EXcel file, but is minimized. I want to maximize that Excel sheet. I have been using open() method of Desktop Class, but sheet is not getting maximized.

It opens file in maximized mode by default XLS reading software (office).
Desktop dt = Desktop.getDesktop();
try {
dt.open(new File("c:\\fileName.xls"));
} catch (IOException e) {
e.printStackTrace();
}
First you maximize your XLS reading software (for e.g. office) window manually and close again and then run your program. It will open in maximized mode only.

Related

How to open an Excel file in default .xslx app using Java?

I created an excel file using POI but now I need to open it using the default .xslx app (MS Excel in my case). I want to specify that I don't need to parse it or read its contents, but just open it in MS Excel.
I figured it out after a while, putting this here in case anyone googles it and this page comes up :
try {
File excelFile = new File(filePath);
Desktop.getDesktop().open(excelFile);
} catch (IOException e) {
e.printStackTrace();
}

include excel in .jar

I need to know what is the best way to export excel file into my .jar code.
I used GetDesktop open file statement, and that works good when I run it inside of Eclipse IDE. But when I export my code into a .JAR, the excel sheet will not open, it will not do anything.
public void actionPerformed(ActionEvent e) {
try { Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
desktop.open(new File("Resources/pics/chart 2.xls"));
} catch (IOException ioe) {
ioe.printStackTrace();
}}
I have a picture image in a jframe, and I have 2 buttons, inside of the picture, for the user to click on. Depends on which button they click on, I need that certain excel file to open. Just for viewing purposes, no editing is involved. I tried using apache POI, but the excel file did not open at all, but when I use the code above at least it opens in eclipse. I see that there is others with the same problem, but mine I need it to just open excel not edit.
thanks
You must copy it out of the jar. You can get an inputstream using InputStream in = ClassYouAreIn.class.getResource("someFile.xls"). Here is an example:
try (InputStream in = ClassYouAreIn.class.getResource("someFile.xls")) {
// copy it here, for example using:
Files.copy(in, Paths.get("somefile.xls"), StandardCopyOption.REPLACE_EXISTING);
}
NB: Your catch code is really bad: exceptions contain 4 interesting bits of information (type, message, stacktrace, causal chain) and you're chucking 3 of the 4 things, AND you are causing a cascade of errors by not aborting. You should always just throw them onwards (put throws IOException on this method) unless you have an ACTUAL strategy to deal with it (and logging / printing it is not dealing with it!).

Open a large html file on browser

I have a desktop bassed eclipse plugin application. In which, for a case I am opening html report file in browser.
for which I am using this:
try {
browser = new Browser(shell, SWT.NONE);
browser
.setUrl(path);
shell.open();
}
catch (SWTError e) {
e.printStacktrace();
}
But in one case I am having html file of 90 MB which is not able to get open by it. It got hanged.
So request you for any action around it need to be done.
On any other optimized way to open it.
Instead of loading the whole file, load it on demand.
Window onscroll event is your friend.

PDF file not opening

I have Java code to open the pdf file in Java and it is running perfectly.
Code is
try {
File pdfFile = new File("/Users/alkandari/Desktop/SMART/Fahim/test.pdf");
if (pdfFile.exists()) {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(pdfFile);
} else {
System.out.println("Awt Desktop is not supported!");
}
} else {
System.out.println("File is not exists!");
}
System.out.println("Done");
} catch (Exception ex) {
ex.printStackTrace();
}
However when I put this in some method say showMyPDFFile() and try to call that on commandLink, pdf doesn't get open. It says Awt Desktop is not supported!.
<h:commandLink value="View PDF/ DOC"
action="#{PersonalInformationDataBean.showMyPDFFile()}" />
Any idea why pdf is not getting opened?
So just to check, you're writing a web application, right?
The Desktop class refers to the local desktop - so it's for local GUI client applications. Even if it were supported, you'd be opening the PDF on the server's desktop - not the client's desktop, which I assume is what you're after.
It sounds like you should basically just be linking straight to the PDF file (as URL). You'll need to make the PDF available via your web server, of course.

Opening an Excel file using the default program

My program successfully creates and fills a Excel(.xls) file. Once created, I would like the new file to open in the system's default program (Excel in my case). How can I achieve this?
For an older program where I wanted to open a txt file in Notepad, I used the following:
if (!Desktop.isDesktopSupported()) {
System.err.println("Desktop not supported");
// use alternative (Runtime.exec)
return;
}
Desktop desktop = Desktop.getDesktop();
if (!desktop.isSupported(Desktop.Action.EDIT)) {
System.err.println("EDIT not supported");
// use alternative (Runtime.exec)
return;
}
try {
desktop.edit(new File(this.outputFilePath));
} catch (IOException ex) {
ex.printStackTrace();
}
When I try to use this code for an Excel file it gives me the following error:
java.io.IOException: Failed to edit file:C:/foo.xls
Suggestions?
Try to use Desktop.open() instead of Desktop.edit() :
Desktop dt = Desktop.getDesktop();
dt.open(new File(this.outputFilePath));
If Desktop.open() is not available then the Windows file association can be used :
Process p =
Runtime.getRuntime()
.exec("rundll32 url.dll,FileProtocolHandler " + this.outputFilePath);
You probably did the Runtime.exec incorrectly. Give this a look to see if that's the case.
If you just want to open an Excel file with Java, I'd recommend using Andy Khan's JExcel API. Perhaps using that with a Swing JTable will be just the ticket.
The most simple and efficient way.
Desktop.getDesktop().open(new File("inputFilePath"));

Categories

Resources