0

My input file is .xls from where I have to read data, manipulate and write back to .xlsx file along with the styles.

So, using NPOI HSSF to read from .xls and NPOI XSSF to generate the .xlsx file. I am done with the data. But I have to copy the cell formats from the .xls and apply to the output file.

When I write outputheaderStyle.CloneStyleFrom(inputheaderStyle); an exception occurs as inputheaderStyle is of type HSSFCellStyle and outputheaderStyle is of type XSSFCellStyle

Can only clone from one XSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle

outputheaderStyle.CloneStyleFrom((XSSFCellStyle)inputheaderStyle);

throws exception

Unable to cast object of type 'NPOI.HSSF.UserModel.HSSFCellStyle' to type 'NPOI.XSSF.UserModel.XSSFCellStyle'

Is there any other way to copy the style?

Sobhan
  • 796
  • 1
  • 9
  • 31
  • i not 100% sure, but I think whith NPOI you can hanlde xls files and xslx files. Looks like you got the two of the mixed up – lordkain Jul 28 '16 at 12:37
  • I know I mixed up both. But, its customer requirement that input is always in .xls format and output must be in .xlsx format with all the style – Sobhan Jul 28 '16 at 12:39
  • oke, I read the question wrong. customers and there requirements :) – lordkain Jul 28 '16 at 12:44

1 Answers1

0

Well, the cell style of .xls file is HSSFCellStyle and for .xlsx file it is XSSFCellStyle. Currently there is no direct way to convert the HSSFCellStyle to XSSFCellStyle in NPOI.

I managed my program by copying one by one style individually.

_xssfStyle .BorderLeft = _hssfStyle .BorderLeft;
_xssfStyle .BorderRight = _hssfStyle .BorderRight;
_xssfStyle .BorderTop = _hssfStyle .BorderTop;
_xssfStyle .BorderBottom = _hssfStyle .BorderBottom;
_xssfStyle .FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;
_xssfStyle .FillPattern = NPOI.SS.UserModel.FillPattern.SolidForeground;
_xssfStyle .WrapText = _hssfStyle .WrapText;
_xssfStyle .VerticalAlignment = _hssfStyle .VerticalAlignment;
Sobhan
  • 796
  • 1
  • 9
  • 31