永发信息网

c# 导出数据到Excel程序关闭后 进程中有个EXCEL进程怎么自动关闭

答案:2  悬赏:50  手机版
解决时间 2021-03-23 04:14
  • 提问者网友:你给我的爱
  • 2021-03-22 19:49
程序如下
[code=csharp]
private void button3_Click(object sender, EventArgs e)

Microsoft.Office.Interop.Excel._Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(Type.Missing));
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
sheet.Name = "订单信息";
for (int i = 0; i <= 5; i++)
{
excel.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
}
for (int i = 0; i <= allrows; i++)
{
for (int j = 0; j <= 5; j++)
{
excel.Cells[i + 2, j + 1] = alldingdan[i, j];
}
}
[/code]

Range allColumn = excel.Columns;
allColumn.AutoFit();
string morenlujing = AppDomain.CurrentDomain.BaseDirectory;
MessageBox.Show(morenlujing + "");
System.IO.File.Delete(morenlujing + "订单信息.xls");
sheet.SaveAs(morenlujing + "订单信息.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);
book.Close(false, Type.Missing, Type.Missing);
books.Close();
excel.Quit();
最佳答案
  • 五星知识达人网友:一叶十三刺
  • 2021-03-22 21:01
1、应该有个application实例,请销毁
2、如果实在不行,就用进程杀一下
全部回答
  • 1楼网友:長槍戰八方
  • 2021-03-22 21:45
//导出excel的方法 
 private void exportexcel() 
 { 
 dataset ds=dtsselect;//数据源 
 if(ds==null) return; 
 
 string savefilename=""; 
 bool filesaved=false; 
 savefiledialog savedialog=new savefiledialog(); 
 savedialog.defaultext ="xls"; 
 savedialog.filter="excel文件|*.xls"; 
 savedialog.filename ="sheet1"; 
 savedialog.showdialog(); 
 savefilename=savedialog.filename; 
 if(savefilename.indexof(":")<0) return; //被点了取消 
 
 excel.application xlapp=new excel.application(); 
 
 if(xlapp==null) 
 { 
 messagebox.show("无法创建excel对象,可能您的机子未安装excel"); 
 return; 
 } 
 
 excel.workbooks workbooks=xlapp.workbooks; 
 excel.workbook workbook=workbooks.add(excel.xlwbatemplate.xlwbatworksheet); 
 excel.worksheet worksheet=(excel.worksheet)workbook.worksheets[1];//取得sheet1 
 //写入字段 
 for(int i=0;i<ds.tables[0].columns.count;i++) 
 { 
 worksheet.cells[1,i+1]=ds.tables[0].columns[i].columnname; 
 } 
 //写入数值 
 
 for(int r=0;r<ds.tables[0].rows.count;r++) 
 { 
 for(int i=0;i<ds.tables[0].columns.count;i++) 
 { 
 worksheet.cells[r+2,i+1]=ds.tables[0].rows[r][i]; 
 } 
 system.windows.forms.application.doevents(); 
 } 
 worksheet.columns.entirecolumn.autofit();//列宽自适应。 
 if(cmbxtype.text!="notification") 
 { 
 excel.range rg=worksheet.get_range(worksheet.cells[2,2],worksheet.cells[ds.tables[0].rows.count+1,2]); 
 rg.numberformat="00000000"; 
 } 
 if(savefilename!="") 
 { 
 try 
 { 
 workbook.saved =true; 
 workbook.savecopyas(savefilename); 
 filesaved=true; 
 } 
 catch(exception ex) 
 { 
 filesaved=false; 
 messagebox.show("导出文件时出错,文件可能正被打开!\n"+ex.message); 
 } 
 } 
 else 
 { 
 filesaved=false; 
 } 
 xlapp.quit(); 
 gc.collect();//强行销毁 
 if(filesaved && system.io.file.exists(savefilename)) system.diagnostics.process.start(savefilename); //打开excel 
 } 
需要在com组件中找到microsoft excel 11.0(不同的版本可能不同,我的是office2003).添加该引用。然后在程序中using excel; 
(vs2008:using microsoft.office.interop.excel;)
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯