Excel has different modes for viewing a sheet: Normal, Page Layout, Page Break Preview. (In excel 2010: in the view tab). The view mode is saved seperately for each sheet in a workbook and is restored when opened again.
I am trying to find a way to set a view mode using Either HSSF or XSSF.
In the old binary format, finding the answer seems quite impossible unfortunately.
In 2007+ OOXML format diffing does give the basic answer, looking at xl/worksheets/sheet1.xml
In normal view:
<sheetViews>
<sheetView rightToLeft="1" tabSelected="1" zoomScaleNormal="100" workbookViewId="0">
</sheetViews>
In page layout view:
<sheetViews>
<sheetView rightToLeft="1" tabSelected="1" view="pageLayout" zoomScaleNormal="100" workbookViewId="0"/>
</sheetViews>
That is the second tag in each sheet. Is there any XSSF API option to edit that attribute? (or the only solution to the problem would be unpacking the file, editing it and repacking)
Thanks!
XSSF doesn't expose this directly, but you can get at it if you want
From the XSSFSheet object, call getCTWorksheet to get the low level XML object backing the sheet. CTWorksheet provides a getSheetViews method. You'll like want something like:
CTSheetView view = sheet.getCTWorksheet().getSheetViews().getSheetViewArray(0);
view.setView(STSheetViewType.PAGE_LAYOUT);
Related
I have an .xls file which I call it model. Now my probleme is that I want to create over 50 .xls file that have that same format of the model. So properly my question is : how do I create a worksheet (excel file) based on an excel template using java? Is there is any way to do it without copying the style between sheets ??
This example does not use a pre defined template but you can go check out my code and morph it into something worth for you.
https://github.com/ernst223/spread-sheet-exporter
So you can maybe code the style in my class and then use my class for all of your excel files
SpreadSheetExporter spreadSheetExporter = new SpreadSheetExporter(List<Object>, "Filename");
File fileExcel = spreadSheetExporter.getExcel();
as you didn't make any apparent effort before asking i will do the same
excel api
I am using aspose-cells-8.7.2-java. When I refresh the pivot table and save it, the excel file is getting corrupted. When I try to open the excel file I am getting the alert message as below :
"Excel found unreadable content in 'Book1.xlsx'.Do you want to recover the contents of this workbook?If you trust the source file of this workbook, click yes."
The code is as below :
Workbook wb = new Workbook("Book1.xlsx");
PivotTable pt = wb.getWorksheets().get(1).getPivotTables().get(0);
pt.refreshData();
pt.calculateData();
wb.save("Book1.xlsx");
Any help ?
I found this thread where the same issue is logged as a ticket :
http://www.aspose.com/community/forums/thread/683715/aspose.cells-generates-a-corrupted-xlsx-file-excel-2007-fails-to-open.aspx.
Is this issue solved?
I'm afraid the logged issue is not resolved yet. By the way, do you use similar Excel file or yours template file "Book1.xlsx" is different. Moreover, your issue can be template specific (if you are using different file) and might have different scenarios, so we need your template "Book1.xlsx" file to properly evaluate your issue on our end. We recommend you to kindly create a separate thread in Aspose.Cells forum with your template Excel file, we will evaluate your issue and help you better there.
I am working as Support developer/ Evangelist at Aspose.
I am using Apache POI to generate an excel workbook containing multiple sheets. I want to create a hyperlink from one sheet to another. How to accomplish this ? There are solutions I found for establishing hyperlinks to websites, even other excel files but not to other sheets within the same workbook. Does Apache POI allow us to do this ?
Yes, Apache POI allows you to create a hyperlink to another sheet in the same workbook. According to the Apache POI Quick Guide:
cell = sheet.createRow(3).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 creates a cell with a hyperlink in it of type LINK_DOCUMENT, with an address of a cell reference, which can be in the same sheet or another sheet. Then it sets the cell style to an existing cell style (created earlier in the code sample), so that it looks like a hyperlink.
You can also link to a cell address in another worksheet using POI.
Create your link as type Hyperlink.LINK_DOCUMENT. Then send setHyperlink() a string in this format :
file:/E:/PROJECTS/SomePrj/ExcelFileName.xlsx#'ExcelSheetName'!B5
that will work - at least for XSSF.
Use aFile.toURI().toString() to build the filepart before the '#' character. Don't forget to add both # , ' and ! characters (exactly as in my example) or it will not work.
I do not see this technique demonstrated in the (otherwise very good) POI documentation.
I found the right way. Tested on poi 3.16 and solved the annoying problem of not allowing space in the link(sheet name can have space). you will get error if you try
link.setAddress("ExcelWrite.xlsx#'Sheet 1'!A2");
RIGHT CODE:
cell = sheet.createRow(1).createCell(0);
cell.setCellValue("File Link");
XSSFHyperlink link = (XSSFHyperlink) createHelper.createHyperlink(HyperlinkType.FILE);
link.setAddress("ExcelWrite.xlsx");
link.setLocation("'Sheet 1'!A2");
cell.setHyperlink(link);
cell.setCellStyle(hlink_style);
Dear all,
I'm working with Apache POI and I would like to duplicate a slide containing several charts from code.
The code below (inspired by https://poi.apache.org/slideshow/xslf-cookbook.html#Merge) works fine when there is no chart on the slide.
Unfortunately, it seems that the charts are not duplicated with this method: when I try to open the resulting file, Powerpoint detects a problem, tries to repair it, but fails, and I get empty slides.
I've checked the underlying XML files (using Open XML SDK), and it seems that the chart themselves (in the folder /ppt/charts) are not duplicated, and the relationship files (in the folder /ppt/slides/_rels) are not completely updated.
Here is my current code:
// Open slideshow
FileInputStream fileInputStream = new FileInputStream(sourceFilePath);
XMLSlideShow slideShow = new XMLSlideShow(fileInputStream);
fileInputStream.close();
// Duplicate slide
XSLFSlideLayout layout = slide.getSlideLayout();
XSLFSlide newSlide = slideshow.createSlide(layout);
newSlide.importContent(slide);
// Save updated slideshow
FileOutputStream fileOutputStream = new FileOutputStream(outputFilePath);
slideShow.write(fileOutputStream);
fileOutputStream.close();
Do you know how I could clone a slide and its charts ?
Thanks a lot, and best regards!
You can't. I tried and tried, the problem is that the duplication does not handle images or charts.
I had to copy files by hand through a script. Here are the steps:
Locate the slide file
Duplicate it
Find the chart object in the XML file, and note the relation Id
In the relations file, check which file is designated by the relation Id
Duplicate this file
You also have to duplicate the relation file for the new slide, and update the name
The new slide is not visible, you have to update presentaion.xml
Please note: if you want your PPTX to work with Microsoft Powerpoint, you'll also have to duplicate associated Excel workbook (see in the relation file of the chart)
My problem was really close to yours with version 5.2.2, the chart was indeed correctly duplicated but the reference to the internal workbook of the sheet was copied too, by reference ! (It's actually done here : https://apache.googlesource.com/poi/+/refs/tags/REL_5_2_2/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFGraphicFrame.java#241 )
It means that when I edited the chart values in the duplicated slide I edited both chart values. It makes visually no difference in powerpoint at first but when you right-click>edit data on the chart, it re-renders and the data changes visually.
To avoid that, just re-set the workbook of the chart to null so that it creates (or really load from the relation part) a new one for the duplicated chart
The code to duplicate a slide with a chart should look like this :
XSLFSlide oldSlide = ppt.getSlides().get(number);
XSLFSlideLayout layout = oldSlide.getSlideLayout();
XSLFSlide newSlide = ppt.createSlide(layout);
newSlide.importContent(oldSlide);
newSlide.getRelations().stream()
.filter(r -> r instanceof XSLFChart)
.forEach(chart->((XSLFChart)chart).setWorkbook(null));
//to force loading the correct worksheet
I hope it help some people stumbling upon this :)
Since Apache POI 4.0.0, the original code from the question will work to duplicate a slide.
I am trying to create a hyperlink using the following code
CreationHelper createHelper = wb.getCreationHelper();
cell.setCellValue("Click Here");
Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_FILE);
File f = new File("C:\\Test\\1.pdf");
link.setAddress(f.getCanonicalPath());
cell.setHyperlink((org.apache.poi.ss.usermodel.Hyperlink) link);
It works fine and it adds a link Click Here to the cell
But how i can set a partial text and a link using same type of code, I mean the link need to be like your file is here, where only here is the link
As far as I know I don't think it is possible, as it is not supported by Excel as well. To achieve that in Excel too there is no straight way and you have to do some tricks to achieve that. Something that is not supported right from Excel cannot be supported by Apache POI too.