Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
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

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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900