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();
            }
        }


    }

  • 相关阅读:
    怎么判断自己在不在一家好公司?
    超全!互联网大厂的薪资和职级一览
    Nginx 又一牛 X 功能!流量拷贝
    时间管理之四象限法则
    罗永浩一个坑位卖60万脏钱背后:放下面子赚钱,才是成年人最大的体面
    2020 年 4月全国程序员工资出炉
    一次 SQL 查询优化原理分析(900W+ 数据,从 17s 到 300ms)
    “Hey Siri” 背后的黑科技大揭秘!
    一文讲透高薪的本质!
    python UnicodeDecodeError: 'gbk' codec can't decode byte 0x99 in position 87: illegal multibyte sequence异常解决
  • 原文地址:https://www.cnblogs.com/pnljs/p/3178131.html
Copyright © 2011-2022 走看看