Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello friends...

I am trying to download gridview's dada in EXCEL file..
for this First I'm creating a file and using Response.Addheader to download it...
The file downaloded successfully.. no issue with this..
I have no problem while this program executes.... But when I try to open my EXCEL file it doesn't open.
For this I looked into Task Manager that already a EXCEL process working due to which I'm not able to open.. so every time I need to kill the process and then open the file..
I used Process class to kill the process through the program this helped me bit..
because I don't need to close process explicitly from Task manager...
But again... If I have opened 3 other excel file this approach closes all the previous opened files..
Help me...

I'm pasting code below
string fileName = "Record FROM " + ddlFromDate.SelectedValue.Trim() + " to " + ddlTodate.SelectedValue.Trim();
        if (File.Exists(@"D:\Manish\" + fileName))
        {
            fileName += "1";
        }
        Excel.ApplicationClass excel = new Excel.ApplicationClass();
        Excel.Workbook workbook = excel.Application.Workbooks.Add(true);
        excel.Cells[1, 1] = "Caller ID";
        excel.Cells[1, 2] = "Holder Name";//exportdata.Columns[1].ColumnName;
        excel.Cells[1, 3] = "Job Level";//exportdata.Columns[2].ColumnName;
        excel.Cells[1, 4] = "Org.Unit";// exportdata.Columns[3].ColumnName;
        excel.Cells[1, 5] = "District";// exportdata.Columns[4].ColumnName;
        excel.Cells[1, 6] = "Episode";// exportdata.Columns[5].ColumnName;
        excel.Cells[1, 7] = "Recording Date";// exportdata.Columns[6].ColumnName;
        excel.Cells[1, 8] = "Recording Time";// exportdata.Columns[7].ColumnName;
        int row = 1;
        foreach (DataRow currRow in exportdata.Rows)
        {
            row++;
            for (int i = 1; i < 9; i++)
            {
                excel.Cells[row, i] = currRow[exportdata.Columns[i].ColumnName];
            }
        }
        string path = @"D:\Manish\"+fileName;
        object missing = System.Reflection.Missing.Value;
        workbook.SaveAs(path, Excel.XlFileFormat.xlExcel7, missing, missing,false, false, Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing);
        excel.Quit();
        excel.Application.Workbooks.Close();
        foreach(Process proc in Process.GetProcessesByName("EXCEL")
        {
            proc.Kill();
        }
        FileInfo getInfo = new FileInfo(@"D:\Manish\" + fileName + ".xls");
        if (getInfo.Exists)
        {
            Response.AddHeader("Content-Disposition", "attachment; filename=" + getInfo.Name);
            Response.ContentType = "application/vnd.xls";
            Response.WriteFile(getInfo.FullName);
            Response.End();
        }
Posted
Updated 1-Aug-11 20:04pm
v2

Hello Manish,

C#
private void releaseObject(object obj)
      {
          try
          {
              System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
              obj = null;
          }
          catch (Exception ex)
          {
              obj = null;
              MessageBox.Show("Unable to release the Object " + ex.ToString());
          }
          finally
          {
              GC.Collect();
          }
      }


use this function and provide the name of ur object. Hoping it will help you.

thanks
sanjeev
 
Share this answer
 
Comments
Manish Kumar Namdev 2-Aug-11 2:45am    
Hello Sanjeev Singh thanks to rpely...
this worked fine..
but it wasn't that what I am looking.. after using your code.. the EXCEL process still remains in Task manager due to which i am not able to open saved file.. I don't want to close all the excel file but just want to dispose or kill or cancel the excel process which started when I had stared to save my data using Excel.ApplicationClass excel = new Excel.ApplicationClass();...
I am still terminating manually excel process from Task Manager
[no name] 2-Aug-11 2:47am    
Thanks.as soon as you finish with work with your object 'excel' call the method and it will close the object.
You can refer the below link for solution


http://forums.bitwiseglobal.com/Forums/Thread.aspx?pageid=0&mid=4&ItemID=40&thread=800
 
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