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. anycast简单总结

    一针见血,言简意赅的总结 bgp+anycast就是不同服务器用了相同的ip地址 anycast 技术特点 bgp+anycast就是多个主机使用相同ip地址的一种技术,当报文发给该地址时,根据路由协 ...

  2. gradle build scan 插件使用

    1. 添加插件 build.gradle plugins { id 'com.gradle.build-scan' version '1.10.2' } buildScan { // Uncommen ...

  3. bower.json 的版本范围

    bower.json 的版本范围 有小伙伴问 ~2.2.0 什么意思. 而且在git 的tags 中没有了 2.2.0 版本,怎么样? 实际上 ~2.2.0 的意思是 >=2.2.0 <2 ...

  4. Spring Boot 入门之缓存和 NoSQL 篇(四)

    原文地址:Spring Boot 入门之缓存和 NoSQL 篇(四) 博客地址:http://www.extlight.com 一.前言 当系统的访问量增大时,相应的数据库的性能就逐渐下降.但是,大多 ...

  5. Android 从上层到底层-----kernel层

    CPU:RK3288 系统:Android 5.1 功能:上层 app 控制 led 亮灭 开发板:Firefly RK3288 1.在dts文件中增加 led 设备 path:kernel/arch ...

  6. jQuery实现清空table表格除首行外的所有数据

    1.其实网上有很多版本,试了好几个都不行,最后还是查到了一个非常方便的:不会清除表格第一行表头部分. 其中J_tab_fam是table的id. ? 1 $("#J_tab_fam  tr: ...

  7. Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用

    Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...

  8. struts2学习(3)struts2核心知识II

    一.struts.xml配置: 1.分模块配置方法: 比如某个系统多个模块,我们把资产管理模块和车辆管理模块,分开,在总的struts.xml配置文件中include他们: 工程结构: struts. ...

  9. free 命令结果完全剖析

    free 命令结果完全剖析 total 总物理内存大小. used 已分配的大小,注意,对操作系统来说任何被使用的内存都是used. free 未被分配的物理内存大小. shared 共享内存大小,主 ...

  10. Entity Framework API介绍 -- DbSet<>().Find()

    过去我们常常使用Where或First(FirstOrDefault)方法来查找对应的实体,比如: var query = context.CertInfoMakeDetails.ToList().W ...