JXLS Set Sheet Name - java

I would like to provide a way for a sheet name to be specified by a command. The sheet names should be dynamically created based on the content in the sheet.
I note that it is possible to specify fixed sheet names for dynamic sheets when using the multisheet attribute of each-command.
In the version 1 documentation there is a reference to the ability to rename a sheet using ${workbook.setSheetName(0, department.name)}. Can this be used in v2? How is it supposed to work? Would workbook need to be added to the context or was it previously available?
I tried renaming the sheet in a custom command but it seems that there is too much dependence on sheet names in XlsArea for the name to be changed halfway through XlsArea.applyAt. The template sheet is not deleted and processing is not completed.
I thought about trying to get the custom command to add an area listener to change the sheet name. But the area listeners are only called on the parent area which I cannot access from within the command.
Thanks,
Wayne.

I doubt this is the correct way to do it, but til now this is the only way I've managed to do it:
Context context = PoiTransformer.createInitialContext();
Workbook workbook = WorkbookFactory.create(templateInputStream);
workbook.setSheetName(0, "newName");//Changing name of the first sheet
PoiTransformer transformer = PoiTransformer.createTransformer(workbook);
transformer.setOutputStream(resultOutputstream);
JxlsHelper.getInstance().processTemplate(context, transformer);

If you use multisheet generation feature you can just pass your own CellRefGenerator to Each-Command and create the sheet names dynamically in it.
Currently it is not possible to pass the CellRefGenerator in Excel template but it should be quite straightforward to add this functionality.
Or you can set the CellRefGenerator with Java code as shown in the documentation.

It can be done using folowing method of XLSTransformer class.
XLSTransformer transformer = new XLSTransformer();
setSpreadsheetToRename(oladSheetName, String newSheetName)

Related

getting MS Excel document properties using Java

Please refer to the https://i.imgur.com/o8inFsx.png. The file is an MS Excel file encrypted with a password.
I am using Aspose Cell to parse this. Before I can do that, I need to get some metadata specified in the custom properties.
Is there a way to get this using Java?
In this example, I want to get "e99y" by providing "myName".
The sheet can belong to any of the users. I want to embed the property myName/e99y so the system will know which user the sheet belongs to. Then, it'll look up the correct password from the system to encrypt and open the sheet.
Thank you!
Could you try the following sample code if it works for you:
e.g
Sample code:
// Creating an XLS LoadOptions object
LoadOptions loadOptions = new LoadOptions(FileFormatType.EXCEL_97_TO_2003);
// Setting the password for the encrypted Excel file
loadOptions.setPassword("1234");
//Instantiate a Workbook object
Workbook workbook = new Workbook("C:\\book1.xls", loadOptions);
//Retrieve a list of all custom document properties of the Excel file
CustomDocumentPropertyCollection customProperties = workbook.getWorksheets().getCustomDocumentProperties();
//Accessng a custom document property by using the property name
DocumentProperty customProperty2 = customProperties.get("myName");
I am working as Support developer/ Evangelist at Aspose.

JXLS - how to create hyperlink to Excel worksheets in workbook

I am trying to create an Excel workbook with with JXLS. I want a text hyperlink for navigating through worksheets in a workbook. I couldn't find any helpful information online. Please give any idea or hyperlink for that can help to to solve the problem. Thanks
jXLS is a small and easy-to-use Java library for writing Excel files using XLS templates and reading data from Excel into Java objects using XML configuration. If you are trying to create hyerlink, jXLS doen't have low lever excel manupulation capability. But you can to use Apache POI a free library. This code create hyperlink to a Cell for that task as shown below.
//creating the cell
Row row = my_sheet.createRow(0);
Cell cell = row.createCell(0);
//creating helper class
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFCreationHelper helper= workbook.getCreationHelper();
//creating the hyperlink
link = helper.createHyperlink(HSSFHyperlink.LINK_DOCUMENT);
link.setAddress("'target_worksheet_name'!A1");
//optional hyperlink style
XSSFCellStyle hlinkstyle = workbook.createCellStyle();
XSSFFont hlinkfont = workbook.createFont();
hlinkfont.setUnderline(XSSFFont.U_SINGLE);
hlinkfont.setColor(HSSFColor.BLUE.index);
hlinkstyle.setFont(hlinkfont);
//applying the hyperlink to the cell
cell.setHyperlink(link);
jxls supports Parameterized formulas, you can probably
use cell with a formula like below
=HYPERLINK("http://test.com/", "Click ME")
Parameterize it in cell with
=HYPERLINK(${paramLink}, ${paramDisplay})
pass parameters to jxls context and they will be rendered as proper link
http://jxls.sourceforge.net/samples/param_formulas.html
Old question, but another possible solution is to use JXLS 2+ and the PoiTransformer. It has a utility class called PoiUtil which can be injected to the context.
final var transformer = PoiTransformer.createTransformer(inputStream, outputStream);
// it is important to create the context like this
// or you can manually insert the PoiUtil instance if you wish
final var context = PoiTransformer.createInitialContext();
// setup your context...
JxlsHelper.getInstance().processTemplate(context, transformer);
And in the template you can use it like this: ${util.hyperlink(linkVar, titleVar)} where the linkVar and titleVar are the corresponding variables in the context.

Image not getting displayed in exported Excel sheet

I am exporting an Excel sheet using NatTable (Eclipse Nebula). It contains an image in the header layer and some text in the header as well as body. Here is the code for configuration:
ImagePainter bgImagePainter = new ImagePainter(getImg());
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, bgImagePainter, DisplayMode.NORMAL, HeaderLayout.overImg);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, cellStyle, DisplayMode.NORMAL, HeaderLayout.overImg);
Also, I use ExcelExporter to export the data to an Excel Workbook.
I don't know where the error could be, because the text in all other columns is displayed perfectly. If you need to look at any other code snippet, I will update it.
I don't see the what the "error" could be, since you are not telling what your issue is. But I suppose your question is why the image is not exported. And AFAIK the reason is that the NatTable exporter does not support exporting of images at the moment.

Hyperlink not working while populating Data into Excel

I am populating URL into Excel sheet through java code. But the hyperlink is not enabled if I populate through Java code.
If I type URL into Excel sheet directly, hyperlink automatically coming. Could you please let me know why it is not coming if I populate through code.
You may need Worksheet_followhyperlink enabled in the spreadsheet through vba. I've encountered this populating hyperlinks into Excel before. Pop this into the sheet with the links:
Private Sub Worksheet_followhyperlink(ByVal Target As Hyperlink)
Application.EnableEvents = True
Target.Follow
' Any other code you might need.
End Sub

Refresh Pivot Table with Apache POI

I'm currently working on a Java application that uses a template excel file that contains a pivot table.
The template file also has a data sheet that seeds the pivot table. This data sheet is dynamically loaded in the java application through the Apache POI api.
When I open the excel file I must refresh the Pivot table manually to get the data loaded correctly.
Is there any way to refresh the Pivot table with the POI api so I don't have to manually do it?
You can simple activate an option that will refresh the pivot table every time the file is opened.
This Microsoft documentation says :
In the PivotTable Options dialog box, on the Data tab, select the Refresh data when opening the file check box.
It is possible.
In the PivotCacheDefinition, there is an attribute refreshOnLoad that can be set to true. The cache is then refreshed when the workbook is opened. More information here.
In POI this can be done by calling the method setRefreshOnLoad(boolean bool), that takes a boolean as parameter, on a CTPivotCacheDefinition.
EDIT:
The Apache POI now provides the possibility to create pivot tables and the pivot table is refreshed on load as default. Class XSSFPivotTable
This post says you can turn on an option on the pivot table that will cause it to refresh itself automatically every time the workbook is opened:
How can I refresh all the pivot tables in my excel workbook with a macro?
Hopefully this will still be true after you have used Apache POI to refresh or extend the data rows on which the pivot tables are based.
protected void setRefreshPtOnLoad(boolean refreshOnLoad){
List<POIXMLDocumentPart.RelationPart> relationParts = ((XSSFWorkbook) workbook).getRelationParts();
for (POIXMLDocumentPart.RelationPart part : relationParts) {
PackageRelationship relationship = part.getRelationship();
if (relationship.getRelationshipType().contains("pivotCacheDefinition")){
String relationId = relationship.getId();
XSSFPivotCacheDefinition cache = (XSSFPivotCacheDefinition)((XSSFWorkbook) workbook).getRelationById(relationId);
CTPivotCacheDefinition ctCache = cache.getCTPivotCacheDefinition();
ctCache.setRefreshOnLoad(refreshOnLoad);
break; // if only pivot table cache exists in workbook, else - remove *break*
}
}
}
The basic answer to this is no. POI is a document format reader and writer. Updating the Pivot table is an excel engine issue. Sure, another application could try to duplicate the Excel engine behavior here, but that is really going to get ugly. I recommend using Joel's workaround of accessing the excel COM objects over a webservice to get this kind of thing done.
This might work:
XSSFPivotTable pivotTable = pivotSheet.getPivotTables().get(0);
pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().setRefreshOnLoad(true);

Categories