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();
}
}
}
- asp.net excel进程
- ASP.NET控制EXCEL,完全结束进程
- asp.net 导入excel并删除进程
- asp.net调用excel后如何快速杀掉excel进程
- 在ASP.NET中结束EXCEL进程的程序
- ASP.NET调用Excel不能结束进程的解决方法
- Asp.net 中导出excel后终止进程的解决办法
- ASP.NET导出Excel并将Excel进程关闭 (根据进程ID)
- ASP.NET中上传并读取Excel文件数据,附后生成EXCEL及杀掉EXCEL进程。
- ASP.NET中上传并读取Excel文件数据,附后生成EXCEL及杀掉EXCEL进程
- .Net 关闭 Excel 进程
- 在asp.net中操作excel如何关闭excel的进程
- 【总结】终于解决了asp.net下excel导出excel.exe进程无法释放的问题!!
- ASP.NET excel导出 excel.exe进程关闭 !经典写法。绝对实用。
- ASP.NET导入、导出EXCEL中的一些问题汇总(结束EXCEL进程)
- ASP.net转Excel
- ASP.net转Excel
- asp.net与Excel
- 2.6内核下不编译内核给iptables加模块
- 房地产业内幕人士自曝:一套房子会压垮中国
- linUx常用手工入侵检测方法与命令
- 类似163邮件附件选择文件上传
- [心怀感动 收获温暖]首钢水钢集团销售分公司 “感动·温暖”诗歌朗诵会圆满闭幕.
- asp.net excel进程
- NetBeans开发Android的插件
- 容忍到厌倦
- Ophone的程序:Hello, OPhone!
- 8,10,16,36,62进制之间的转换for vb/vbscript
- Dinamica update 2009/08/05
- Dinamica 2009/08/12 update
- 最近苹果开发的书好多,搞得偶也想写书玩儿了。
- 牛人博客