Hello,
I ran across an issue that took me several hours to narrow down what was actually happening, since it seemed very random. However, once I simplified my code, I finally found how to re-produce it and also a workaround for it. The issue that I'm having is that I have a file called "Resources.xlsx" that contains 5 worksheets, 2 of which are Templates that are formatted exactly how I want them to be, but the values are left blank. I want to use these 2 unique templates to create workbooks with many of the templates copied and then populated. However, an issue occurs where many cells in the first worksheet of the new workbook are formatted incorrectly.
The exact thing that I'm finding is happening is that it seems to be combining all formatting from the first 3 or 4 rows of the spreadsheet that are being used, so the first 3 or 4 rows from FirstRowUsed() basically, and once it combines the formatting from those cells, it places that format on every other cell in the worksheet that is in a blank row. But it only seems to insert the formatting from column A to the LastColumnUsed(). This ONLY seems to happen to the first worksheet, and all other worksheets copied from the same template are fine. I did have it happen where it changed the format in all worksheets, but I cannot replicate it.
Now my workaround has 2 options that both seem to work. My first test that worked was to use CopyTo to copy the first worksheet as normal, but then use SaveAs on the new workbook, then assign the workbook to the newly created file, and then Delete the first worksheet. Then I could copy all of the worksheets and all would be ok. However, if I simply created an empty sheet, that did NOT fix it. So this showed me that even if I deleted the first worksheet it would be fine, but only if I copied the sheet from that template. So it seemed to only be the first time that template was copied into the workbook.
My second workaround is much easier and simpler. When the template is moved to the first worksheet in the workbook, the issue seems to go away. So instead of having one file called Resources.xlsx with my Expense Voucher Template and my Travel Voucher Template, I'll now have a separate workbook for each template, so it will be the first sheet in its own workbook.
So it seems that when a worksheet is not the first one in a workbook, and is copied to a new workbook, the first worksheet copied takes the first 3 or 4 rows from FirstRowUsed() and places that formatting in all empty rows in the worksheet, from column A to LastColumnUsed().
I'm attaching my Resources.xlsx file, and below is my code. My 2 templates are the ones I mentioned above, Expense Voucher Template and Travel Voucher Template. In my code I'm only using Expense Voucher but it still illustrates the issue. Specifically notice the cells in the range E4:F6. If I remove the border and remove the Bold font type, it also removes border and Bold type from all cells in the first copied worksheet.
Thanks!
Paul
Here's the code:
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClosedXML.Excel;
using DocumentFormat.OpenXml.Office.Excel;
using System.IO;
namespace TestingAnything
{
class Program
{
XLWorkbook travelVouchersWkBk = null;
IXLWorksheet travelVoucherTemplate = null;
static void Main(string[] args)
{
Program p = new Program();
p.run();
}
private void run()
{
if (File.Exists("./TravelVouchers.xlsx")) File.Delete("./TravelVouchers.xlsx");
XLWorkbook wb = new XLWorkbook("./Resources.xlsx");
travelVoucherTemplate = wb.Worksheet("Expense Voucher Template");
IXLWorksheet vendorSheet = travelVoucherTemplate.CopyTo(travelVouchersWkBk, "test");
if (!File.Exists("./TravelVouchers.xlsx"))
{
travelVouchersWkBk.SaveAs("./TravelVouchers.xlsx");
travelVouchersWkBk = new XLWorkbook("./TravelVouchers.xlsx");
}
else travelVouchersWkBk.Save();
}
}
}
```
I ran across an issue that took me several hours to narrow down what was actually happening, since it seemed very random. However, once I simplified my code, I finally found how to re-produce it and also a workaround for it. The issue that I'm having is that I have a file called "Resources.xlsx" that contains 5 worksheets, 2 of which are Templates that are formatted exactly how I want them to be, but the values are left blank. I want to use these 2 unique templates to create workbooks with many of the templates copied and then populated. However, an issue occurs where many cells in the first worksheet of the new workbook are formatted incorrectly.
The exact thing that I'm finding is happening is that it seems to be combining all formatting from the first 3 or 4 rows of the spreadsheet that are being used, so the first 3 or 4 rows from FirstRowUsed() basically, and once it combines the formatting from those cells, it places that format on every other cell in the worksheet that is in a blank row. But it only seems to insert the formatting from column A to the LastColumnUsed(). This ONLY seems to happen to the first worksheet, and all other worksheets copied from the same template are fine. I did have it happen where it changed the format in all worksheets, but I cannot replicate it.
Now my workaround has 2 options that both seem to work. My first test that worked was to use CopyTo to copy the first worksheet as normal, but then use SaveAs on the new workbook, then assign the workbook to the newly created file, and then Delete the first worksheet. Then I could copy all of the worksheets and all would be ok. However, if I simply created an empty sheet, that did NOT fix it. So this showed me that even if I deleted the first worksheet it would be fine, but only if I copied the sheet from that template. So it seemed to only be the first time that template was copied into the workbook.
My second workaround is much easier and simpler. When the template is moved to the first worksheet in the workbook, the issue seems to go away. So instead of having one file called Resources.xlsx with my Expense Voucher Template and my Travel Voucher Template, I'll now have a separate workbook for each template, so it will be the first sheet in its own workbook.
So it seems that when a worksheet is not the first one in a workbook, and is copied to a new workbook, the first worksheet copied takes the first 3 or 4 rows from FirstRowUsed() and places that formatting in all empty rows in the worksheet, from column A to LastColumnUsed().
I'm attaching my Resources.xlsx file, and below is my code. My 2 templates are the ones I mentioned above, Expense Voucher Template and Travel Voucher Template. In my code I'm only using Expense Voucher but it still illustrates the issue. Specifically notice the cells in the range E4:F6. If I remove the border and remove the Bold font type, it also removes border and Bold type from all cells in the first copied worksheet.
Thanks!
Paul
Here's the code:
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClosedXML.Excel;
using DocumentFormat.OpenXml.Office.Excel;
using System.IO;
namespace TestingAnything
{
class Program
{
XLWorkbook travelVouchersWkBk = null;
IXLWorksheet travelVoucherTemplate = null;
static void Main(string[] args)
{
Program p = new Program();
p.run();
}
private void run()
{
if (File.Exists("./TravelVouchers.xlsx")) File.Delete("./TravelVouchers.xlsx");
XLWorkbook wb = new XLWorkbook("./Resources.xlsx");
travelVoucherTemplate = wb.Worksheet("Expense Voucher Template");
IXLWorksheet vendorSheet = travelVoucherTemplate.CopyTo(travelVouchersWkBk, "test");
if (!File.Exists("./TravelVouchers.xlsx"))
{
travelVouchersWkBk.SaveAs("./TravelVouchers.xlsx");
travelVouchersWkBk = new XLWorkbook("./TravelVouchers.xlsx");
}
else travelVouchersWkBk.Save();
}
}
}
```