Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
For export data from GridView to Excel By Component, I use from below commands:
C#
System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
        Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
        Microsoft.Office.Interop.Excel.Workbook excelWorkBook = excelApp.Workbooks.Add(XlSheetType.xlWorksheet);
        Microsoft.Office.Interop.Excel.Worksheet excelWorkSheet = (Worksheet)excelWorkBook.Worksheets[1];
        excelWorkSheet.DisplayRightToLeft = true;
        int ClmCount = GridView1.Columns.Count;
        Range[] rng = new Range[ClmCount];

        for (int i = 0; i < ClmCount; i++)
        {
            string CellAddress = Convert.ToString(Convert.ToChar(Convert.ToByte(i + 65))) + "1";
            rng[i] = excelWorkSheet.get_Range(CellAddress, CellAddress);
            rng[i].Value2 = GridView1.Columns[i].HeaderText;
        }

        int j = 2;
        foreach (GridViewRow grdr in GridView1.Rows)
        {
            for (int k = 0; k < ClmCount; k++)
            {
                string CellAddress = Convert.ToString(Convert.ToChar(Convert.ToByte(k + 65))) + j.ToString();
                rng[k] = excelWorkSheet.get_Range(CellAddress, CellAddress);
                rng[k].Value2 = grdr.Cells[k].Text.ToString();
            }
            j++;
        }

        excelApp.Visible = true;


but after publish and run my web site , I will be faced with this error:

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)). 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

Source Error: 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 
[COMException (0x80040154): Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).]
   rl_allusers_Rep_cpdbc_2.send_to_Excel_by_Component2_Click(Object sender, EventArgs e) +100
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

________________________________________
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1 
Thanks very much
Posted
Updated 2-Jul-12 6:42am
v2
Comments
[no name] 2-Jul-12 11:12am    
And your question is what?
ZurdoDev 2-Jul-12 12:56pm    
You need the Excel dlls on the server.

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