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


}

posted @ 2013-07-08 15:35  pnljs  阅读(1429)  评论(0编辑  收藏  举报