Click here to Skip to main content
15,922,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using the Following Code to create the excel file while i execute the code the error has come the line at middle of the code(I mention seperately)

C#
[STAThread]

       protected void Generate_Click(object sender, EventArgs e)
        {
            int io = 0;
            System.Data.DataTable workTable = new System.Data.DataTable();
            workTable.TableName = "VendorList";
            workTable.Columns.Add("CURRENCY_CODE");
            workTable.Columns.Add("REMARKS");
            workTable.Columns.Add("VENDOR_NAME");
            System.Data.DataRow workRow;
            objDAOUploadEnquiry.ActionType = SRCMAction.FETCH.ToString();
            objBLLUploadEnquiry.SelectCommonMasterUom(objDAOUploadEnquiry);
            object dropDownValues = "";
            for (int y = 0; y <= 9; y++)
            {
                workRow = workTable.NewRow();
                //workRow[0] = "Cust" + y.ToString();// y;
                //workRow[1] = "Cust" + y.ToString();
                if (io == 0)
                {
                    for (int kl = 0; kl < objDAOUploadEnquiry.UploadEnquriyList.Rows.Count; kl++)
                    {
                        dropDownValues += objDAOUploadEnquiry.UploadEnquriyList.Rows[kl]["TEXT_VALUE"].ToString() + ",";
                    }
                    io = 1;
                }
                workTable.Rows.Add(workRow);
            }

            string data = null;
            int i = 0;
            int j = 0;
            int rowsCount = workTable.Rows.Count + 1;

            Excel.Application xlApp;

            Excel.Workbook xlWorkBook;

            Excel.Worksheet xlWorkSheet;

            object misValue = System.Reflection.Missing.Value;
            object pwd = "pwd";
            Excel.Application xl = null;

            Excel._Workbook wb = null;

            Excel._Worksheet sheet = null;

            object Missing = System.Reflection.Missing.Value;

            try
            {

                xlApp = new Excel.ApplicationClass();
                xl = new Excel.Application();

                xlWorkBook = xlApp.Workbooks.Add(misValue);
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                wb = (Excel._Workbook)(xl.Workbooks.Add(Missing));
                wb.Sheets.Add(Missing, Missing, Missing, Missing);
                sheet = (Excel._Worksheet)(wb.Sheets[1]);
                xlWorkSheet.Name = "IncludeVendorList";
                Excel.Range m_range = sheet.get_Range("A1", "A10");
                //sheet.get_Range("A1", "A11").Font.Color  = System .Drawing .Color .Red ;





                Excel.Range _nValidationRange = xlApp.get_Range("C2", "C10");

<code>I got an error on the Following Line While i execute this code </code>
                _nValidationRange.Validation.Add(Excel.XlDVType.xlValidateList, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, dropDownValues, Type.Missing);
                xlWorkBook.SaveAs(("G:\\dhinakr.xls"), Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);



                xlWorkBook.Save();
                xlApp.Quit();
                xlApp.Application.Quit();
                string filepath = "G:\\dhinakr.xls";
                FileInfo myfile = new FileInfo(filepath);
                if (myfile.Exists)
                {
                    Response.ClearContent();
                    Response.ContentType = "application/vnd.ms-excel";
                    Response.AppendHeader("Content-Disposition", "attachment; filename=" + myfile.Name);
                    Response.AddHeader("Content-Length", myfile.Length.ToString());
                    Response.TransmitFile(myfile.FullName);

                    Response.End();
                }
            }
            catch (COMException Ex)
            {
                GC.Collect();
                throw new Exception(Ex.Message);
            }
        }
Posted
Updated 5-Jul-10 22:55pm
v3
Comments
R. Giskard Reventlov 5-Jul-10 11:11am    
Way too much code: no one has the time to wade through this. Try to cut it down to the bare minimum to illustrate the problem: if anyone is that interested or needs to see more they will ask.
Richard MacCutchan 5-Jul-10 13:15pm    
Especially as you failed to post the code where you actually get the bad result!
Sandeep Mewara 6-Jul-10 1:02am    
And which part of your code throws that error?
Nishant Singh 6-Jul-10 2:18am    
Debug the Code and see where the error is coming ..........and then if you cannot find post that snippet

1 solution

It's a COM error, so either you have a reference to the wrong version of Excel, or you have some other issue in your code as it relates to the version of Excel you have, or in general.
 
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