Generate and return docx file to client - java

I have an app that generates a docx file base on user input. It uses Apache POI to generate the docx file and I can get the FileOutputStream from that, the document opens perfectly on a local machine when I write it to a file.
The webapp is using Dojo xhrPost to send the necessary data to the server to generate the document. What I am wondering is how I get the docx file to the client.
I know I can do it be creating a temp file and passing the location of that file to the client to download, but I would think there would be a way to do it by piping the FileOutputStream straight to the client, which would be much cleaner.
Any suggestions?

The answer from Mr Shiny in this SO question has an example streaming an excel file, should be very similar for a docx:
How can I get an Input Stream from HSSFWorkbook Object
Except that a docx content type should, probably, be application/vnd.ms-word

Related

Possible to automate an excel process using java?

My current program is trying to download files and then combine them into one large excel file. The issue that I'm struggling with is that the website I'm downloading them from is for some reason making them .html files, but appending the .xls extension to them. This allows them to be opened by Excel manually but does not allow me to use Apache POI in order to read them as it sees a file format/extension difference. My process is as follows:
1 - Run part of my program which downloads a file through my web browser using Selenium - This works fine
2 - Manually open each downloaded file and Save-As xlsx files (Note: When I open them in Excel manually is when I'm told there is a file format/extension difference just to be clear)
3 - Run the rest of my program which combs through each new file (the ones created in step 2) and appends all the data to the ultimate output file - This works fine
Is there any way to automate the process or am I going to have to continue to do it manually?
you said in the comment that you opened the file in text editor and saw that it is HTML5.
I would use HTML parser like jsoup to get the data that you need and create a new file using Apache POI.
You can use EasyXLS library. It allows to read HTML files and save as XLSX.
ExcelDocument workbookForXLSX = new ExcelDocument();
for (int i=0; i<fileCount; i++){
ExcelDocument workbookForHTML = new ExcelDocument();
workbookForHTML.easy_LoadHTMLFile(filePath[i]);//or stream to the file
workbookForXLSX.easy_addWorksheet((ExcelWorksheet)workbookForHTML.easy_getSheetAt(0));
workbookForHTML.Dispose();
}
workbookForXLSX.easy_WriteXLSXFile(filePathXLSX);
workbookForXLSX.Dispose();
You can download the Excel library for Java from:
https://www.easyxls.com/java-excel-library
More details about reading HTML files and what HTML tags are supported at:
https://www.easyxls.com/manual/basics/import-from-html-file-format.html

Java save xls file as PDF

I have a simple java program that creates .xls file (open office excel file), and I want to save it as pdf. I saw some answers here but none of them worked for me. Is there a simple and free way to do so?
The file contains only 1 page of .xls
Thanks
Not sure, please provide more details , share code so that the answer can be more precise, but for code perspective it can be achieved via Apache POI.something like below ....
//Instantiate a new workbook with excel file path
Workbook workbook = new Workbook("F:\\FileTemp\\Book1.xls");
//Save the document in Pdf format
workbook.save("F:\\FileTemp\\MyPdfFile.pdf", FileFormatType.PDF);
I have published a library that saves files, and handles everything with one line of code only, you can find it here along with its documentation
Github repository
and the answer to your question is so easy
String path = FileSaver
.get()
.save(fileXls,"file.pdf");

how Read a particular Page from a doc or pdf file.

I have some document of .doc and .pdf file and my requirement is to read a particular page from the .doc or .pdf file which i will provide at the run time .This can be possible by reading page by page and at the end of each page if i do numbering .but some i am getting some document where numbering is not their so how can i do that?
is their any api or any other logic so that i can fixed this problem?
hello all
i have .DOC file but i am not supposed to read entire file instead i am given a page number.
therefore i got to read only that particular page from the doc file.
I am using apache.poi api.
file = new File("c://doc/assignment/afternoon_24.doc");
FileInputStream fis=new FileInputStream(file.getAbsolutePath());
i need to read the page X of this file and write to a text file?
I guess there is a missunderstanding: You can not read a DOC (or PDF) simply as an Inputstream and skip pages (unless you know and evaluate the fileformat).
Both files have a format (encoding the formatting and meta info into some binary formats). Just try to open a PDF in notepad or another plain text editor. You will see it.
As mkl suggested: to access the contents of a DOC (or PDF) you need a library that can handle that fileformat. For Microsoft Office formats there is for example the open source library Apache POI, for PDF there is for example PDF box among others and a full thread about it. There are different libraries for each of the formats with different features and licensing models.

Using java to create excel file with a web query link

I want to create excel file from java (for example with Apache POI) that contains web query with link to my application but I couldn't find any reference for it. Is that possible?
I'll even settle for updating the link of a web query in an existing excel file.
Thank you.
What I finally did is to create an xlsx file with a temp connection. Then I opened the file using a zip reader and modified the connections.xml file inside and then I zipped the file again to xlsx.
Works like a charm.

Getting extension check(hardening) alert while opening a .xls file using Office 2007/2010

I am working on a struts based web application. In that application, we generate and download xls file from Jsp.
In Jsp file and web.xml, I have set the content-type as "application/vnd.ms-excel"
it seems xls files generated by the jsp pages are not real excel files, but a text format that is understood by the MS Excel. Hence excel opens the files and displays the output similar to excel files saved by MS Excel. Since newer versions of MS Office 2007/2010 checks the file extension and the content inside the file, they issue a warning that the file format does not match with the content.
To get rid of the warning how can i ensure that the generated xls is real office excel file .
Please help.
For future readers who might need this...
Excel will complain anyway, even if your excel file is 'correct', as long as its structure doesn't properly match the extension. So if you're saving as an .XLS, it expects to see the classic excel file.
The popup you are getting is because of the new security feature in Office 2007, called Extension Hardening and you can disable it if you want - either manually in the registry, or you can save the patch to a .REG file and share it to you clients etc.
Save those 2 line below into a GiveItSomeName.reg file, which you can then email to your clients and tell them to execute it.
[HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security]
"ExtensionHardening"=dword:00000000

Categories