Excel文件中的数据并下载 [英] Data in Excel File and Download

查看:75
本文介绍了Excel文件中的数据并下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友...

我正在尝试在EXCEL文件中下载gridview的dada..
为此,我首先创建一个文件,然后使用Response.Addheader下载它...
该文件已成功降级..与此没有问题..
该程序执行时我没问题....但是当我尝试打开我的EXCEL文件时,它没有打开.
为此,我调查了任务管理器,由于它无法打开.EXCEL进程,因此我无法打开..所以每次我需要终止该进程然后打开文件时..
我使用Process类通过该程序杀死了进程,这对我有所帮助.
因为我不需要从任务管理器中明确关闭进程...
但是再次...如果我打开了其他3个excel文件,此方法将关闭所有以前打开的文件..
帮帮我...

我在下面粘贴代码

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();
        }

推荐答案

Hello Manish,

Hello Manish,

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();
          }
      }



使用此功能并提供ur对象的名称.希望对您有帮助.

谢谢
sanjeev



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

thanks
sanjeev


您可以参考以下链接获取解决方案


http://forums.bitwiseglobal.com/Forums/Thread.aspx?pageid=0&mid=4&ItemID=40&thread=800
You can refer the below link for solution


http://forums.bitwiseglobal.com/Forums/Thread.aspx?pageid=0&mid=4&ItemID=40&thread=800


这篇关于Excel文件中的数据并下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆