0

I try to create new Excel file from a template Excel by copying, and edit the new Excel file by NPOI. The template has cells whose background colors are light gray. But the background colors of the light gray dissapear on the new Excel. Only light gray background color dissapear but not other colors.

code:

private void button1_Click(object sender, EventArgs e)
        {
            File.Copy(
                templateFilePath,
                destFileName,
                true);

            string filePath = destFileName;

            IWorkbook workbook;
            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                workbook = new XSSFWorkbook(fs);
            }

            /*
            ISheet sheet = workbook.GetSheetAt(0);

            IRow row = sheet.GetRow(1);
            ICell cell = row.GetCell(1);

            cell.SetCellValue("a");
            */

            using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                workbook.Write(fs);
            }
        }

Template Excel:

enter image description here

The new Excel:

enter image description here

Angus B.
  • 23
  • 1
  • 8
  • こにちは - The default color pallet in Excel changed from 2003 to current. Perhaps the old color was reference by name but the new Excel doesn't find a match and defaults to white or clear instead. You may be able to open the original and reset the color pallet before copying it to get around this but am not sure. – Automate This Nov 12 '21 at 18:00
  • @AutomateThis こんにちは! yes, seems so. but how to reset the color pallet before copying it? – Angus B. Nov 16 '21 at 04:18
  • I believe if you open the old file in your current Excel you can go to 'Page Layout' -> 'Colors' -> 'Customize' -> and then hit 'Reset' on the lower left of the dialog box. I'm on 2016 so not sure if it's the same for Office 365. – Automate This Nov 16 '21 at 15:04

0 Answers0