Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to export datagridview to excel with header logo and text in window application

What I have tried:

Microsoft.Office.Interop.Excel.Application xlApp;
            Microsoft.Office.Interop.Excel.Application oExcel_12 = null;                //Excel_12 Application
            Microsoft.Office.Interop.Excel.Workbook oBook = null;                       // Excel_12 Workbook
            Microsoft.Office.Interop.Excel.Sheets oSheetsColl = null;                   // Excel_12 Worksheets collection
            Microsoft.Office.Interop.Excel.Worksheet oSheet = null;                     // Excel_12 Worksheet
            Microsoft.Office.Interop.Excel.Range oRange = null;                         // Cell or Range in worksheet
            Object oMissing = System.Reflection.Missing.Value;
            xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

            // Create an instance of Excel_12.
            oExcel_12 = new Microsoft.Office.Interop.Excel.Application();

            // Make Excel_12 visible to the user.
            oExcel_12.Visible = true;

            // Set the UserControl property so Excel_12 won't shut down.
            oExcel_12.UserControl = true;

            // System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");

            // Add a workbook.
            oBook = oExcel_12.Workbooks.Add(oMissing);

            // Get worksheets collection 
            oSheetsColl = oExcel_12.Worksheets;

            // Get Worksheet "Sheet1"
            oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oSheetsColl.get_Item(1);
            int count = dGV.Columns.Count;
            Microsoft.Office.Interop.Excel.Range formatRange = oSheet.UsedRange;
            formatRange = oSheet.get_Range("a1") as Microsoft.Office.Interop.Excel.Range;
            formatRange.EntireRow.Font.Bold = true;
            oSheet.Cells[1, 1] = "Bold";
            oRange = oSheet.Cells[1, 1] as Microsoft.Office.Interop.Excel.Range;
            oRange.Value2 = "Agent  : " + cmbagent.Text + " GST Type : " + cmbGSTType.Text;
            // Export titles
            for (int j = 0; j < dGV.Columns.Count; j++)
            {
                Microsoft.Office.Interop.Excel.Range formatRange2 = oSheet.UsedRange;
                formatRange2 = oSheet.get_Range("b1") as Microsoft.Office.Interop.Excel.Range;
                formatRange2.EntireRow.Font.Bold = true;
                oSheet.Cells[2, j + 1] = "Bold";
                oRange = oSheet.Cells[2, j + 1] as Microsoft.Office.Interop.Excel.Range;
                oRange.Value2 = dGV.Columns[j].HeaderText;
            }
            // Export data
            for (int i = 0; i < dGV.Rows.Count; i++)
            {
                for (int j = 0; j < dGV.Columns.Count; j++)
                {
                    oRange = oSheet.Cells[i + 3, j + 1] as Microsoft.Office.Interop.Excel.Range;
                    oRange.Value2 = dGV.Rows[i].Cells[j].Value;
                }
            }
            oRange.get_Range("A1", "A2").Merge(false);

            // Release the variables.
            //oBook.Close(false, oMissing, oMissing);
            oBook = null;

            //oExcel_12.Quit();
            oExcel_12 = null;

            // Collect garbage.
            GC.Collect();
Posted
Updated 28-Nov-17 20:04pm
v2
Comments
F-ES Sitecore 28-Nov-17 6:24am    
What's the question?

1 solution

 
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