Set thousand separator Apache POI - apache-poi

I'm triying to format the number on my report with Apache poi but it is not working, I tried :
c.getCellStyle().setDataFormat(HSSFDataFormat.getBuiltinFormat("#.##0,00"));
and
c.getCellStyle().setDataFormat((short)BuiltinFormats.getBuiltinFormat("#.##0,00"));
But they are not working,
I'd like to show it as 1.000,99 but it is showwing 1000,99
I've only found the custom option but not the numeric, is it possible to use it ?do you know how to do it work?
Thanks

I got it. This is my solution :
DataFormat format = workbook.createDataFormat();
CellStyle style = workbook.createCellStyle();
style.setDataFormat(format.getFormat("#,##0.00"));
...
Cell c = row.createCell(cellDynamicNum);
c.setCellStyle(style);

Related

Excel or POI changes my date format to something that doesn't work

I've got an Excel file I want to recreate through POI. The existing Excel file uses as DataFormat _(* #.##0_);_(* (#.##0);_(* "-"??_);_(#_). But when I assign that data format through POI, the resulting Excel file will instead use _(* #,##0_);_(* (#,##0);_(* "-"??_);_(#_). Note the tiny difference: a dot changed to a comma. Because of this, the entire format doesn't work anymore. It's not like it's now showing a comma where it used to have a dot; it's formatting the entire value in a completely different way.
Why does this happen? And how do I fix it?
The correct format string _(* #.##0_);_(* (#.##0);_(* "-"??_);_(#_) results in the number being displayed as 13.534.000.
The incorrect format string that Excel or POI changes it to, _(* #,##0_);_(* (#,##0);_(* "-"??_);_(#_) formats the value as 13534000,0.
It's a complete mystery to me why it would do that. I suppose it has something to do with the US and Europe using different formats to display big numbers, but I would imagine that that's exactly what this data format is supposed to address. Instead, it turns it into nonsense.
Apache POI creates Microsoft Office files. Those files never are localized. They always store data in en_US locale. The locale dependent adjustments are done in locale Office applications then. So a Microsoft Office file can be sent around the world without the need to change the stored data to a foreign locale.
So if you set the data format using ...
...
Workbook workbook = new XSSFWorkbook();
DataFormat dataformat = workbook.createDataFormat();
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(dataformat.getFormat("_(* #,##0_);_(* (#,##0);_(* \"-\"??_);_(#_)"));
...
... the format pattern always needs to be en_US. That means dot is decimal separator, comma is thousands delimiter. A locale Excel application might adjust that to _(* #.##0_);_(* (#.##0);_(* "-"??_);_(#_) then.
Let's have a complete example:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CreateExcelNumberFormat {
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook();
DataFormat dataformat = workbook.createDataFormat();
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(dataformat.getFormat("_(* #,##0_);_(* (#,##0);_(* \"-\"??_);_(#_)"));
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue(1234567.89);
cell.setCellStyle(cellStyle);
row = sheet.createRow(1);
cell = row.createCell(0);
cell.setCellValue(-1234567.89);
cell.setCellStyle(cellStyle);
sheet.setColumnWidth(0, 15*256);
FileOutputStream out = new FileOutputStream("./CreateExcelNumberFormat.xlsx");
workbook.write(out);
out.close();
workbook.close();
}
}
The result Excel file looks in my German Excel like so:

In C#, Excel cell's custom date format not working with EPPlus

I'm having trouble with Excel's custom format using EPPLUS. Here's my code:
var destFile = new FileInfo(#"C:\temp\test1.xlsx");
var fileName = "test1";
using (ExcelPackage pck = new ExcelPackage(destFile))
{
pck.Workbook.Worksheets.Add(fileName); // Create the worksheet in package
pck.Workbook.Worksheets[fileName].Cells["A2"].Value = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");
pck.Workbook.Worksheets[fileName].Cells["A2"].Style.Numberformat.Format = "d-mmm-yy";
pck.Save();
}
I'm getting the following:
The custom format is showed right, but the value in the cell doesn't display the format needed. Here's what I'm trying to get:
Note: I need the full date value DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") for other files, but the custom format is all I need for this file.
What do I need to change to get this to work?
Thanks to #PaullAbbott, here's the correct answer:
pck.Workbook.Worksheets[fileName].Cells["A2"].Value = DateTime.Now;
This displays the results that I needed.

How to display date in UltraWinGrid SummaryRow in MM-dd-yyyy?

I am using UltraWinGrid and displaying the sum of the values using the SummaryRow. I need to display the maximum date in the summary row in MM-dd-yyyy format. Only date should be there. How to achieve this. Thanks in advance...
I got the answer form the site
http://www.csharp-examples.net/string-format-datetime/
SummarySettings SumSummary = new SummarySettings();
e.Layout.Bands[0].Columns["DateColumn"].AllowRowSummaries = AllowRowSummaries.False;
SumSummary = e.Layout.Bands[0].Summaries.Add(SummaryType.Minimum, e.Layout.Bands[0].Columns["DateColumn"]);
SumSummary.DisplayFormat = "{0:dd-MM-yyyy}";

Does CELL_TYPE_BLANK works for hssf or xls files?

I wanna ask that does CELL_TYPE_BLANK works for hssf or xls file system? I knw it works for xlsx or XSSF but i'm having problem with xls?
Yes it does. But take note that CELL_TYPE_BLANK is no null. You should always check first if the cell is null before identifying the cell type.
If you want complete certainty then you can use this code:
for(Row blankRow : sheet)
{
for(int cn = 0; cn < blankRow.getLastCellNum(); cn++)
{
Cell cells = blankRow.getCell(cn, Row.CREATE_NULL_AS_BLANK);
}
}
It marks all the null spaces as blank and then the CELL_TYPE_BLANK works like it should be working.

setting cell style not working

i'm working with apache poi and XLSX file. i use xssf classes to dynamically create a spreadsheet.
i'd like to set cell's style in a for loop, but it doesn't seem to work...here's my code :
for(int i=1;i<=gc.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);i++,gc.add(GregorianCalendar.DATE, 1),righe++){
Row r = foglio.createRow(righe);
if(getDayOfWeek(gc)== 6 || getDayOfWeek(gc) == 7){
XSSFCellStyle cs1 = wb.createCellStyle();
cs1.setFillBackgroundColor(IndexedColors.YELLOW.getIndex());
cs1.setFillPattern(CellStyle.SOLID_FOREGROUND);
XSSFFont f = wb.createFont();
f.setBold(true);
f.setColor(IndexedColors.RED.getIndex());
cs1.setFont(f);
Cell c1 = r.createCell(0);
c1.setCellValue(cost.getGiorni().get(getDayOfWeek(gc)-1).getNomeGiorno());
c1.setCellStyle(cs1);
Cell c2 = r.createCell(1);
c2.setCellValue(i);
c2.setCellStyle(cs1);
}
r.createCell(0).setCellValue(cost.getGiorni().get(getDayOfWeek(gc)-1).getNomeGiorno());
r.createCell(1).setCellValue(i);
...this i just a portion of the code...
i can't understand why is not working. Seems like the cellstyle is ignored or overwrited....
any clue ?
CellStyles are per-workbook, and there's a hard limit that Excel imposes on the numbers that a file is allowed to have, so you need to make sure you create the cell style once outside the loop.
Your code would then look something like:
XSSFCellStyle cs1 = wb.createCellStyle();
cs1.setFillBackgroundColor(IndexedColors.YELLOW.getIndex());
cs1.setFillPattern(CellStyle.SOLID_FOREGROUND);
XSSFFont f = wb.createFont();
f.setBold(true);
f.setColor(IndexedColors.RED.getIndex());
cs1.setFont(f);
for(int i=1;i<=gc.getActualMaximum(GregorianCalendar.DAY_OF_MONTH) i++,gc.add(GregorianCalendar.DATE, 1),righe++){
Row r = foglio.createRow(righe);
if(getDayOfWeek(gc)== 6 || getDayOfWeek(gc) == 7){
Cell c1 = r.createCell(0);
c1.setCellValue(cost.getGiorni().get(getDayOfWeek(gc)-1).getNomeGiorno());
c1.setCellStyle(cs1);
Cell c2 = r.createCell(1);
c2.setCellValue(i);
c2.setCellStyle(cs1);
}
}
If you're having issues with the styling not looking quite as you expect, the best option is to style a cell as you want it in Excel, save the file, read that into POI, and review the cell style that Excel wrote. Sometimes, Excel can do some strange things that take some getting used to, so check what it does to work out what you need to be doing!
You can use Following method, perhaps this will resolve your problem.
public static void setCellColorAndFontColor(XSSFCell cell, IndexedColors FGcolor, IndexedColors FontColor ){
XSSFWorkbook wb = cell.getRow().getSheet().getWorkbook();
CellStyle style = wb.createCellStyle();
XSSFFont font = wb.createFont();
font.setBold(true);
font.setColor(FontColor.getIndex());
style.setFont(font);
style.setFillForegroundColor(FGcolor.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell.setCellStyle(style);
}
When you are calling this method the way should be like
setCellColorAndFontColor(cell, IndexedColors.BLACK, IndexedColors.WHITE);
will create bold & white font text color with black cell background color in the sheet.
I think the
.setFillPattern(CellStyle.SOLID_FOREGROUND);
got changed to
.setFillPattern(FillPatternType.SOLID_FOREGROUND);
this works for me:
//design settings for header row
Font headerFont = wb.createFont();
headerFont.setColor(IndexedColors.WHITE.getIndex());
CellStyle headerCellStyle = wb.createCellStyle();
headerCellStyle.setFont(headerFont);
headerCellStyle.setAlignment(HorizontalAlignment.CENTER);
headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
headerCellStyle.setFillForegroundColor(IndexedColors.BLACK.getIndex());
headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

Resources