简体   繁体   中英

how to add header while exporting data table to excel C#

This is my code which is working fine,But how to add header for excel file in this method

public void ExportToExcel()
     {
     DataTable Tbl = new DataTable();
     Tbl = dt;
     OpenFileDialog openDlg = new OpenFileDialog();
     System.Windows.Forms.SaveFileDialog saveDlg = new 
     System.Windows.Forms.SaveFileDialog();
     if (saveDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
     {
     string path = saveDlg.FileName;
     try
     {
     if (Tbl == null || Tbl.Columns.Count == 0)
     throw new Exception("ExportToExcel: Null or empty input table!\n");
     // load excel, and create a new workbook
     Microsoft.Office.Interop.Excel.Application excelApp = new 
     Microsoft.Office.Interop.Excel.Application();
     excelApp.Workbooks.Add();

     // single worksheet
     Microsoft.Office.Interop.Excel._Worksheet workSheet = 
     excelApp.ActiveSheet;
     // column headings
     for (int i = 0; i < Tbl.Columns.Count; i++)
     {
     workSheet.Cells[1, (i + 1)] = Tbl.Columns[i].ColumnName;
     }

     // rows
     for (int i = 0; i < Tbl.Rows.Count; i++)
     {
     // to do: format datetime values before printing
     for (int j = 0; j < Tbl.Columns.Count; j++)
     {
     workSheet.Cells[(i + 2), (j + 1)] = Tbl.Rows[i][j];
     }
     }
     // check fielpath
     if (path != null && path != "")
     {
     try
     {
     workSheet.SaveAs(path);
     excelApp.Quit();
     MessageBox.Show("Excel file saved!");
     }
     catch (Exception ex)
     {
      throw new Exception("ExportToExcel: Excel file could  not be saved! Check 
      filepath.\n"+ ex.Message);
      }
      }
      else    // no filepath is given
      {
      excelApp.Visible = true;
      }
      }
      catch (Exception ex)
      {
      throw new Exception("ExportToExcel: \n" + ex.Message);
      }
      }
      }
Excel.Range headerRange = xlWorkSheet1.get_Range("A1", "V1");
headerRange.HorizontalAlignment = 
Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
headerRange.Value = "Header text 1";

headerRange = xlWorkSheet2.get_Range("A1", "V1");
headerRange.HorizontalAlignment = 
Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
headerRange.Value = "Header text 2";

The sample application adds a header or footer in a document that you supply, calling the XLInsertHeaderFooter method in the sample to do the work. The method enables you to add a header or footer that indicates on which group of pages the header should appear.

const string fileName = @"C:\temp\test.xlsx"
XLInsertHeaderFooter(fileName, "Sheet1", 
"This is my header", HeaderType.EvenHeader);
XLInsertHeaderFooter(fileName, "Sheet1", 
"This is my footer", HeaderType.AllFooter);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM