Apache poi excel link to other page/sheet - java

I need to generate an excel report using java. So I'm planning to use Apache POI.
But there is one required which I'm not sure if its possible through apache POI.
Requesting you to please help me with this.
Requirement:
Excel doc should have multiple sheets say sheet1 and sheet 2.
sheet1 will be having multiple links.
sheet2 will be having 100's of columns.
Each link in sheet1 should be linked to one of the column of sheet2.
When user clicks on any of these links, it should take user to sheet2 and automatically focus on that column.
For ex. If user clicks on link pointing to 95th column of sheet2, then clicking on that link should open sheet2 and 95th column should be in focus.
Please let me know if its feasible. I searched through net but couldn't find any way of doing it.
Any pointer is really appreciated.

Well, I think you could use Hyperlinks to Sheet cells(if not columns) in the same document.
http://poi.apache.org/spreadsheet/quick-guide.html#Hyperlinks
Following is an sample extracted from above link-
To create links you could do something like:
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Cell cell;
Sheet sheet = wb.createSheet("Hyperlinks");
//URL
cell = sheet.createRow(0).createCell((short)0);
cell.setCellValue("Worksheet Link");
Hyperlink link2 = createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT);
link2.setAddress("'Target Sheet'!A1");
cell.setHyperlink(link2);
cell.setCellStyle(hlink_style);
This will take you to a sheet named 'Target Sheet' and then to cell A1

Related

Is it possible to edit existing Excel formulas using POI

I have one excel template placed in my application's configuration folder. Now while downloading that excel template, I am trying to edit it's data and existing formulas using POI api. I am able to edit data easily.
How can I edit existing formulas? The formula which I am trying to edit, is attached with more than 1000 cells, so instead editing every cell and updating formula, I am trying to update that formula itself.
I am using following code to get workbook object
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File("ExcelTemplate.xlsx")));
XSSFSheet sheet = workbook.getSheetAt(5);
for(int i = 2; i<174; i++) {
Row row = sheet.getRow(i);
Cell cell = row.getCell(0);
cell.setCellValue("My Data");
}
FileOutputStream output_file =new FileOutputStream(new File("UpdatedExcelTemplate.xlsx"));
workbook.write(output_file);
output_file.close();
Problem which I am facing is as following:
I have one sheet (say sheetA) in which I am inserting values at run time. And I am referring that list of data in another sheet (say sheetB) as drop down for any cells. Now as per data range in sheetA, my formula should get change.

Lock excel work book using apache poi api java

I have to lock a workbook while reading and writing a row.
Is there way to accomplish this using Apache POI API without protecting and password option?.
As a suggestion, paste some of your code that could help others to understand and solve the problem.
As far as I know you can't "lock" the hole Workbook but one thing you can try is to apply a CellStyle to cells you want to protect while reading or writing.
Workbook wb = new XSSFWorkbook();
CellStyle lockedCellStyle = wb.createCellStyle();
lockedCellStyle.setLocked(true);
Sheet sheet = wb.createSheet();
// .... Create rows and cells as needed
// When Writing or reading
Cell cell = getCellsToLockWithAnyMethod();
cell.setCellStyle(lockedCellStyle);

How to write to an existing file using SXSSF?

I have an .xlsx file with multiple sheets containing different data. Of all the sheets one sheet needs to accommodate close to 100,000 rows of data, and the data needs to be written using Java with poi.
This seems quite fast and simple with SXSSFWorkbook, where I can keep only 100 rows in memory, but the disadvantage is that I can only write to a new file (or overwrite existing file).
Also, I am not allowed to 'load' an existing file, i.e
SXSSFWorkbook wb = new SXSSFWorkbook(file_input_stream) is not allowed.
I can use Workbook factory:
Workbook workbook = new SXSSFWorkbook();
workbook = WorkbookFactory.create(file_input_stream);
but when the time comes for me to flush the rows,
((SXSSFSheet)sheet).flushRows(100);
I get the error that type conversion is not allowed from XSSFSheet to SXSSFSheet.
I tried to see if there was any way to copy sheets across different workbooks, but so far it seems it has to be done cell by cell.
Any insights on how to approach this problem?
You are probably having a template to which you want to add large data. You need to use the SXSSFWorkbook(XSSFWorkbook) constructor:
XSSFWorkbook wb = new XSSFWorkbook(new File("template.xlsx"));
SXSSFWorkbook wbss = new SXSSFWorkbook(wb, 100);
Sheet sheet = wbss.createSheet("sheet1");
// now add rows to sheet

How to print excel file from java?

First Question on the community!
Let me explain a little the question.
I have a user interface, that allows me to create an excel file. And I want to add at that ui the a "print Document" button. This button is supposed to allow the user to print that excel document. So what I need is a way to call the windows printing dialog for that file, so the user can print the file.
I have read http://docs.oracle.com/javase/tutorial/2d/printing/printable.html, But I can not find the way of telling the printing dialog what file to print.
Thanks in advance!
I think Apache POI is the best thing for you. Code syntax is like
POIFSFileSystem filesystem = new POIFSFileSystem(new FileInputStream(file));
HSSFWorkbook wrkbook = new HSSFWorkbook(filesystem );
HSSFSheet sheet = wrkbook.getSheetAt(0);
HSSFRow row;
HSSFCell cell;
//Keep on doing your manipulation.
More about Apache POI.

After removing a sheet from an excel workbook with Apache Poi, a sheet remains?

I'm using workbook.removeSheetAt() to remove a certain named sheet from a workbook. However, the workbook contains a new sheet called "Sheet3" after writing it out to disk. What gives? I'm not creating this new sheet anywhere in my code. Note: I am using XSSFWorkbook and all the other XSSF stuff.
Some more info: Here is a snippet from workbook.xml inside the output xlsx. I am deleting sheet with index=2, ostensibly sheetId=3. The template workbook I am starting with only has four sheets.
<sheets>
<sheet r:id="rId1" sheetId="1" name="Sheet1"/>
<sheet r:id="rId2" sheetId="2" name="Params" state="hidden"/>
<sheet r:id="rId4" sheetId="4" name="Warnings" state="hidden"/>
<sheet r:id="rId3" sheetId="5" name="Sheet3"/>
</sheets>
Apache POI is using index based sheets (kinda like an array). If you have a sheet at a position that is greater than x, there must exist a sheet at position x even if it is blank. You could try moving the sheets where n > x to position n - 1 using setSheetOrder
Seems like there is a bug report similar to this: Bug Report
What about hiding the sheet: setSheetHidden

Categories

Resources