Apache POI : Add images to the header of excel files - java

I am using Apache POI 4.1 to generate Excel reports. I have a requirement to include logo image as Header event in Sheet. I could find the only below code, which adds text in Header. Is there any way to include image in the Header section of Sheet?
SXSSFWorkbook workBook = new SXSSFWorkbook(100);
SXSSFSheet sheet = workBook.createSheet("Sample Sheet");
Header header= sheet.getHeader();
header.setCenter("Header");

Related

Keeping Pivot Table and Formating intact in JExcel

I am trying to write data to an excel sheet using JExcel. I have an excel file placed on disk and I opened it using JExcel and then created a Writable Workbook.
The Excel workbook contains sheets having pivot table. Idea is to just populate source sheets and then refresh the pivot tables. But after creating Writable Workbook, the formatting and pivot tables got away.
Here is my code snippet:
FileInputStream fileInputStream = new FileInputStream("C:\\Pivot_File_template.xls");
workbook = Workbook.getWorkbook(fileInputStream);
WorkbookSettings workbookSettings = new WorkbookSettings();
writableWorkbook = Workbook.createWorkbook(new File("C:\\Writable_Pivot_File.xls"),workbook,workbookSettings);
The file "Writable_Pivot_File.xls" just lost the orignal formatting and pivot tables. Any suggestions what I can do to maintain all the formatting and pivot tables? I am using "jxl_2.6.9.jar". Thanks in advance.
Just got away with this issue by using Apache POI. JExcel doesnot support xlsx and updating an xls file having pivot table using JExcel will disable the Pivot table and change its formatting.

Apache poi excel link to other page/sheet

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

Apache POI encoding

I use Apache POI 3.13 to generate excel in cyrillic.
HSSFSheet sheet = workbook.createSheet("Лист");
I also tried:
HSSFSheet sheet = workbook.createSheet(
new String("Лист".getBytes(Charset.forName("UTF-8")))
);
But i get this error:
Use XSSFSheet instead of HSSHSheet like this.
XSSFSheet sheet = workbook.createSheet("Лист");
For this use poi-ooxml 3.9.
When saving the java file, there will be a alert message. Click Save as UTF-8.
Here is the image of alert message: screenshot

POI 3.2 Excel file Data has been lost

I'm generating an Excel file in Java with POI 3.2 (Latest version I can use for my client).
Here is my code. As you can see I'm using HSSF because I need to make a XLS file.
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Reporting");
sheet.setColumnWidth(250,250);
HSSFRow Row;
HSSFCell Cell;
//Content part (doesn't matter)
IWDResource resource = WDResourceFactory.createCachedResource(
wb.getBytes(),
"workbook.xls",
WDWebResourceType.XLS);
wdContext.currentContextElement().setXls(resource);
Now after I've downloaded the XLS file, I want to open it but the file seems to be corrupt.
Image on link: http://tinyurl.com/nop52sh
When I'm pressing 2 times on 'Don't send', de excell file opens in correct form.
Any idea why?
Don't call wb.getBytes(), it doesn't do what you want. From the getBytes() javadoc
Method getBytes - get the bytes of just the HSSF portions of the XLS file. Use this to construct a POI POIFSFileSystem yourself.
Instead, if you want the overall xls file as a byte array, do
ByteArrayOutStream baos = new ByteArrayOutStream();
wb.write(baos);
byte[] xlsBytes = baos.toByteArray();
Finally, Apache POI 3.2 is ancient, over 5 years old now! You really ought to upgrade, see the changelog for an idea of all the bugs fixed since then

single cell read only in excel using java

How can I make read only cell in Excel using Java ?
You may use Apache POI library to achieve this. Here is a simple code sample:
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Test");
Row row = sheet.createRow(0);
CellStyle style = wb.createCellStyle();
style.setLocked(true);
cell = row.createCell(0);
cell.setCellStyle(style);
// this is important as locking is pnly activated if sheet is protected
sheet.protectSheet("");
Maybe you can try jExcel. It is quite simple and easy to use.
Take a look at the Apache POI project, specifically the HSSF & XSSF subprojects, that provides a Java library to manipulate Excel documents.

Categories

Resources