C# 操作Excel相关

问题描述

利用C#向excel文件中预先写入部分数据,然后,利用webBrower打开excel文件,添加数据,但保存总是有问题,主要是Excel进程无法在前一次彻底关闭,困扰了很久,求助大神帮助!

解决方案

解决方案二:
无法在前一次彻底关闭-》什么叫无法彻底关闭?写入保存后,在线程中彻底的Killexcel的进程。
解决方案三:
usingSystem.Runtime.InteropServices;[DllImport("User32.dll",CharSet=CharSet.Auto)]publicstaticexternintGetWindowThreadProcessId(IntPtrhwnd,outintID);protectedvoidButton1_Click(objectsender,EventArgse){Excel.ApplicationClassexcel=newMicrosoft.Office.Interop.Excel.ApplicationClass();excel.Workbooks.Open("d:aaa.xls",Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);IntPtrt=newIntPtr(excel.Hwnd);intk=0;GetWindowThreadProcessId(t,outk);System.Diagnostics.Processp=System.Diagnostics.Process.GetProcessById(k);p.Kill();}

解决方案四:
用npoi代替vba
解决方案五:
引用2楼道玄希言的回复:

usingSystem.Runtime.InteropServices;[DllImport("User32.dll",CharSet=CharSet.Auto)]publicstaticexternintGetWindowThreadProcessId(IntPtrhwnd,outintID);protectedvoidButton1_Click(objectsender,EventArgse){Excel.ApplicationClassexcel=newMicrosoft.Office.Interop.Excel.ApplicationClass();excel.Workbooks.Open("d:aaa.xls",Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);IntPtrt=newIntPtr(excel.Hwnd);intk=0;GetWindowThreadProcessId(t,outk);System.Diagnostics.Processp=System.Diagnostics.Process.GetProcessById(k);p.Kill();}

pia使用的非托管对象需要释放
解决方案六:
一般情况下,excel.book都需要在close之前save一下,然后将book和app都设置为null我操作excel和word基本都不会有进程残留啊

时间: 2024-08-02 00:04:16

C# 操作Excel相关的相关文章

ASP操作Excel相关技术总结_应用技巧

目录  一. 环境配置  二. ASP对Excel的基本操作  三. ASP操作Excel生成数据表  四. ASP操作Excel生成Chart图  五. 服务器端Excel文件浏览.下载.删除方案  六. 附录  正文  一. 环境配置  服务器端的环境配置从参考资料上看,微软系列的配置应该都行,即:  1.Win9x+PWS+Office  2.Win2000 Professional+PWS+Office  3.Win2000 Server+IIS+Office  目前笔者测试成功的环境是

ASP操作Excel相关技术总结

目录  一. 环境配置  二. ASP对Excel的基本操作  三. ASP操作Excel生成数据表  四. ASP操作Excel生成Chart图  五. 服务器端Excel文件浏览.下载.删除方案  六. 附录 正文  一. 环境配置  服务器端的环境配置从参考资料上看,微软系列的配置应该都行,即:  1.Win9x+PWS+Office  2.Win2000 Professional+PWS+Office  3.Win2000 Server+IIS+Office  目前笔者测试成功的环境是后

php实现的操作excel类详解_php技巧

本文实例讲述了php实现的操作excel类.分享给大家供大家参考,具体如下: <?php class Excel { static $instance=null; private $excel=null; private $workbook=null; private $workbookadd=null; private $worksheet=null; private $worksheetadd=null; private $sheetnum=1; private $cells=array()

JS操作Excel小例

遇到一个功能开发,直接在页面上点击一个按钮,然后弹出一个excel,进入"打印预览"模式,用js实现,具体如下: //新建一个excel文件,设置为打印预览  function xlPrint(){    var xlApp;//存放Excel对象    var xlBook;//存放Excel工件簿文件    var xlSheet;//存放Excel活动工作表    try{      xlApp = new ActiveXObject("Excel.Applicatio

在SWT中使用OLE操作Excel(三)——设置单元格背景色

packagecom.jrkui.example.excel; import org.eclipse.swt.SWT;import org.eclipse.swt.layout.FillLayout;import org.eclipse.swt.ole.win32.OLE;import org.eclipse.swt.ole.win32.OleAutomation;import org.eclipse.swt.ole.win32.OleClientSite;import org.eclipse.

ADO.NET操作Excel的限制

 ADO.NET操作Excel的限制: 1.不能设置单元格格式,插入新行会自动继承上一行的格式 . ? 2.您不能使用 ADO.NET 在单元格中插入公式. ? 3.Jet OLE DB 提供程序无法为 Excel 工作簿中的表提供主键/索引信息.因此,您不能使用 CommandBuilder 在 Excel 工作簿中自动更新和插入记录. // 连接字符串 string xlsPath = @"d:PathFileName.XLS"; // 绝对物理路径 string connStr

Java使用poi操作excel实例解析_java

本文实例为大家分享了Java使用poi操作excel的具体代码,供大家参考,具体内容如下 依赖poi的jar包,pom.xml配置如下: <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0h

python中使用xlrd、xlwt操作excel表格详解_python

最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究"(用此一词让我觉得颇有成就感)之后,基本解决了日常所需.主要记录使用过程的常见问题及解决. python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库.可从这里下载https://pypi.python.org/pypi.下面分别记录python

java操作excel的方法_java

本文实例讲述了java操作excel的方法.分享给大家供大家参考.具体如下: WritableWorkbook workbook = Workbook.createWorkbook(new File("d:\\output.xls")); WritableSheet sheet = workbook.createSheet("项目简报", 0); //样式 WritableFont sonti18font = new WritableFont(WritableFon