How to open an Excel file in default .xslx app using Java? - 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();
}

Related

Is there a way to view rptdesign as PDF without creating PDF file first?

Currently I'm viewing birt rptdesign as PDF by creating to PDF file first as follow :
design = engine.openReportDesign("c:/temp/customer.rptdesign");
task = engine.createRunAndRenderTask(design);
PDFRenderOption options = new PDFRenderOption();
options.setOutputFileName("c:/temp/customer.PDF");
options.setOutputFormat("PDF");
Then I open it with desktop as follow:
File file = new File("c:/temp/customer.PDF");
try {
Desktop.getDesktop().open(file);
} catch (IOException e) {e.printStackTrace();}
Is there a way to view it as PDF directly without creating PDF file first?
I'm using jdk-8u181-windows-i586 and birt-runtime-4.8.0-20180626.
Thanks.
You can use the sample servlet, or write your own servlet.

Opening Excel file in Java using POI results in error InvalidFormatException: Package should contain a content type part [M1.13]

I have seen several posts dealing with this error, but none have addressed the specific problem I am encountering.
Here is the code I am testing with.
public class CicTemplateTest {
public static void main(String[] args) {
try {
new CicTemplateTest().process();
System.out.println("DONE xlsx");
} catch (Exception e) {
e.printStackTrace();
}
}
public void process() throws Exception
{
String inputFileName = "Test.xlsx";
String folder = "/Temp3/formaterror/";
File file = new File(folder + inputFileName);
FileInputStream inputStream = new FileInputStream(file);
Workbook wb = new XSSFWorkbook(inputStream); // line where error occurred
OutputStream out2 = new FileOutputStream(new File(folder+"JunkFile.xlsx"));
wb.write(out2);
out2.flush();
out2.close();
}
}
Here is the error I get.
org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:274)
at com.aquent.sdc.jeffk.poiftest.CicTemplateTest.process(CicTemplateTest.java:41)
at com.aquent.sdc.jeffk.poiftest.CicTemplateTest.main(CicTemplateTest.java:25)
The file in question is not an XLS file. It seems to be a weird form of an XLSX file. I wish I could share the file, but I can't as the data is proprietary. I've had to unzip XLSX files in the past and am somewhat familiar with the contents. When I unzip this file the contents do not look normal.
List of files in XLSX file
There are no XML files in there. I've examined every file in the XLSX and they are all binary files. No text file, no XML files. It just doesn't look like a normal XLSX file at all.
It opens fine in Excel, and if I save it, the saved file is a normal XLSX file and sample code works fine.
We get about 1000 users per year submitting these files, and about 15% come to us in this format. I think most of the files with the format problem are from outside of North America.
I have tried reading the file using POI ver 3.9 and 3.14 and get the same error.
I have 2 questions.
How are users creating these files?
How can I get my app to open these files?

Can I reference a file from within a Spring Boot project?

I am currently writing a spring boot program that should populate an XLSX template with data and am trying to read the template from inside the project. My directory structure is as follows:
I am now trying to access the file using the following code:
try {
InputStream fis = this.getClass().getResourceAsStream("xlstemplates/billingReview.xlsx");
XSSFWorkbook billingReviewTemplate = new XSSFWorkbook(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Unfortunately I have yet to find a way to read the xlsx file into an InputStream. It always comes back null. Anyone have any pointers on how I can reference that file that is included in the project?

Autoprinting an iText-generated PDF

Is there a way to send a newly iText-generated PDF directly to the printer ? Preferably in the same method that is used to generate it and save it to disc ?
I tried and this works fine after you have saved the file in the disk.
import com.lowagie.tools.Executable;
try{
Executable ex = new Executable();
ex.openDocument(fileName);
ex.printDocument(fileName);
}catch(IOException e){
e.printStackTrace();
}
For more info, see this Daniweb forum conversation.

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