zoukankan      html  css  js  c++  java
  • C#创建excel并释放资源

    using System;
    using Microsoft.Office.Interop.Excel;
    using Excel = Microsoft.Office.Interop.Excel;
    using System.IO;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    public class Class2
        {
            private string filePath;
            private Excel.Application app = null;
            private Workbook wb;
            private Worksheet ws;
            private Workbooks wbs;
            /// <summary>
            /// 创建
            /// </summary>
            /// <param name="filePath"></param>
            public void create(string filePath)
            {
                this.filePath = filePath;
                app = new Excel.Application();
                if (app == null)
                {
                    Out.show("无法创建Excel对象,可能您的电脑未安装Excel!");
                    app.Quit();
                    GC.Collect();
                    return;
                }
                try
                {
                    app.Visible = false;
                    app.Application.DisplayAlerts = false;
                }
                catch (Exception e)
                {
    
                }
                wbs = app.Workbooks;
                wb = wbs.Add(Excel.XlWBATemplate.xlWBATWorksheet);
                ws = wb.Worksheets[1] as Worksheet;
            }
    
            public void Write()
            {
                ws.Cells[1, 1] = "hello";
            }
            /// <summary>
            /// 保存文件
            /// </summary>
            public void Save()
            {
                while (File.Exists(filePath))
                {
                    try
                    {
                        File.Delete(filePath);
                        break;
                    }
                    catch (Exception e)
                    {
                        if (MessageBox.Show("该文件已被占用,请关闭", "提示", MessageBoxButtons.RetryCancel)
                            != DialogResult.Retry)
                        {
                            break;
                        };
                    }
                }
                wb.Saved = true;
                wb.SaveAs(filePath);
                Kill();
            }
            [DllImport("User32.dll", CharSet = CharSet.Auto)]
            public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
            /// <summary>
            /// 杀掉对应进程,释放资源
            /// </summary>
            public void Kill()
            {
                int processId;
                GetWindowThreadProcessId(new IntPtr(app.Hwnd), out processId);
                Out.show(processId.ToString());
                System.Diagnostics.Process.GetProcessById(processId).Kill();
            }
        }
  • 相关阅读:
    PCB 铺铜 转载
    VC++ 学习笔记3 获取编辑框字符串
    VC++ 学习笔记2 列表框添加字符串
    VC++组合框——学习笔记1(组合框选项的添加和无法显示下拉选项)
    微信蓝牙ble记录
    最近遇到的问题与分析还有可能的结果
    注入与以往的开发思路
    abp的权限与导航菜单的关系
    ionic入坑记记录
    abp相关
  • 原文地址:https://www.cnblogs.com/jecyhw/p/4047216.html
Copyright © 2011-2022 走看看