Using java to create excel file with a web query link - java

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.

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

Adding data in Excel file without closing it

I am currently using Apache POI to enter data into Excel file. The only problem is I can not keep the file open if I have to append data to the same file. Are there any specific sample codes which would allow me to do so?
My basic requirement is to fetch Runtime data from a place (this I am able to do) and add it to the Excel sheet while the file is still open.
Any suggestions?
I don't think that this is possible without a C# addon or some kind of macro. You could write a simple C# addon for Excel that connects to your java programm and recieves the realtime data. The addon will write it to the spreadsheat then.

putting an xls file in a jar

I have an xls spreadsheet that I'm querying using metaModel, and I want to keep the xls file in the jar, because it won't be updated. The method used for creating the data context doesn't allow inputstreams, I tried using this code:
DataContext dataContext = DataContextFactory.createExcelDataContext(getClass().getResourceAsStream("database.xls"));
Unfortunately this doesn't work, as the method createExcelDataContext doesn't take inputstreams as a parameter. Is there any way to keep the file in the jar?
It seems the easiest way to do this is just export to csv, as this is easily done.
Given that other kinds of contexts can be created from an InputStream, I would guess that it's a limitation of Excel, and that you won't be able to open the Excel file if it isn't on the file system.
Consider extracting the file from the jar and copy its content to a temporary file, possibly deleted when the application ends. See File.createTempFile() and File.deleteOnExit().

Automatically update database when new excel sheet is added in a folder

I need to write one java program which monitors a folder containing excel sheets (.xls format) . Once a new excel sheet is added I have to update the database in db2 and move the excel sheet to other place.
Please suggest
It sounds like you need to get notified if files appear in a directory. Java 7 has good support for this, see this article about the Watch Service API. The db2 part, well it isn't clear what you mean by "update", but likely you want to parse the content of the xls file and make some database updates. Apache POI is a good starting place for reading the content of the file into a Java process.

Generate and return docx file to client

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

Categories