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

}

Excel另存为_有些Excel打开时会出现一些提示的更多相关文章

  1. 将Excel另存为CSV格式文件

    直接将Excel另存为CSV,速度很快: $CurrentPath = $MyInvocation.MyCommand.Path.substring(0,$MyInvocation.MyCommand ...

  2. C#_简单Excel导入

    引用程序集 Microsoft.Office.Core Microsoft.Office.Interop.Excel using System; using System.Collections.Ge ...

  3. [计算机故障处理]EXCEL文件双击不能直接打开

    同事的电脑里的EXCEL文件不知什么原因双击不能直接打开了,双击只能打开软件而且是没有任何表格的,但通过软件中的“打开”再找到指定的文件能打开. 解决方案: 打开excel,依次选择:工具-选项-常规 ...

  4. Python: 如何判断远程服务器上Excel文件是否被人打开

    最近工作中需要去判断远程服务器上的某个Excel文件是否被打开,如果被人打开,则等待,如果没人打开使用,则去填写数据进Excel文件. 开始想的很简单,和其他语言一样,比如C#,打开文件,如果报错说明 ...

  5. Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

    Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...

  6. 办公软件-Excel:Microsoft Office Excel 2003百科

    ylbtech-办公软件-Excel:Microsoft Office Excel 2003百科 Microsoft® Office Excel 2003 是一种电子表格程序,可提供对于 XML 的支 ...

  7. 浅谈Excel开发:九 Excel 开发中遇到的常见问题及解决方法

    Excel开发过程中有时候会遇到各种奇怪的问题,下面就列出一些本人在开发中遇到的一些比较典型的问题,并给出了解决方法,希望对大家有所帮助. 一 插件调试不了以及错误导致崩溃的问题 在开发机器上,有时可 ...

  8. 浅谈Excel开发:三 Excel 对象模型

    前一篇文章介绍了Excel中的菜单系统,在创建完菜单和工具栏之后,就要着手进行功能的开发了.不论您采用何种方式来开发Excel应用程序,了解Excel对象模型尤其重要,这些对象是您与Excel进行交互 ...

  9. ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 try.dot.net 的正确使用姿势 .Net NPOI 根据excel模板导出excel、直接生成excel .Net NPOI 上传excel文件、提交后台获取excel里的数据

    ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案   ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不 ...

随机推荐

  1. java中如何高效的判断数组中是否包含某个元素---

    package zaLearnpackage; import org.apache.commons.lang3.ArrayUtils; import java.util.Arrays; import ...

  2. 第一次Sprint团队贡献分

    201406114105       董婷婷           21 201406114157       容杰龙           22 201406114343       卓炜杰       ...

  3. toxiproxy 安装试用

    备注:    实际上是一个代理工具,但是又不是简单的进行代理(tcp,可以配置策略,toxics 实现延迟,模拟故障,    对于这个大家可能了解的就是netflix 公司的chaos monkey, ...

  4. Docker-Compose API too old for Windows

    I was working on some code with a Docker Windows container today and ran into this error message: ER ...

  5. ZBar的简单使用

    NSRunLoop类声明的编程接口用于管理输入源对象.一个NSRunLoop对象处理像来自窗体系统中的鼠标和键盘事件,NSPORT对象和NSConnection连接对象这类的输入源.一个NSRunLo ...

  6. 对两个奇葩的C语言程序的思考

    原文章的连接为:http://www.cnblogs.com/jacksu-tencent/default.html?page=2 1. 第一个程序例如以下: #include <stdio.h ...

  7. hadoop之 HDFS fs 命令总结

    版本:Hadoop 2.7.4 -- 查看hadoop fs帮助信息[root@hadp-master sbin]# hadoop fsUsage: hadoop fs [generic option ...

  8. Java 设计模式之单例模式(一)

    原文地址:Java 设计模式之单例模式(一) 博客地址:http://www.extlight.com 一.背景 没有太多原由,纯粹是记录和总结自己从业以来经历和学习的点点滴滴. 本篇内容为 Java ...

  9. Sql Server 2012 存储过程的调试

    [一]Sql Server 关于存储过程调试SQL2000是在查询分析器中的对象浏览器中选中需要调试的存储过程,右键----调试---输入参数开始调试.sqlserver2008中则完全不同,变成了必 ...

  10. WPF ComboBox下拉绑定Treeview 功能的实现

    因为项目需要,接触到这个功能点,借助网络还有自己的一点摸索,实现了这个功能.相关代码如下: XAML部分的代码: <ComboBox Grid.Row=" RenderTransfor ...