加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C#修改Excel并且保存Excel

(2018-03-28 16:43:20)
分类: OS维护
using System;
using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
//using System.Reflection;
using System.IO;
//参考博客 https://blog.csdn.net/hui_shixu/article/details/5785029
namespace FileNameInject
{
    class Program
    {
        static void Main(string[] args)
        {
            Application app = new Application();
            try
            {
                //让后台执行设置为不可见  
                app.Visible = false;

                string dirName = null;
                if(args.Length > 0)
                {
                    dirName = args[0];
                }
                else
                {
                    dirName = @"C:\Users\l\Desktop\code";
                }
               
                DirectoryInfo inputDir = new DirectoryInfo(dirName);
                FileInfo[] fileInfos = inputDir.GetFiles("*.xls");
                List fileNames = new List();
                for (int i = 0; i < fileInfos.Length; i++)
                {
                    string path = fileInfos[i].FullName;
                    if (path.Contains("~$"))
                    {
                        Console.WriteLine("碰到非法文件:" + path + " 不解析");
                        continue;
                    }
                    else
                    {
                        fileNames.Add(path);
                    }
                }


                for (int i = 0; i < fileNames.Count; i++)
                {
                    string fileName = fileNames[i];

                    Workbook wBook = app.Workbooks.Open(fileName);

                    //取得一个工作表  
                    //如果打开了已有的工作簿,也可以这样获取工作表Worksheet wSheet = wBook.ActiveSheet as Worksheet  
                    Worksheet wSheet = wBook.Worksheets[1] as Worksheet;
                    Range cells = wSheet.Cells;
                    int rowCount = wSheet.UsedRange.Rows.Count;
                    Console.WriteLine(fileName + " 行数:" + rowCount);
                    for (int j = 2; j <= rowCount; j++)
                    {
                        string fileNoExt = System.IO.Path.GetFileName(fileName);
                        Console.WriteLine("     [I:" + j + "]=" + fileNoExt);
                        cells[j, 9] = fileNoExt;
                    }
                    
                    //设置禁止弹出保存和覆盖的询问提示框  
                    app.DisplayAlerts = false;
                    app.AlertBeforeOverwriting = false;
                    //保存工作簿  
                    wBook.Save();
                    wBook.Close();
                    //保存excel文件  
                    // app.Save(fileName);
                }

            }
            catch(Exception ex)
            {
                Console.WriteLine("ex:" + ex.Message);
            }
            finally
            {
                //确保Excel进程关闭  
                app.Quit();
                app = null;
            }
            Console.WriteLine("Done!press any key 2 exit");
            Console.ReadKey();
        }
    }
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有