Click here to Skip to main content
12,623,724 members (33,753 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET Windows MS-Excel , +
Im doing a windows application on store management for a company, I have exported my datagrid successfully to excel but I would like to know if there is a way to set a header to the document similar to a company letter head with its name, logo and address.
The code I used to export my datagrid was

private void button2_Click_1(object sender, EventArgs e)
       {
           Excel.Application app = new Excel.Application();
           app.Visible = true;
           Excel.Workbook wb = app.Workbooks.Add(1);
           Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
           // changing the name of active sheet
           ws.Name = "Exported from gridview";
 
           ws.Rows.HorizontalAlignment = HorizontalAlignment.Center;
           // storing header part in Excel
           for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
           {
               ws.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
           }
 

           // storing Each row and column value to excel sheet
           for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
           {
               for (int j = 0; j < dataGridView1.Columns.Count; j++)
               {
                   ws.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
               }
           }
 
           // sizing the columns
           ws.Cells.EntireColumn.AutoFit();
 
           // save the application
           wb.SaveAs("D:\\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
 
           // Exit from the application
           app.Quit();
       }

Please help me with this, what modification should i do to add the company details in the header in the excel doc automatically while exporing
Posted 10-Jan-13 19:41pm
Comments
cekshanu 11-Jan-13 5:27am
   
The client has given me an image to include as header in to the excel sheet while exporting it... Is it possible?

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Read last answer in this blog . Very good explanation over your requirement

http://stackoverflow.com/questions/9410401/adding-custom-header-to-the-excel-file[^]

Accept and vote if found helpful
--RDBurmon
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161128.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100