下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、本文由xawzd贡献 最近,做了几个月的WinForm,接触了一些进程、句柄方面的知识。于是试着解决一下,没想到成功了,创建的Excel进程被成功Kill掉了,而其它Excel进程则相安无事。 关键代码: 查看源代码打印?public void KillSpecialExcel() try if (m_objExcel != null) int lpdwProcessId; GetWindowThreadProcessId(new IntPtr(m_objExcel.Hwnd), out lpdwProcessId); System.Diagnostics.Process.GetProcess
2、ById(lpdwProcessId).Kill(); catch (Exception ex) Console.WriteLine("Delete Excel Process Error:" + ex.Message); 操作生成Excel的全部代码: 查看源代码打印?using System; using System.Collections.Generic; using System.Text; using System.Reflection; using System.Runtime.InteropServices; using System.Drawing; na
3、mespace VeryCodes.Common.MyExcel / / ExcelClass 的摘要说明。 / public class MSExcel / / 构建ExcelClass类 / public MSExcel() /别忘了需要添加Excel Library的引用 this.m_objExcel = new Microsoft.Office.Interop.Excel.Application(); / / 构建ExcelClass类 / / Excel.Application public MSExcel(Microsoft.Office.Interop.Excel.Applic
4、ation objExcel) this.m_objExcel = objExcel; / / 列标号,Excel最大列数是256 / private string ALists = new string "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", &q
5、uot;N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG&qu
6、ot;, "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY", "A
7、Z", "BA", "BB", "BC", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BK", "BL", "BM", "BN", "BO", "BP", "BQ", "BR", &qu
8、ot;BS", "BT", "BU", "BV", "BW", "BX", "BY", "BZ", "CA", "CB", "CC", "CD", "CE", "CF", "CG", "CH", "CI", "CJ", "CK",
9、 "CL", "CM", "CN", "CO", "CP", "CQ", "CR", "CS", "CT", "CU", "CV", "CW", "CX", "CY", "CZ", "DA", "DB", "DC", "DD&qu
10、ot;, "DE", "DF", "DG", "DH", "DI", "DJ", "DK", "DL", "DM", "DN", "DO", "DP", "DQ", "DR", "DS", "DT", "DU", "DV", "D
11、W", "DX", "DY", "DZ", "EA", "EB", "EC", "ED", "EE", "EF", "EG", "EH", "EI", "EJ", "EK", "EL", "EM", "EN", "EO", &qu
12、ot;EP", "EQ", "ER", "ES", "ET", "EU", "EV", "EW", "EX", "EY", "EZ", "FA", "FB", "FC", "FD", "FE", "FF", "FG", "FH",
13、 "FI", "FJ", "FK", "FL", "FM", "FN", "FO", "FP", "FQ", "FR", "FS", "FT", "FU", "FV", "FW", "FX", "FY", "FZ", "GA&qu
14、ot;, "GB", "GC", "GD", "GE", "GF", "GG", "GH", "GI", "GJ", "GK", "GL", "GM", "GN", "GO", "GP", "GQ", "GR", "GS", "G
15、T", "GU", "GV", "GW", "GX", "GY", "GZ", "HA", "HB", "HC", "HD", "HE", "HF", "HG", "HH", "HI", "HJ", "HK", "HL", &qu
16、ot;HM", "HN", "HO", "HP" , "HQ", "HR", "HS", "HT", "HU", "HV", "HW", "HX", "HY", "HZ", "IA", "IB", "IC", "ID", "IE"
17、, "IF", "IG", "IH", "II", "IJ", "IK", "IL", "IM", "IN", "IO", "IP", "IQ", "IR", "IS", "IT", "IU", "IV" ; / / 获取描述区域的字符 / / / / pub
18、lic string GetAix(int x, int y) if (x > 256) return "" string s = "" s = s + AListsx - 1.ToString(); s = s + y.ToString(); return s; / / 给单元格赋值1 / / 行号 / 列号 / 对齐(CENTER、LEFT、RIGHT) / 值 public void setValue(int y, int x, string align, string text) Microsoft.Office.Interop.Excel
19、.Range range = sheet.get_Range(this.GetAix(x, y), miss); range.set_Value(miss, text); if (align.ToUpper() = "CENTER") range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; if (align.ToUpper() = "LEFT") range.HorizontalAlignment = Microsoft.Office.Int
20、erop.Excel.XlHAlign.xlHAlignLeft; if (align.ToUpper() = "RIGHT") range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight; / / 给单元格赋值2 / / 行号 / 列号 / 值 public void setValue(int y, int x, string text) Microsoft.Office.Interop.Excel.Range range = sheet.get_Range(this.
21、GetAix(x, y), miss); range.set_Value(miss, text); / / 给单元格赋值3 / / 行号 / 列号 / 值 / 字符格式 / 颜色 public void setValue(int y, int x, string text, System.Drawing.Font font, int color) this.setValue(x, y, text); Microsoft.Office.Interop.Excel.Range range = sheet.get_Range(this.GetAix(x, y), miss); range.Font.
22、Size = font.Size; range.Font.Bold = font.Bold; /这里是int型的颜色 range.Font.Color = ColorTranslator.ToOle(ColorTranslator.FromWin32(color); range.Font.Name = font.Name; range.Font.Italic = font.Italic; range.Font.Underline = font.Underline; / / 给单元格赋值3 / / 行号 / 列号 / 值 / 字符格式 / 颜色 public void setValue(int
23、y, int x, string text, System.Drawing.Font font, int color, string align) this.setValue(x, y, text); Microsoft.Office.Interop.Excel.Range range = sheet.get_Range(this.GetAix(x, y), miss); range.Font.Size = font.Size; range.Font.Bold = font.Bold; /这里是int型的颜色 range.Font.Color = ColorTranslator.ToOle(C
24、olorTranslator.FromWin32(color); range.Font.Name = font.Name; range.Font.Italic = font.Italic; range.Font .Underline = font.Underline; if (align.ToUpper() = "CENTER") range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; if (align.ToUpper() = "LEFT")
25、 range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; if (align.ToUpper() = "RIGHT") range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight; / / 插入新行 / / 模板行号 public void insertRow(int y) Microsoft.Office.Interop.Excel.Range range = s
26、heet.get_Range(GetAix(1, y), GetAix(255, y); range.Copy(miss); range.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, miss); range.get_Range(GetAix(1, y), GetAix(255, y); range.Select(); sheet.Paste(miss, miss); / / 把剪切内容粘贴到当前区域 / public void paste() string s = "a,b,c,d,e,f,g" she
27、et.Paste(sheet.get_Range(this.GetAix(10, 10), miss), s); / / 设置边框 / / / / / / public void setBorder(int x1, int y1, int x2, int y2, int Width) Microsoft.Office.Interop.Excel.Range range = sheet.get_Range(this.GetAix(x1, y1), this.GetAix(x2, y2); range.Borders.Weight = Width; public void mergeCell(in
28、t x1, int y1, int x2, int y2) Microsoft.Office.Interop.Excel.Range range = sheet.get_Range(this.GetAix(x1, y1), this.GetAix(x2, y2); range.Merge(true); public Microsoft.Office.Interop.Excel.Range getRange(int x1, int y1, int x2, int y2) Microsoft.Office.Interop.Excel.Range range = sheet.get_Range(th
29、is.GetAix(x1, y1), this.GetAix(x2, y2); return range; private object miss = Missing.Value; /忽略的参数OLENULL private Microsoft.Office.Interop.Excel.Application m_objExcel; /Excel应用程序实例 private Microsoft.Office.Interop.Excel.Workbooks m_objBooks; /工作表集合 private Microsoft.Office.Interop.Excel.Workbook m_o
30、bjBook; /当前操作的工作表 private Microsoft.Office.Interop.Excel.Worksheet sheet; /当前操作的表格 public Microsoft.Office.Interop.Excel.Worksheet CurrentSheet get return sheet; set this.sheet = value; public Microsoft.Office.Interop.Excel.Workbooks CurrentWorkBooks get return this.m_objBooks; set this.m_objBooks =
31、 value; public Microsoft.Office.Interop.Excel.Workbook CurrentWorkBook get return this.m_objBook; set this.m_objBook = value; / / 打开Excel文件 / / 路径 public void OpenE xcelFile(string filename) UserControl(false); m_objExcel.Workbooks.Open(filename, miss, miss, miss, miss, miss, miss, miss, miss, miss,
32、 miss, miss, miss, miss, miss); m_objBooks = (Microsoft.Office.Interop.Excel.Workbooks)m_objExcel.Workbooks; m_objBook = m_objExcel.ActiveWorkbook; sheet = (Microsoft.Office.Interop.Excel.Worksheet)m_objBook.ActiveSheet; public void UserControl(bool usercontrol) if (m_objExcel = null) return; m_objE
33、xcel.UserControl = usercontrol; m_objExcel.DisplayAlerts = usercontrol; m_objExcel.Visible = usercontrol; public void CreateExceFile() UserControl(false); m_objBooks = (Microsoft.Office.Interop.Excel.Workbooks)m_objExcel.Workbooks; m_objBook = (Microsoft.Office.Interop.Excel.Workbook)(m_objBooks.Add
34、(miss); sheet = (Microsoft.Office.Interop.Excel.Worksheet)m_objBook.ActiveSheet; public void SaveAs(string FileName) m_objBook.SaveAs(FileName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalS
35、essionChanges, miss, miss, miss, miss); /m_objBook.Close(false, miss, miss); public void ReleaseExcel() m_objExcel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(object)m_objExcel); System.Runtime.InteropServices.Marshal.ReleaseComObject(object)m_objBooks); System.Runtime.InteropSer
36、vices.Marshal.ReleaseComObject(object)m_objBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(object)sheet); sheet = null; m_objBook = null; m_objBooks = null; m_objExcel = null; GC.Collect(0); #region KillAllExcel public bool KillAllExcel() try if (m_objExcel != null) / isRunning是判断xlAp
37、p是怎么启动的flag. m_objExcel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel); /释放COM组件,其实就是将其引用计数减1 /System.Diagnostics.Process theProc; foreach (System.Diagnostics.Process theProc in System.Diagnostics.Process.GetProcessesByName("EXCEL") /先关闭图形窗口。如果关闭失败有的时候在状态里看不到图形窗口的excel了, /但是在进程里仍然有EXCEL.EXE的进程存在,那么就需要杀掉它:p if (theProc.CloseMainWindow() = false) theProc.Kill(); m_objExcel = null
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《弟子规》完整拼音对照版
- 消化性溃疡讲课
- 文学社社团活动总结范文(3篇)
- 快递柜和快递员合同模板6篇
- 高二政治备课组工作计划(10篇)
- 关于人生演讲稿范文汇编(3篇)
- 2024届浙江省杭州市9+1高中联盟高三实验班暑期第一次月考数学试题
- 绝经后ER晚期乳腺癌内分泌治疗指南解读
- 鄂教版科学三下《蚕蛹变成了什么》课件
- 新版浙教版2024-2025学年度八年级数学上学期期末综合素质评价二含答案
- 第1-4单元测试卷(单元测试)-2024-2025学年四年级上册数学人教版
- GB/T 44489-2024高级辅助驾驶地图审查要求
- 2024-2030年中国氢能源行业发展趋势与项目投资专项调研报告
- GB/T 34370.12-2024游乐设施无损检测第12部分:螺栓相控阵超声检测
- 2023--2024学年苏少版七上综合实践教案
- 《婴幼儿常见病识别与预防》课件-婴幼儿湿疹
- 坦克介绍英语解读
- 光伏发电项目投资合同三篇
- 2024版民政局办理离婚的离婚协议书
- 2024年秋新人教版七年级上册数学教学课件 第三章 代数式 数学活动
- Unit2 Sports and Fitness Lesson 3教学设计-2023-2024学年高中英语北师大版(2019)必修第一册
评论
0/150
提交评论