zoukankan      html  css  js  c++  java
  • Excel另存为_有些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 System.IO;
    using System.Reflection;
    using System.Runtime.InteropServices;

    namespace DealWithHtml
    {
        public partial class Excel另存为 : Form
        {
            public Excel另存为()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                //this.textBox1.Text = "";
                FolderBrowserDialog openFileDialog = new FolderBrowserDialog();

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    this.textBox1.Text = openFileDialog.SelectedPath;
                }
                else
                {
                    return;
                }

                DirectoryInfo dir = new DirectoryInfo(this.textBox1.Text.Trim());
                FileInfo[] files = GetFiles(); //dir.GetFiles("*.xls"); //GetFiles();
                if (files == null || files.Length == 0) return;

                string file = openFileDialog.SelectedPath + "\另存为文件夹";
                if (Directory.Exists(file))
                {
                    MessageBox.Show("你已经另存为过了,若想再另存为,请先移动或删除此文件夹!");
                    return;
                }

                //File.Create(file);
                Directory.CreateDirectory(file);

                object missing = Missing.Value;
                Microsoft.Office.Interop.Excel.Application appExcel = null;//实例Excel类
                appExcel = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook workbook = null;
                try
                {
                    foreach (FileInfo info in files)
                    {
                        workbook = null;
                        appExcel.DisplayAlerts = false;//DisplayAlerts 属性设置成 False,就不会出现这种警告。                
                        workbook = appExcel.Workbooks.Open(info.FullName,
                            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);//打开Excel

                        string name = info.Name.Substring(0, info.Name.LastIndexOf('.')); // info.Name + "_SaveAs";
                        name = file + "\" + name + "_SaveAs" + (info.Extension == ".xml" ? ".xls" : info.Extension);
                        workbook.SaveAs(name, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, missing, missing, missing, missing,
                            Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive
                            , missing, missing, missing, missing, missing);
                    }
                    MessageBox.Show("另存为完成!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    Kill(appExcel);
                }
            }

            [DllImport("User32.dll", CharSet = CharSet.Auto)]
            public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
            public static void Kill(Microsoft.Office.Interop.Excel.Application excel)
            {
                IntPtr t = new IntPtr(excel.Hwnd);   //得到这个句柄,具体作用是得到这块内存入口  
                int k = 0;
                GetWindowThreadProcessId(t, out k);   //得到本进程唯一标志k 
                System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);   //得到对进程k的引用 
                p.Kill();     //关闭进程k 
                GC.Collect();
            }

            private FileInfo[] GetFiles()
            {
                List<FileInfo> list = new List<FileInfo>();
                DirectoryInfo dir = new DirectoryInfo(this.textBox1.Text.Trim());
                FileInfo[] files1 = dir.GetFiles("*.xls");
                FileInfo[] files2 = dir.GetFiles("*.xml");
                list.AddRange(files1.ToList());
                list.AddRange(files2.ToList());

                return list.ToArray();
            }
        }


    }

  • 相关阅读:
    常用命令
    添加云存储服务
    安装Prometheus-Opeartor
    Prometheus-operator架构详解
    Helm使用详解
    莫队乱搞--BZOJ2038: [2009国家集训队]小Z的袜子(hose)
    BZOJ1443: [JSOI2009]游戏Game
    BZOJ2006: [NOI2010]超级钢琴
    BZOJ4408: [Fjoi 2016]神秘数
    hdu6110:路径交
  • 原文地址:https://www.cnblogs.com/pnljs/p/3178131.html
Copyright © 2011-2022 走看看