How to set Cell Format using Apache POI? [duplicate] - java

I am creating excel sheet using apache poi. I have numbers like - 337499.939437217, which I want to show as it is in excel without rounding off. Also the cell format should be number (for some columns) and currency (for some columns).
Please suggest which BuiltinFormats should I use to achieve this.
Many thanks for the help.

At first you need to know how to use DataFormats. Then you need to know the guidelines for customizing a number format.
For your number -337499.939437217 which will be displayed rounded with general number format, you could use format #.###############. The # means a digit which will be displayed only if needed (is not leading zero and/or is not zero as last decimal digit) - see guidelines. So the whole format means show up to 15 decimal digits if needed but only as much as needed.
For currency you should really using a built in number format for currency. So the currency symbol depends on the locale settings of Excel. The following BuiltinFormats are usable with apache poi. Using a built in number format you need only the hexadecimal format numbers.
Example:
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CreateNumberFormats {
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("format sheet");
CellStyle style;
DataFormat format = wb.createDataFormat();
Row row;
Cell cell;
short rowNum = 0;
short colNum = 0;
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(-337499.939437217); // general format
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("#.###############")); // custom number format
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(-337499.939437217);
cell.setCellStyle(style);
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(123.456789012345);
cell.setCellStyle(style);
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(123456789.012345);
cell.setCellStyle(style);
style = wb.createCellStyle();
style.setDataFormat((short)0x7); // builtin currency format
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(-1234.5678);
cell.setCellStyle(style);
sheet.autoSizeColumn(0);
FileOutputStream fileOut = new FileOutputStream("CreateNumberFormats.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();
}
}

Related

How to create an excel sheet with text format?

I am using SXSSFWorkbook for creating excel file. I know how to create data format for excel cell like this:
DataFormat fmt = workbook.createDataFormat();
CellStyle cellStyleText = workbook.createCellStyle();
cellStyleText.setDataFormat(
fmt.getFormat("#"));
But this one is good when you are trying to write the datas into the cell and at the same time indicate its format. But I want to set ALL cells in excel to text format, NOT only the cells I am writing datas into. In default it creates sheet with general cell format. So I would like to know is there any tips for setting this cells into text format?
At first, having all cells using text data format in a spreadsheet is not really a good idea. In such a sheet no formulas will work. This contradicts the main usage of spreadsheets.
But your question is mainly about how to set a special default cell style for each cell in the sheet. In your case the special text style but that also could be any other style.
One solution would be using a template which already has that special style set for all cells. Then one could do something like this:
...
Workbook workbook = WorkbookFactory.create(new FileInputStream("./template.xlsx"));
SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook((XSSFWorkbook)workbook);
Sheet sxssfSheet = sxssfWorkbook.getSheetAt(0);
// stream in the data into the sheet
...
If you need doing this from scratch, then one could start having a new XSSFWorkbook. Then create the needed cell style and apply this for all cells.
The class ColumnHelper provides methods to set a default style to all cells in one given column. But it lacks methods to do this for all columns. So org.apache.poi.xssf.usermodel.helpers.ColumnHelper needs to be extended to provide that functionality.
Following complete example shows this:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
class CreateExcelDefaultColumnStyle {
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook();
DataFormat format = workbook.createDataFormat();
CellStyle textStyle = workbook.createCellStyle();
textStyle.setDataFormat(format.getFormat("#"));
Sheet sheet = workbook.createSheet();
MyColumnHelper columnHelper = new MyColumnHelper(((XSSFSheet)sheet).getCTWorksheet());
columnHelper.setColDefaultStyle(0, workbook.getSpreadsheetVersion().getLastColumnIndex(), textStyle);
SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook((XSSFWorkbook)workbook);
Sheet sxssfSheet = sxssfWorkbook.getSheetAt(0);
// stream in the data into the sheet
FileOutputStream fileout = new FileOutputStream("./Excel.xlsx");
sxssfWorkbook.write(fileout);
fileout.close();
sxssfWorkbook.close();
sxssfWorkbook.dispose();
}
}
class MyColumnHelper extends org.apache.poi.xssf.usermodel.helpers.ColumnHelper {
public MyColumnHelper(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet worksheet) {
super(worksheet);
}
public void setColDefaultStyle(long fromCol, long toCol, CellStyle style) {
setColDefaultStyle(fromCol, toCol, style.getIndex());
}
public void setColDefaultStyle(long fromCol, long toCol, int styleId) {
org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol col = getOrCreateColumn1Based(fromCol+1, true);
col.setMax(toCol+1);
col.setStyle(styleId);
}
}
After running this code Excel.xls contains one sheet having all cells using the cell style where data format "Text" is set. It is tested and works using current apache poi 5.2.2.

Can't set type for excel cell in java

I am using org.apache.poi for excel interaction through Java and I am using SXSSF (not any other format like HSSF etc.). Here is how I want to set type.
private void setTechSheet(SXSSFWorkbook workbook, String entityJson) {
SXSSFSheet tech = workbook.createSheet("tech");
SXSSFRow jwtRow = tech.createRow(0);
SXSSFRow SecondRow = tech.createRow(1);
jwtRow.createCell(1).setCellValue(entityJson);
}
If to put this in the 4th line of method:
jwtRow.createCell(1).setCellType(CellType.STRING).setCellValue(entityJson),
it won't let me set value. As I read from tutorial, the cell will be the type of datas I write (string in my example). But when I open excel and click on my cell it still shows general type. How can I solve this problem? I want the cell to be any type, but not general.
General is not the type but the data format. So you have a cell containing a text value in General data format. That is the default. There is nothing wrong with this.
The special Text data format only is needed when one needs to store numbers as text for example.
If you need Text data format, then setting # as data format is necessary. For this, the cell needs a cell style having that data format. And because cell styles are stored on workbook level and shared between all cells having that date format, each data format needs to be created only once per workbook and then applied to all cells which shall use this data format.
SXSSFWorkbook workbook...
...
DataFormat format = workbook.createDataFormat();
CellStyle textStyle = workbook.createCellStyle();
textStyle.setDataFormat(format.getFormat("#"));
...
setTechSheet(workbook, "1234", textStyle);
...
private void setTechSheet(SXSSFWorkbook workbook, String entityJson, CellStyle textStyle) {
SXSSFSheet tech = workbook.createSheet("tech");
SXSSFRow jwtRow = tech.createRow(0);
SXSSFCell cell = jwtRow.createCell(1);
cell.setCellValue(entityJson);
cell.setCellStyle(textStyle);
}
...
Following complete example shows when data format Text is needed.
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.*;
public class SXSSFTextDataFormat {
private static void setTechSheet(SXSSFWorkbook workbook, Object[][] techSheetData, CellStyle textStyle) {
SXSSFSheet tech = workbook.createSheet("tech");
int r = 0;
for (Object[] dataRow : techSheetData) {
SXSSFRow row = tech.createRow(r);
int c = 0;
for (int i = 0; i < dataRow.length; i+=2) {
SXSSFCell cell = row.createCell(c);
Object data = dataRow[i];
String format = (String)dataRow[i+1];
if (data instanceof Number) {
cell.setCellValue(((Number)data).doubleValue());
} else {
cell.setCellValue(String.valueOf(data));
}
if ("Text".equals(format)) {
cell.setCellStyle(textStyle);
}
c++;
}
r++;
}
}
public static void main(String args[]) throws Exception {
Object[][] techSheetData = new Object[][] {
new Object[] {"A1", "General", new java.math.BigInteger("12345678901234567890"), "General"},
new Object[] {"A2", "General", "12345678901234567890", "Text"},
new Object[] {"A3", "General", "12345678901234567890", "General"},
};
try ( SXSSFWorkbook workbook = new SXSSFWorkbook();
java.io.FileOutputStream out = new java.io.FileOutputStream("./Excel.xlsx")) {
DataFormat format = workbook.createDataFormat();
CellStyle textStyle = workbook.createCellStyle();
textStyle.setDataFormat(format.getFormat("#"));
setTechSheet(workbook, techSheetData, textStyle);
workbook.write(out);
workbook.dispose();
}
}
}
The big integer in B1will not be stored in Excel properly because Excel only stores numbers in double precision.
The big integer string in B2 is formatted using Text data format. So one can edit that cell in Excel's GUI without destroying the format.
The big integer string in B3 is formatted using General data format. So if one edits that cell in Excel's GUI this will destroy the format and Excel converts the number into a double value.

copy cells between Excel workbooks with Apache POI

I'm trying to copy from cells from one workbook to another with the latest version of Apache POI (4.1.2).
If both workbooks are .xlsx files, everything works fine. But if the source workbook is an (old) .xls file and the destination workbook is an .xlsx file, the following code fails
// Copy style from old cell and apply to new cell
CellStyle newCellStyle = targetWorkbook.createCellStyle();
newCellStyle.cloneStyleFrom(sourceCell.getCellStyle());
targetCell.setCellStyle(newCellStyle);
The exception that's thrown is:
java.lang.IllegalArgumentException: Can only clone from one XSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle
If we can't use cloneStyleFrom when the files (or Workbook objects) are of different types, how can we convert a HSSFCellStyle object to a XSSFCellStyle?
The answer to your question "How can we convert a HSSFCellStyle object to a XSSFCellStyle?" is: We can't do that using apache poi 4.1.2. This simply is not supported as clearly stated in CellStyle.cloneStyleFrom: "However, both of the CellStyles will need to be of the same type (HSSFCellStyle or XSSFCellStyle)."
The other question is: Should we at all convert one cell style into another? Or what use cases are there for CellStyle.cloneStyleFrom at all? In my opinion there are none. There are Excel limitations for the count of unique cell formats/cell styles. See Excel specifications and limits. So we should not create a single cell style for each single cell because then those limitations will be reached very fast. So instead of cloning cell styles we should get the style properties from source style style1 and then using CellUtil.setCellStyleProperties to set those style properties to the other cell in question. This method attempts to find an existing CellStyle that matches the cell's current style plus styles properties in properties. A new style only is created if the workbook does not contain a matching style.
Since your question title is "Copy cells between Excel workbooks with Apache POI", I have created a working draft of how I woud do this.
The following code first gets a existent Workbook.xls as HSSFWorkbook wb1 and creates a new XSSFWorkbook wb2. Then it loops over all cells of the first sheet of wb1 and tries copying those cells into the first sheet of wb2. To do so there is a method copyCells(Cell cell1, Cell cell2) which uses copyStyles(Cell cell1, Cell cell2). The latter gets the style properties from source style style1 got from cell1 and then uses CellUtil.setCellStyleProperties to set those style properties to cell2. For copying fonts copyFont(Font font1, Workbook wb2) is used. This tries creating new fonts in wb2 only if such a font is not already present in that workbook. This is necessary because there also is a limit of unique font types per workbook in Excel.
Working example:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.util.CellUtil;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.*;
class ExcelCopyCells {
static Font copyFont(Font font1, Workbook wb2) {
boolean isBold = font1.getBold();
short color = font1.getColor();
short fontHeight = font1.getFontHeight();
String fontName = font1.getFontName();
boolean isItalic = font1.getItalic();
boolean isStrikeout = font1.getStrikeout();
short typeOffset = font1.getTypeOffset();
byte underline = font1.getUnderline();
Font font2 = wb2.findFont(isBold, color, fontHeight, fontName, isItalic, isStrikeout, typeOffset, underline);
if (font2 == null) {
font2 = wb2.createFont();
font2.setBold(isBold);
font2.setColor(color);
font2.setFontHeight(fontHeight);
font2.setFontName(fontName);
font2.setItalic(isItalic);
font2.setStrikeout(isStrikeout);
font2.setTypeOffset(typeOffset);
font2.setUnderline(underline);
}
return font2;
}
static void copyStyles(Cell cell1, Cell cell2) {
CellStyle style1 = cell1.getCellStyle();
Map<String, Object> properties = new HashMap<String, Object>();
//CellUtil.DATA_FORMAT
short dataFormat1 = style1.getDataFormat();
if (BuiltinFormats.getBuiltinFormat(dataFormat1) == null) {
String formatString1 = style1.getDataFormatString();
DataFormat format2 = cell2.getSheet().getWorkbook().createDataFormat();
dataFormat1 = format2.getFormat(formatString1);
}
properties.put(CellUtil.DATA_FORMAT, dataFormat1);
//CellUtil.FILL_PATTERN
//CellUtil.FILL_FOREGROUND_COLOR
FillPatternType fillPattern = style1.getFillPattern();
short fillForegroundColor = style1.getFillForegroundColor(); //gets only indexed colors, no custom HSSF or XSSF colors
properties.put(CellUtil.FILL_PATTERN, fillPattern);
properties.put(CellUtil.FILL_FOREGROUND_COLOR, fillForegroundColor);
//CellUtil.FONT
Font font1 = cell1.getSheet().getWorkbook().getFontAt(style1.getFontIndexAsInt());
Font font2 = copyFont(font1, cell2.getSheet().getWorkbook());
properties.put(CellUtil.FONT, font2.getIndexAsInt());
//BORDERS
BorderStyle borderStyle = null;
short borderColor = -1;
//CellUtil.BORDER_LEFT
//CellUtil.LEFT_BORDER_COLOR
borderStyle = style1.getBorderLeft();
properties.put(CellUtil.BORDER_LEFT, borderStyle);
borderColor = style1.getLeftBorderColor();
properties.put(CellUtil.LEFT_BORDER_COLOR, borderColor);
//CellUtil.BORDER_RIGHT
//CellUtil.RIGHT_BORDER_COLOR
borderStyle = style1.getBorderRight();
properties.put(CellUtil.BORDER_RIGHT, borderStyle);
borderColor = style1.getRightBorderColor();
properties.put(CellUtil.RIGHT_BORDER_COLOR, borderColor);
//CellUtil.BORDER_TOP
//CellUtil.TOP_BORDER_COLOR
borderStyle = style1.getBorderTop();
properties.put(CellUtil.BORDER_TOP, borderStyle);
borderColor = style1.getTopBorderColor();
properties.put(CellUtil.TOP_BORDER_COLOR, borderColor);
//CellUtil.BORDER_BOTTOM
//CellUtil.BOTTOM_BORDER_COLOR
borderStyle = style1.getBorderBottom();
properties.put(CellUtil.BORDER_BOTTOM, borderStyle);
borderColor = style1.getBottomBorderColor();
properties.put(CellUtil.BOTTOM_BORDER_COLOR, borderColor);
CellUtil.setCellStyleProperties(cell2, properties);
}
static void copyCells(Cell cell1, Cell cell2) {
switch (cell1.getCellType()) {
case STRING:
/*
//TODO: copy HSSFRichTextString to XSSFRichTextString
RichTextString rtString1 = cell1.getRichStringCellValue();
cell2.setCellValue(rtString1); // this fails if cell2 is XSSF and rtString1 is HSSF
*/
String string1 = cell1.getStringCellValue();
cell2.setCellValue(string1);
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell1)) {
Date date1 = cell1.getDateCellValue();
cell2.setCellValue(date1);
} else {
double cellValue1 = cell1.getNumericCellValue();
cell2.setCellValue(cellValue1);
}
break;
case FORMULA:
String formula1 = cell1.getCellFormula();
cell2.setCellFormula(formula1);
break;
//case : //TODO: further cell types
}
copyStyles(cell1, cell2);
}
public static void main(String[] args) throws Exception {
Workbook wb1 = WorkbookFactory.create(new FileInputStream("Workbook.xls"));
Workbook wb2 = new XSSFWorkbook();
Sheet sheet1 = wb1.getSheetAt(0);
Sheet sheet2 = wb2.createSheet();
Set<Integer> columns = new HashSet<Integer>();
Row row2 = null;
Cell cell2 = null;
for (Row row1 : sheet1) {
row2 = sheet2.createRow(row1.getRowNum());
for (Cell cell1 : row1) {
columns.add(cell1.getColumnIndex());
cell2 = row2.createCell(cell1.getColumnIndex());
copyCells(cell1, cell2);
}
}
wb1.close();
for (Integer column : columns) {
sheet2.autoSizeColumn(column);
}
FileOutputStream out = new FileOutputStream("Workbook.xlsx");
wb2.write(out);
out.close();
wb2.close();
}
}
If Workbook.xls looks like this:
then the resulting Workbook.xlsx looks like this:
Note: This is a working draft and needs to be completed. See TODO comments in the code. RichTextString cell values needs to be considered. Further cell types needs to be considered.
Method copyStyles only provides copying data format, fill pattern and fill foreground color (only for indexed colors), font and borders. Further cell style properties needs to be considered.

Apache-poi decimal formatting is being applied only after selection

Could you please suggest on next case?
I`ve set up dataformat and cell type ("#,###.00", NUMERIC)
(I want thounsand separator plus two decimal numbers)
It works as I expected but to have formatted cells I need to select them first
Before selection data looks not formatted
In other words I have to select cell so that it is formatted, otherwise it stays without any formatting
CellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.RIGHT);
style.setLocked(locked);
style.setDataFormat(workbook.createDataFormat().getFormat("#,###.00"));
cell.setCellStyle(style);
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(<big decimal value>.toString());
Simply do not set cell value as string if you need a numeric cell value. If you set cell value as String, then the cell type also will be string. This is independent of setting CellType before setting cell value. While setting a String cell value the type changes to string always.
See API documentation which shows that Cell.setCellType is deprecated and what Cell.setCellValue methods are possible.
You needs setting a double cell value if cell shall have numeric content.
Example:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.math.BigDecimal;
import java.util.Random;
class CreateExcelCellNumberFormat {
public static void main(String[] args) throws Exception {
try (Workbook workbook = new XSSFWorkbook();
FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {
CellStyle style = workbook.createCellStyle();
style.setDataFormat(workbook.createDataFormat().getFormat("#,###.00"));
Sheet sheet = workbook.createSheet();
for (int r = 0; r < 10; r++) {
Cell cell = sheet.createRow(r).createCell(0);
cell.setCellStyle(style);
BigDecimal bigDecimal = new BigDecimal(new Random().nextDouble() * 10000000000000d);
cell.setCellValue(bigDecimal.doubleValue());
}
sheet.setColumnWidth(0, 25 * 256);
workbook.write(fileout);
}
}
}

Currency format for Indian(INR) and US(dollar) while exporting data using Apache POI doesn't work

I am exporting data using Apache POI and the data has both Indian and US currency to be displayed..but after applying separate DataFormats too I get the same format(Indian INR) applied to all data's.
public static void main(String[] args) {
try {
System.out.println("*******Export Excel*********");
File myFile = new File("MyFirstExcel.xls");
FileOutputStream out = new FileOutputStream(myFile);
XSSFWorkbook xssfworkbook = new XSSFWorkbook();
XSSFSheet sheet = xssfworkbook.createSheet("new sheet");
XSSFCellStyle INRformatStyle = xssfworkbook.createCellStyle();
XSSFCellStyle USformatStyle = xssfworkbook.createCellStyle();
XSSFDataFormat df = xssfworkbook.createDataFormat();
USformatStyle.setDataFormat(df.getFormat("[$$-409]#,##0.00;"));
INRformatStyle.setDataFormat(df.getFormat("₹#,##0.00;"));
sheet.setColumnWidth(0, 7000);
XSSFRow row = sheet.createRow((short) 0);
XSSFCell cell = row.createCell((short) 0);
cell.setCellValue(100000.0);
cell.setCellStyle(USformatStyle);
XSSFRow row1 = sheet.createRow((short) 1);
XSSFCell cell1 = row1.createCell((short) 0);
cell1.setCellValue(100000.0);
cell1.setCellStyle(INRformatStyle);
xssfworkbook.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Expected $100,000.00
₹1,00,000.00
Actual $1,00,000.00
₹1,00,000.00
How the digit grouping is done in Excel is determined on the Windows system regional settings: Change the Windows regional settings. There is actually not a native option to set this via number format. So as long your system tells Excel: "We wants using format Hindi(India)" and so the digit grouping is set to 12,34,56,789 then this will be so for each number having thousands separator set.
Only possibility avoiding this is to set used system regional settings not to format Hindi(India) and so to set digit grouping not to 12,34,56,789.
Or using kind of fake number format as [$$-409]#\,###\,##0.00.
...
USformatStyle.setDataFormat(df.getFormat("[$$-409]#\\,###\\,##0.00"));
...
This is not using comma as thousands separator but , as text within the digits flow. So system's digit grouping is bypassed.
Complete example:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
class CreateExcelDigitGroupings {
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook();
DataFormat dataformat = workbook.createDataFormat();
CellStyle defaultCurrency = workbook.createCellStyle();
defaultCurrency.setDataFormat((short)8); //see https://poi.apache.org/apidocs/dev/org/apache/poi/ss/usermodel/BuiltinFormats.html
CellStyle defaultUSDCurrency = workbook.createCellStyle();
defaultUSDCurrency.setDataFormat(dataformat.getFormat("[$$-409]#,##0.00"));
CellStyle fakeHindiCurrency = workbook.createCellStyle();
fakeHindiCurrency.setDataFormat(dataformat.getFormat("₹#\\,##\\,##\\,##0.00"));
CellStyle fakeUSDCurrency = workbook.createCellStyle();
fakeUSDCurrency.setDataFormat(dataformat.getFormat("$#\\,###\\,##0.00"));
Sheet sheet = workbook.createSheet("Sheet1");
double value = 12345678.9;
Cell cell;
int r = 0;
sheet.createRow(r).createCell(0).setCellValue("defaultCurrency");
cell = sheet.getRow(r++).createCell(1);
cell.setCellValue(value);
cell.setCellStyle(defaultCurrency);
sheet.createRow(r).createCell(0).setCellValue("defaultUSDCurrency");
cell = sheet.getRow(r++).createCell(1);
cell.setCellValue(value);
cell.setCellStyle(defaultUSDCurrency);
sheet.createRow(r).createCell(0).setCellValue("fakeHindiCurrency");
cell = sheet.getRow(r++).createCell(1);
cell.setCellValue(value);
cell.setCellStyle(fakeHindiCurrency);
sheet.createRow(r).createCell(0).setCellValue("fakeUSDCurrency");
cell = sheet.getRow(r++).createCell(1);
cell.setCellValue(value);
cell.setCellStyle(fakeUSDCurrency);
sheet.autoSizeColumn(0);
sheet.autoSizeColumn(1);
FileOutputStream out = new FileOutputStream("CreateExcelDigitGroupings.xlsx");
workbook.write(out);
workbook.close();
out.close();
}
}
Result in Excel having Format Hindi(India) set in Windows regional settings and so default digit grouping is set to 12,34,56,789:
As you see for all default number formats the system's digit grouping is used. But my fake USD format works.
Result in OpenOffice 3.2.1 Ubuntu 18.04.1 LTS.
Please try with the following modifications to the currency formats
USformatStyle.setDataFormat(df.getFormat("$#,##0.00"));
INRformatStyle.setDataFormat(df.getFormat("₹#,##0.00"));
Finally found that Libreoffice Calc takes the regional language formatting(by default I get indian formatting)..works fine in MS excel.

Categories

Resources