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. Prism patterns & practices Developer Center

    Prism https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ff648465(v=pandp.10) Prism provides ...

  2. qqbot 配置

    qqbot 配置 用起来还是挺方便的,使用 pip install qqbot 就可以. 不过找配置文件没注意,以为是在程序目前,原来是在 C:\Users\xxx.qqbot-tmp 目录. 插件可 ...

  3. 3、MR开发入门

    1.预先准备2个文件file1.txt和file2.txt.文件内容为网页上摘下,不具有代表性,只为举例. file1.txt内容为: With this setup, whenever you ch ...

  4. k8s1.4.3安装实践记录(3)下载基础镜像

    下载基础镜像,因为Google被墙,所以我们用时速云中的镜像来tag docker pull index.tenxcloud.com/google_containers/pause-amd64:3.0 ...

  5. 后台判断ajax请求的请求后字段

    headers设置:X-Requested-With:XMLHttpRequest    后台我就是根据这个来判断的

  6. 线程组ThreadGroup

      ThreadGroup线程组表示一个线程的集合.此外,线程组也可以包含其他线程组. 线程组构成一棵树,在树中,除了初始线程组外,每个线程组都有一个父线程组. 允许线程访问有关自己的线程组的信息,但 ...

  7. 北京师范大学第十六届程序设计竞赛决赛 C萌萌哒身高差

    链接:https://www.nowcoder.com/acm/contest/117/C来源:牛客网 萌萌哒身高差 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他 ...

  8. Java - 将 List 等分(最后一部分处理多余部分)

    背景 今天由于要使用多线程,所以事先需要确定启动线程个数.于是需要先将集合进行分配,确定线程的个数. 解决方案 首先是实现 public static <T> List<List&l ...

  9. 【HDU】2222 Keywords Search(AC自动机)

    题目 传送门:QWQ 分析 $ AC $自动机模板,黈力的码风真的棒极了,这是我抄他的. 还有 题号不错 代码 #include <cstdio> #include <cstring ...

  10. 将子类对象引用赋值给超类对象 JAVA 编译时多态性

    将子类对象引用赋值给超类对象 JAVA 编译时多态性(转) (2012-05-10 11:24:05) 转载▼ 标签: 杂谈 分类: 也无晴_soft 1.通过将子类对象引用赋值给超类对象引用变量来实 ...