Underlining Headers from WPF Datagrid when printing using Excel print function

0 votes

Currently using the Excel print function to Export data from a WPF Datagrid and print it. Below is the code I am using:

 private void OnDataGridPrinting(object sender, RoutedEventArgs e)
    {
        System.Windows.Controls.PrintDialog Printdlg = new System.Windows.Controls.PrintDialog();
        if ((bool)Printdlg.ShowDialog().GetValueOrDefault())
        {
            // creating Excel Application  
            Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
            // creating new WorkBook within Excel application  
            Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
            // creating new Excelsheet in workbook  
            Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
            // see the excel sheet behind the program  
            app.Visible = false;
            // get the reference of first sheet. By default its name is Sheet1.  
            // store its reference to worksheet  
            worksheet = workbook.Sheets["Sheet1"];
            worksheet = workbook.ActiveSheet;
            // changing the name of active sheet  
            worksheet.Name = SelectedCustomer.CustName;
            // storing header part in Excel  

            CollectionViewSource itemCollectionViewSources;
            itemCollectionViewSources = (CollectionViewSource)(FindResource("ItemCollectionViewSources"));



            for (int i = 1; i < dgEndOfYear.Columns.Count + 1; i++)
            {
                **worksheet.Cells[1, i] = dgEndOfYear.Columns[i - 1].Header;**

            }
            // storing Each row and column value to excel sheet  
            for (int i = 0; i < CustomerEOYTotals.Count - 0; i++)
            {
                absvwEndOfYearTotal1 line = CustomerEOYTotals[i];
                worksheet.PageSetup.CenterHeader = "&UEnd Of Year Report " + CurrentYear;
                worksheet.Cells[i + 2, 1] = line.CalendarYear;
                worksheet.Cells[i + 2, 2] = line.CustName;
                worksheet.Cells[i + 2, 3] = line.MonthName;
                worksheet.Cells[i + 2, 4] = line.SumGal.Value.ToString();
                worksheet.Cells[i + 2, 5] = line.Manifest;
               
                worksheet.Columns.Cells.Font.FontStyle = Microsoft.Office.Interop.Excel.XlUnderlineStyle.xlUnderlineStyleSingle;
                worksheet.Cells.Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                worksheet.Columns.ColumnWidth = 14;

            }
           

            // Print the workbook
            workbook.PrintOut();

The line "worksheet.Cells[1, i] = dgEndOfYear.Columns[i - 1].Header" are the headers from the data grid I have made several attempts to style the headers so they are underlined like the below example when printed out but have had no luck any help would be appreciated: example

Jan 19, 2023 in Others by Kithuzzz
• 38,000 points
371 views

1 answer to this question.

0 votes

Try this:

worksheet.Cells[1, i] = dgCountyEOY.Columns[i - 1].Header;
                worksheet.Cells[1, i].Characters.Font.Underline = true;
answered Jan 19, 2023 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer

How to convert data from txt files to Excel files using python

Hi , there are few steps to ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,690 points
14,049 views
0 votes
1 answer

Calculate Birthdate from an age using y,m,d in Excel

Hello, yes u can find your birthdate using ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,690 points
4,161 views
0 votes
1 answer

Calculate Birthdate from an age using y,m,d in Excel

Hi To Calculate the date, we can ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,690 points
2,026 views
0 votes
1 answer

Unable to import data in excel from another website using VB code

Replace : Set ieTable = ieDoc.all.Item("report-table") With: Set ieTable = ...READ MORE

answered Sep 21, 2022 in Others by narikkadan
• 63,600 points
810 views
0 votes
0 answers

Print Excel Worksheet/Workbook

I'm attempting to make a C# Winform ...READ MORE

Oct 31, 2022 in Others by Kithuzzz
• 38,000 points
673 views
0 votes
1 answer

Generate a flat list of all excel cell formulas

Hello, you'll have to follow certain steps ...READ MORE

answered Feb 18, 2022 in Others by gaurav
• 23,260 points
567 views
0 votes
1 answer

Convert a number to a letter in C# for use in Microsoft Excel [duplicate]

If you are familiar with using formulas ...READ MORE

answered Feb 23, 2022 in Database by gaurav
• 23,260 points
868 views
0 votes
1 answer

Deleting duplicate rows in Excel using Epplus

You need to re-think this… the while ...READ MORE

answered Feb 23, 2022 in Database by gaurav
• 23,260 points
1,516 views
0 votes
1 answer

Using Visual Basic to pull data from within a range to use in an Excel function

Use AVERAGEIFS instead of the full range. ...READ MORE

answered Jan 14, 2023 in Others by narikkadan
• 63,600 points
520 views
0 votes
1 answer

Convert CASE WHEN logic from SQL to EXCEL

A combination of IF(), ISNUMBER(SEARCH()), and OR() ...READ MORE

answered Sep 21, 2022 in Others by narikkadan
• 63,600 points
1,225 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP