Apache POI setPrintArea to A4 page size - java

I'm working with the Apache POI API to generate .xls files, and I want to set the printArea of my file.
I know there is a funcion called setPrintArea() but this one receive as parameters start and end row and column of my print area.
I want to know, if there is a way to set my print area for each sheet with the size of an A4 paper (210mmx290mm)
Thanks.

For each Sheet in the workbook, get its PrintSetup object and call setPaperSize on it, passing the appropriate PrintSetup constant: A4_PAPERSIZE.
mySheet.getPrintSetup().setPaperSize(PrintSetup.A4_PAPERSIZE);

Related

messy decimal number when i click on excel cell using Apache poi java

The number in the excel was 1.6741, but when i click on cell it is showing as 1.6741000000000001. Several cases are like that, but curiously not all. Some are correctly showing.i found some solution which they are saying convert from double to string and print as it is, but this cell value using in other cell as formula so it will not work.
Apache POI: version No:4.0.1

Custom number format is gone after updating cells

I'm using Java API of Google Spreadsheets to modify the value of a cell in a spreadsheet. Doing it this way:
CellEntry cell = ...
cell.changeInputValueLocal(newInputValue);
cell.update();
My problem is that the cell has a custom number format (# ###0 "colli"), and it's gone after I write the cell programmatically. I want to write a number into the cell (200). The cell's number format is set to "Automatic" after this operation.
What am I doing wrong?

XLS to PDF conversion without losing formatting

I have a generic problem with a not-so-generic requirement.
I have to convert an Excel file into PDF with a catch that all the formatting of each cell in the excel file has to be retained as it is. No change allowed. Like if a cell is formatted as Currency/Accounting then by default negative values are displayed in round braces e.g.(8.5) but when read from Java the value is -8.5. For numeric cell, value would be 0 but is displayed as hyphen (-).
Similarly for rest of the formatting types, the display changes.
As the cell has different cell types and the actual value in the cell is displayed according to the formatting applied on the cell, how to copy it in output PDF file?
Latest I learnt that DataFormatter is much useful in my case. So I wrote below code
DataFormatter df = new DataFormatter();
Cell c = row.getCell(i);
CellStyle style = c.getCellStyle();
cellvalue = df.formatRawCellContents(row.getCell(i).getNumericCellValue(), style.getDataFormat(), style.getDataFormatString());
One of the cell has formatting ($* #,##0.00);($* (#,##0.00);($* "-"??);(#_) (0x2c). If the value in the cell is 0 then ideally it should display "-" but it displays as 0.0 only.
Any help would be appreciated.
First install the pdf software in your local system then run this code in excel vb application. Give the name as you wish... that's it..
Sub expf()
Application.ActivePrinter = "PDF Complete on PDFC"
ExecuteExcel4Macro "PRINT(1,,,1,,,,,,,,2,""PDF Complete on PDFC"",,TRUE,,FALSE)"
End Sub
Using POI jar we can read excel . After reading for each row you can create PDF files using PDFBox. Also based on your requirements apply the styles for the pdf.

Apache POI - Writing to Excel template without overwriting existing cell formatting

I am using Apache POI to write data to an Excel template. The template only contains headers on the first row, but I also applied specific styles to the entire column (e.g. Accounting and Percentage).
It would be nice to use these formats when I write data to the new cells. But if I use the createRow and createCell methods, the cell format is overwritten and I get General for all the cells. If I try to use getRow and getCell instead, I run into NullPointerExceptions retrieving the blank cell.
Is there a way to use the pre-existing cell formatting saved in the template? Or am I stuck setting the data format myself through the API?
Thanks for your help.
If you have applied specific styles to an entire column, then you can retrieve that CellStyle with Sheet's getColumnStyle method, passing it the 0-based column index. It retrieves a normal CellStyle object that can be used anywhere else CellStyles are accepted, such as in Cell's setCellStyle method.
For avoiding the NullPointerException, both getRow and getCell may return null if the row or cell doesn't exist, respectively. You will need to call createRow and/or createCell to create the Cell, on which you can always call setCellStyle.
cell.setCellStyle(sheet.getColumnStyle(index) works well.

Apache POI excel cell style update

Using Apache Poi I'm exporting my table to an excel sheet. I extended ExcelExport class, override the getCellStyle method and customized my cell styles.
Now what i want is when a user edits any cell in the excel document i want that cell to change its color, so anyone later reviewing the document can easily see what has been changed.
Is there way to achieve this?
One solution could be to add conditional formatting to the cells use Apache POI's HSSFSheetConditionalFormatting.
For example, if the cell value written by your program is 5 then your conditional formatting could set the cell background to yellow if the value wasn't equal to 5

Categories