asp.net excel进程

来源:互联网 发布:ip 域名 协议 编辑:程序博客网 时间:2024/05/24 04:30

当我们利用excel进行VBA程序设计时,往往会遇到excel进程关不了的问题,怎样解决呢,请看如下实例:

在使用之前必须对excel相关命名空间进行引用:

using Excel;

using System.Reflection;
using System.Diagnostics;
using System.Runtime.InteropServices;

 

 

protected void LoadDownLoad()
  {   
  
   string save_path="";
   CreateExcel(ref save_path);    
   
   string path=Server.MapPath(save_path);
   System.IO.FileInfo file = new System.IO.FileInfo(path);
   Response.Clear();
   Response.Charset="GB2312";
   Response.ContentEncoding=System.Text.Encoding.UTF8;
   Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
   Response.AddHeader("Content-Length", file.Length.ToString());
   Response.ContentType = "application/ms-excel";
   Response.WriteFile(file.FullName);
   Response.End();

  }

 

 

 

  private void CreateExcel(ref string save_path)
  { 
   DateTime beforeTime = DateTime.Now;
 
   Excel.Application app = new Excel.ApplicationClass();
   if(app==null)
   {
    return;
   }   
   app.Visible = false;
   app.DisplayAlerts=false;
   DateTime afterTime = DateTime.Now;

   Excel.Range oRng;  
   Excel._Workbook workbook = app.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);  
   Excel._Worksheet worksheet = (Excel._Worksheet)workbook.Worksheets[1];
   if(worksheet==null)
   {
    return;
   }
   worksheet.Cells[1,1]="客户中文名";
   worksheet.Cells[1,2]="客户英文名";
   worksheet.Cells[1,3]="最终用户(中文)";
   worksheet.Cells[1,4]="最终用户(英文)";
   worksheet.Cells[1,5]="项目名称";
   worksheet.Cells[1,6]="项目状态";
   worksheet.Cells[1,7]="产品线";
   worksheet.Cells[1,8]="产品型号";
   worksheet.Cells[1,9]="注册号";
   worksheet.Cells[1,10]="询价数量";
   worksheet.Cells[1,11]="预测量(年)";
   worksheet.Cells[1,12]="目标价(USD)";
   worksheet.Cells[1,13]="竟争型号";
   worksheet.Cells[1,14]="价格(USD)";
   worksheet.Cells[1,15]="竞争品牌";
   worksheet.Cells[1,16]="说明描述";
   worksheet.get_Range(worksheet.Cells[1,1],worksheet.Cells[1,16]).HorizontalAlignment=Excel.XlVAlign.xlVAlignCenter;
   worksheet.get_Range(worksheet.Cells[1,1],worksheet.Cells[1,15]).ColumnWidth=10;
   worksheet.get_Range(worksheet.Cells[1,16],worksheet.Cells[1,16]).ColumnWidth=65;
   worksheet.get_Range(worksheet.Cells[1,1],worksheet.Cells[1,16]).Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
   int i=0;
   foreach(DataGridItem Item in DgDownLoadDetail.Items)
   {
    worksheet.Cells[2+i,1]=Item.Cells[2].Text.ToString().Trim().Replace(" ","");//客户
    worksheet.Cells[2+i,2]=Item.Cells[9].Text.ToString().Trim().Replace(" ","");//客户英文名
    worksheet.Cells[2+i,3]=Item.Cells[10].Text.ToString().Trim().Replace(" ","");//最终用户(中)
    worksheet.Cells[2+i,4]=Item.Cells[11].Text.ToString().Trim().Replace(" ","");//最终用户(英)
    worksheet.Cells[2+i,5]=Item.Cells[1].Text.ToString().Trim().Replace(" ","");//项目名称
    worksheet.Cells[2+i,6]=Item.Cells[12].Text.ToString().Trim().Replace(" ","");//项目状态
    worksheet.Cells[2+i,7]=Item.Cells[3].Text.ToString().Trim().Replace(" ","");//产品线
    worksheet.Cells[2+i,8]=Item.Cells[4].Text.ToString().Trim().Replace(" ","");//产品型号
    worksheet.Cells[2+i,9]=Item.Cells[5].Text.ToString().Trim().Replace(" ","");//注册号
    worksheet.Cells[2+i,10]=Item.Cells[13].Text.ToString().Trim().Replace(" ","");//询价数量
    worksheet.Cells[2+i,11]=Item.Cells[6].Text.ToString().Trim().Replace(" ","");//年预测量
    worksheet.Cells[2+i,12]=Item.Cells[14].Text.ToString().Trim().Replace(" ","");//目标价(USD)
    worksheet.Cells[2+i,13]=Item.Cells[15].Text.ToString().Trim().Replace(" ","");//竞争型号
    worksheet.Cells[2+i,14]=Item.Cells[16].Text.ToString().Trim().Replace(" ","");//价格(USD)
    worksheet.Cells[2+i,15]=Item.Cells[17].Text.ToString().Trim().Replace(" ","");//竞争品牌
    worksheet.Cells[2+i,16]=Item.Cells[18].Text.ToString().Trim().Replace(" ","");//说明描述
    worksheet.get_Range(worksheet.Cells[2 + i, 1],worksheet.Cells[2 + i,16]).Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);//设置边框颜色,不然打印预览,会非常不雅观
    i+=1;
   }
   oRng = worksheet.get_Range("A1", "H1");
   oRng.EntireColumn.AutoFit();
   worksheet.Name = "ProductRFQ";
   string tick =System.DateTime.Now.ToFileTime().ToString();
   save_path = "~/UploadFiles"+ "//Rfq"+tick+ ".xls";
   workbook.SaveAs(Server.MapPath(save_path),Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
    Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
   workbook.Close(false,Type.Missing,Type.Missing);
   app.Workbooks.Close();  
   app.Quit();
     
   System.Runtime.InteropServices.Marshal.ReleaseComObject(oRng);  
   System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
   System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);   
   System.Runtime.InteropServices.Marshal.ReleaseComObject(app);    

   oRng=null;
   worksheet = null;   
   workbook = null;
   app = null;   
   GC.Collect();

   Process[] myProcesses;
            DateTime startTime;
            myProcesses = Process.GetProcessesByName("Excel");

            //得不到Excel进程ID,暂时只能判断进程启动时间
   foreach(Process myProcess in myProcesses)
   {
    startTime = myProcess.StartTime;

    if(startTime > beforeTime && startTime < afterTime)
    {
     myProcess.Kill();
    }
   }
   


  }

原创粉丝点击