zoukankan      html  css  js  c++  java
  • 新建并保存一个空的Excel

    测试用的

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Application = Microsoft.Office.Interop.Excel.Application;
    using Microsoft.Office.Interop.Excel;
    using System.IO;
    using System.Reflection;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    
    namespace ExcelTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            string fileName;
            Workbook workbook;
            object missing = Missing.Value; 
            Application app;
    
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    app = new Application()
                    {
                        Visible = false
                    };
                    workbook = app.Workbooks.Add();
                    fileName = System.Windows.Forms.Application.StartupPath + "\excel.xlsx";
                    SaveAsFile();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
            public void SaveAsFile()
            {
                if (string.IsNullOrEmpty(fileName))
                {
                    throw new Exception("没有指定输出文件路径!");
                }
    
                XlFileFormat fileFormat;
                if (String.Compare(Path.GetExtension(fileName).ToLower(), ".xlsx", false) == 0)
                {
                    fileFormat = XlFileFormat.xlWorkbookDefault; //Excel 2007版本 
                }
                else
                {
                    fileFormat = XlFileFormat.xlAddIn8;//Excel 2003版本 
                }
    
                try
                {
                    //workbook.Save();
                    workbook.SaveAs(fileName, fileFormat, missing, missing, missing, missing, XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);
                }
                catch (Exception ex)
                {
                   // ExceptionLog.Instance.WriteLog(ex, LogType.UI);
                    throw;
                }
                finally
                {
                    Dispose();
                }
            }
    
            private void Dispose()
            {
                int appHwnd = 0;
                try
                {
                    if (workbook != null)
                    {
                        workbook.Close(true, missing, missing);
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
                        workbook = null;
                    }
                    if (app != null)
                    {
                        appHwnd = app.Hwnd;
                        app.Workbooks.Close();
                        app.Quit();
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
                        app = null;
                    }
                }
                catch (Exception ex)
                {
                    //ExceptionLog.Instance.WriteLog(ex, LogType.UI);
                    throw ex;
                }
                GC.Collect();
                if (appHwnd > 0)
                {
                    KillExcelProcess(appHwnd);
                }
            }
    
    
            [DllImport("User32.dll", CharSet = CharSet.Auto)]
            private static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
            private static void KillExcelProcess(int appHwnd)
            {
                Process[] ps = Process.GetProcesses();
                IntPtr t = new IntPtr(appHwnd); //得到这个句柄,具体作用是得到这块内存入口 
                int ExcelID = 0;
                GetWindowThreadProcessId(t, out ExcelID); //得到本进程唯一标志
                foreach (Process p in ps)
                {
                    if (p.ProcessName.ToLower().Equals("excel"))
                    {
                        if (p.Id == ExcelID)
                        {
                            p.Kill();
                        }
                    }
                }
            }
        }
    }
  • 相关阅读:
    php面试专题---16、MySQL创建高性能索引考点
    php面试专题---Mysql索引类型、介绍及优点
    php面试专题---Mysql索引原理及SQL优化
    北风设计模式课程---责任链模式 总结
    黑马lavarel教程---2、获取用户输入
    php面试专题---15、MySQL数据库基础考察点
    北风设计模式课程---外观模式、代理模式和中介者模式的区别
    legend3---1、meedu安装
    mysql中utf8和utf8mb4区别
    Struts2基于注解的Action配置
  • 原文地址:https://www.cnblogs.com/chucklu/p/4544902.html
Copyright © 2011-2022 走看看