Home > database >  Is there a way to transfer a header to several worksheets?
Is there a way to transfer a header to several worksheets?

Time:08-17

I create the following header and want to apply it to several worksheets instead of writing the same code for every 16 worksheets. Is there a way to build a method for this and use it for all worksheets?

            // Create Worksheet
            var nf = package.Workbook.Worksheets.Add("Station_1");
            //  15 worksheets...

            // Format Header
            nf.Cells["A1"].Value = "Login";
            nf.Cells["A2"].Value = "Password";
            nf.Cells["A3"].Value = "Date";
            nf.Row(1).Style.Font.Bold = true;

CodePudding user response:

I suppose nf is of type ExcelWorkSheet. Add a method FormatHeader(ExcelWorkSheet ws) and call it with all your worksheets:

private void FormatHeader(ExcelWorkSheet ws)
{
    // Format Header
    ws.Cells["A1"].Value = "Login";
    ws.Cells["A2"].Value = "Password";
    ws.Cells["A3"].Value = "Date";
    ws.Row(1).Style.Font.Bold = true;
}

Call it like this:

FormatHeader(package.Workbook.Worksheets.Add("Station_1"));
FormatHeader(package.Workbook.Worksheets.Add("Station_2"));
...

  • Related