How to create an excel sheet with text format? - java

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.

Related

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

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();
}
}

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.

Can't see styling changes in POI Apache Excel .xls document

I wonder how to autoSize the columns in Excel doc.
When I run this code it don't do a jack shit in the document. And I can't really find out what is wrong!
Literally, nothing is autoSized in the document. I don't understand what could be wrong!! Very frustrating problem..
Also, I would be happy to get some feedback on the code, do I practice bad coding habits?
Thanks!
Here is my code:
try
{
FileInputStream myxls = new FileInputStream("/Users/xxxxxx/Desktop/tryIt.xls");
HSSFWorkbook workbook = new HSSFWorkbook(myxls);
HSSFSheet sheet = workbook.getSheetAt(0);
int lastRow=sheet.getLastRowNum();
HSSFCellStyle styleRowHeading = workbook.createCellStyle();
HSSFCellStyle style = workbook.createCellStyle();
HSSFFont fontRowHeading = workbook.createFont();
HSSFFont font = workbook.createFont();
fontRowHeading.setBold(true);
fontRowHeading.setFontName(HSSFFont.FONT_ARIAL);
fontRowHeading.setFontHeightInPoints((short) 14);
styleRowHeading.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
styleRowHeading.setFillPattern(FillPatternType.SOLID_FOREGROUND);
styleRowHeading.setBorderTop(BorderStyle.MEDIUM);
styleRowHeading.setBorderBottom(BorderStyle.MEDIUM);
styleRowHeading.setBorderLeft(BorderStyle.MEDIUM);
styleRowHeading.setBorderRight(BorderStyle.MEDIUM);
styleRowHeading.setFont(fontRowHeading);
font.setFontName(HSSFFont.FONT_ARIAL);
font.setFontHeightInPoints((short)12);
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setBorderTop(BorderStyle.MEDIUM);
style.setBorderBottom(BorderStyle.MEDIUM);
style.setBorderLeft(BorderStyle.MEDIUM);
style.setBorderRight(BorderStyle.MEDIUM);
style.setFont(font);
// Create heading
if(lastRow <=0){
Row rowHeading = sheet.createRow(lastRow);
rowHeading.createCell(0).setCellValue("TEST1");
rowHeading.createCell(1).setCellValue("TEST2");
rowHeading.createCell(2).setCellValue("TEST3");
rowHeading.createCell(3).setCellValue("TEST4");
for(int i = 0; i < 4; i++){
rowHeading.getCell(i).setCellStyle(styleRowHeading);
}
}
Row row = sheet.createRow(++lastRow);
int i = 0;
org.apache.poi.ss.usermodel.Cell cellId = row.createCell(i);
org.apache.poi.ss.usermodel.Cell cellId1 = row.createCell(i+=1);
org.apache.poi.ss.usermodel.Cell cellId2 = row.createCell(i+=1);
org.apache.poi.ss.usermodel.Cell cellId3 = row.createCell(i+=1);
cellId.setCellValue(todaysDate);
cellId1.setCellValue(txt_year.getText());
cellId2.setCellValue(txt_correct.getText());
cellId3.setCellValue(txt_errors.getText());
cellId.setCellStyle(style);
cellId1.setCellStyle(style);
cellId2.setCellStyle(style);
cellId3.setCellStyle(style);
// Autofit
for(int w = 0; w < 5; w++){
sheet.autoSizeColumn(w);
}
myxls.close();
FileOutputStream output_file =new FileOutputStream(new File("/Users/xxxx/Desktop/tryIt.xls"));
//write changes
workbook.write(output_file);
output_file.close();
System.out.println("SUCCESSSSSSSSS!");
}catch(Exception e){
System.out.println(e.getMessage());
}
I assume HSSFCellStyle might be causing issues here, could you change to CellStyle and check once if you see any formatting changes:
CellStyle style=null;
XSSFFont defaultFont= wb.createFont();
defaultFont.setFontHeightInPoints((short)10);
defaultFont.setFontName("Arial");
defaultFont.setColor(IndexedColors.BLACK.getIndex());
defaultFont.setBold(false);
defaultFont.setItalic(false);
XSSFFont font= wb.createFont();
font.setFontHeightInPoints((short)10);
font.setFontName("Arial");
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
font.setItalic(false);
style=row.getRowStyle();
style.setFillBackgroundColor(IndexedColors.DARK_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFont(font);
Key things to keep in mind:
Let's understand the basic difference between HSSFWorkbook and XSSFWorkbook
HSSFWorkbook: This class has methods to read and write Microsoft Excel files in .xls format.
XSSFWorkbook: This class has methods to read and write Microsoft Excel and OpenOffice xml files in .xls or .xlsx format.
SXSSF: it is an API-compatible streaming extension of XSSF to be used when very large spreadsheets have to be produced, and heap space is limited
Workbook
This is the super-interface of all classes that create or maintain
Excel workbooks. It belongs to the org.apache.poi.ss.usermodel package
and both the above mentioned XSSF, HSSF and SXSSF are implementations of
WORKBOOK
Hence, my suggestion would be to until-unless utmost necessary, i.e, you need a specific feature for xlsx or xls, just go with the workbook implementation
Most of the styling changes are hit and trial. You need to keep
digging iterating with to finally find what you need.
Suggestions:
If you code for just HSSF via HSSFWorkbook, you can only work with .xls files. I'd suggest you go for the common ones wherever possible (workbook)
Your loading code should be something like:
Workbook wb = WorkbookFactory.create(new File("test.xls"));
Sheet s = wb.getSheetAt(0);
....
Now, it will auto-detect the type of the file and give you back a working object for either .xls or .xlsx based on what it finds. Also, wherever possible try to keep the styling and designing parts generic and version independent. That way the same code could be re-used for both formats.
If you need to have any specific feature which require either XSSF
or HSSF and can't use just the Workbook then do a check for the
type first like this:
Workbook wb = WorkbookFactory.create(myExcelFile);
Then you can check the exact type created by the factory:
if (wb instanceof HSSFWorkbook) {
// do whatever
} else if (wb instanceof SXSSFWorkbook) {
// do whatever
} else if (wb instanceof XSSFWorkbook) {
// do whatever
}

Adding Custom colours to Excel sheet using Apache POI

Can anyone explain how to add Custom colours either using (rgb values or hex values ) to an excelsheet sheet(either in foreground or background) using Cellstyle in Apche poi to a Excelsheet(XSSF Workbook)?
Setting custom colors depends on the kind of Excel file (Office Open XML format *.xlsx vs. BIFF format *.xls). And it might be different using different versions of apache poi because of deprecation.
Using Office Open XML format *.xlsx we can simply set new colors using constructor of XSSFColor. In apache poi 4.0.0 XSSFColor(byte[] rgb, IndexedColorMap colorMap) can be used. IndexedColorMap can be null if no additional color map shall be used instead of the default one.
Using BIFF format *.xls only indexed colors are usable. But temporary overwriting some of the indexed colors is possible.
Following code shows both used for setting a cells's fill color. The used custom color is RGB(112,134,156). Using HSSF(BIFF format *.xls) the indexed color HSSFColor.HSSFColorPredefined.LIME will be temporary overwritten.
Note, the following is tested and works using apache poi 4.0.0. No guarantee using other versions.
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
public class CreateExcelCustomColor {
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook();
//Workbook workbook = new HSSFWorkbook();
CellStyle cellcolorstyle = workbook.createCellStyle();
cellcolorstyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
byte[] rgb = new byte[]{(byte)112, (byte)134, (byte)156};
if (cellcolorstyle instanceof XSSFCellStyle) {
XSSFCellStyle xssfcellcolorstyle = (XSSFCellStyle)cellcolorstyle;
xssfcellcolorstyle.setFillForegroundColor(new XSSFColor(rgb, null));
} else if (cellcolorstyle instanceof HSSFCellStyle) {
cellcolorstyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.LIME.getIndex());
HSSFWorkbook hssfworkbook = (HSSFWorkbook)workbook;
HSSFPalette palette = hssfworkbook.getCustomPalette();
palette.setColorAtIndex(HSSFColor.HSSFColorPredefined.LIME.getIndex(), rgb[0], rgb[1], rgb[2]);
}
Sheet sheet = workbook.createSheet();
Cell cell = sheet.createRow(0).createCell(0);
cell.setCellStyle(cellcolorstyle);
FileOutputStream out = null;
if (workbook instanceof XSSFWorkbook) {
out = new FileOutputStream("CreateExcelCustomColor.xlsx");
} else if (workbook instanceof HSSFWorkbook) {
out = new FileOutputStream("CreateExcelCustomColor.xls");
}
workbook.write(out);
out.close();
workbook.close();
}
}

Changing template when applying cell style in xls file using apache poi

I have a problem with HSSFWorkbook object. I have a XLS template, where row no 14 is light gray, and next rows are populated with bean values.
During generation of the report, I want to change the date style for which I am writing below code but it's changing the whole tempalate.
workbook = (HSSFWorkbook) WorkbookFactory.create(new FileInputStream(finalFilePath));
sheet = workbook.getSheetAt(0);
workbook.setActiveSheet(0);
CreationHelper creationHelper = workbook.getCreationHelper();
HSSFCellStyle importDataDateStyle = workbook.createCellStyle();
importDataDateStyle.setDataFormat(creationHelper.createDataFormat().getFormat("M/d/yyyy"));
BeanClass bean = new BeanClass();
if (bean.getLastDate() != null) {
migrationBeanRow.createCell(6).setCellValue(lastDate);
} else {
migrationBeanRow.createCell(6).setCellValue(notAvailable);
}
migrationBeanRow.getCell(6).setCellStyle(cellStyleDate);
workbook.write(fileOut);
workbook.close();
This code is working correctly for xlsx(XSSFWorkbook) using XSSFStyle format but changing template for when trying for xls(HSSFWorkbook) file.
Pleas find below template snippet before and after file generation.
pre code template
post code template(Date format is coming as expected but template header color is changing from gray to green)

Categories

Resources