批量编辑Excel x64.docx

上传人:b****6 文档编号:3333465 上传时间:2022-11-21 格式:DOCX 页数:9 大小:26.85KB
下载 相关 举报
批量编辑Excel x64.docx_第1页
第1页 / 共9页
批量编辑Excel x64.docx_第2页
第2页 / 共9页
批量编辑Excel x64.docx_第3页
第3页 / 共9页
批量编辑Excel x64.docx_第4页
第4页 / 共9页
批量编辑Excel x64.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

批量编辑Excel x64.docx

《批量编辑Excel x64.docx》由会员分享,可在线阅读,更多相关《批量编辑Excel x64.docx(9页珍藏版)》请在冰豆网上搜索。

批量编辑Excel x64.docx

批量编辑Excelx64

批量编辑Excel2010x64

首先添加引用:

usingMicrosoft.Office.Interop.Excel;

usingSystem.Drawing;

usingSystem.IO;

 

代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Office.Interop.Excel;

using System.Drawing;

using System.IO;

namespace ExcelHelp

{

    /// 

    /// Date:

 2010.03.31

    /// Author:

 Mr.Brooks

    /// HomePage:

 

    /// Email:

 undead_47@

    /// 

    class ExcelHelp

    {

        /// 

        /// 将一个工作簿中一个工作表中的内容复制到另一个工作簿的一个工作表中

        /// 

        /// 源工作薄的完整路径

        /// 源工作表名

        /// 目标工作薄的完整路径

        /// 目的工作表名

        private static void fnCopy(string v_strSourceWorkbook, string v_strSourceWorksheet, string v_strDestWorkbook, string v_strDestWorksheet)

        {

            Application app = new Application();

            Workbook srcbook = app.Workbooks.Open(v_strSourceWorkbook);

            Worksheet srcsheet = srcbook.Worksheets[v_strSourceWorksheet];

            Workbook destbook = app.Workbooks.Open(v_strDestWorkbook);

            Worksheet destsheet = destbook.Worksheets[v_strDestWorksheet];

            try

            {

                //Worksheet.UsedRange:

 获取工作表中所使用的范围

                for (int i = 1; i <= srcsheet.UsedRange.Rows.Count; i++)

                {

                    for (int j = 1; j <= srcsheet.UsedRange.Columns.Count; j++)

                    {

                        //将Excel的颜色与RGB联系起来

                        if (srcsheet.Cells[i, j].Interior.Color !

= ColorTranslator.ToOle(Color.FromArgb(255, 204, 153)))

                        {

                            destsheet.Cells[i, j].Value2 = srcsheet.Cells[i, j].Value2;

                        }

                    }

                }

                //屏蔽Excel的提示信息

                app.Application.DisplayAlerts = false;

                destbook.Save();

            }

            catch (Exception ex)

            {

                throw ex;

            }

            finally 

            {

                app.Quit();

                System.Runtime.InteropServices.Marshal.ReleaseComObject(app);

                System.Runtime.InteropServices.Marshal.ReleaseComObject(srcbook);

                System.Runtime.InteropServices.Marshal.ReleaseComObject(srcsheet);

                System.Runtime.InteropServices.Marshal.ReleaseComObject(destbook);

                System.Runtime.InteropServices.Marshal.ReleaseComObject(destsheet);

                app = null;

                srcbook = null;

                destbook = null;

                srcsheet = null;

                destsheet = null;

                GC.Collect();

            }

        }

 

Range.Interior.Color是Office中表示颜色的方法,其返回一个整数,表示一种特定的颜色:

 但是我们平时用的最多的是RGB表示的颜色,可以用

srcsheet.Cells[i, j].Interior.Color !

= ColorTranslator.ToOle(Color.FromArgb(255, 204, 153))

将ColorIndex与RGB进行关联。

 

 

代码

        /// 

        /// 判断一个工作簿的一个工作表有没有添加保护

        /// 

        /// 工作簿的完整路径

        /// 工作表名

        private static void fn判断工作表是否被保护(string v_strDir, string v_strSheetName)

        {

            StringBuilder sb = new StringBuilder();

            Application app = new Application();

            DirectoryInfo dir = new DirectoryInfo(v_strDir);

            try

            {

                //递归查找所有Excel 2007/2010的文件

                foreach (FileInfo f in dir.GetFiles("*.xlsx", SearchOption.AllDirectories))

                {

                    Workbook book = app.Workbooks.Open(f.FullName);

                    if (book.Worksheets[v_strSheetName].ProtectContents)

                    {

                        sb.Append(book.Name + ",");

                    }

                    app.Application.DisplayAlerts = false;

                    book.Save();

                }

            }

            catch (Exception ex)

            {

                throw ex;

            }

            finally

            {

                app.Quit();

                System.Runtime.InteropServices.Marshal.ReleaseComObject(app);

                app = null;

                GC.Collect();

            }

            Console.WriteLine(sb.ToString());

        }

若不知道Excel工作簿或工作表的密码,可以用Passware进行破解,经过测试,Passware可以破解Office2003/2007/2010,其官方主页称可以破解Windows7的BitLocker加密技术。

 

 

代码

        /// 

        /// 判断一个工作表中所有的单元格是否包含公式

        /// 

        /// 工作簿的完整路径

        private static void fn判断单元格是否包含公式(string v_strPath)

        {

            StringBuilder sb = new StringBuilder();

            Application app = new Application();

            DirectoryInfo dir = new DirectoryInfo(v_strPath);

            try

            {

                foreach (FileInfo file in dir.GetFiles("*.xlsx", SearchOption.AllDirectories))

                {

                    Workbook book = app.Workbooks.Open(file.FullName);

                    foreach (Worksheet sheet in book.Worksheets)

                    {

                        for (int i = 1; i < sheet.UsedRange.Rows.Count; i++)

                        {

                            for (int j = 1; j < sheet.UsedRange.Columns.Count; j++)

                            {

                                if (sheet.Cells[i, j].Interior.Color !

= ColorTranslator.ToOle(Color.FromArgb(255, 204, 153)))

                                {

                                    if (sheet.Cells[i, j].HasFormula)

                                    {

                                        sb.Append(book.Name + " " + sheet.Name + " 第" + i.ToString() + "行第" + j.ToString() + "列包含公式!

\n");

                                    }

                                }

                            }

                        }

                    }

                }

            }

            catch (Exception ex)

            {

                throw ex;

            }

            finally

            {

                app.Quit();

                System.Runtime.InteropServices.Marshal.ReleaseComObject(app);

                app = null;

                GC.Collect();

            }

            Console.WriteLine(sb.ToString());

        }

 

 

 

代码

        /// 

        /// 获取指定工作簿的所有工作表

        /// 

        /// 工作簿的完整路径

        private static void fn获取指定工作簿的所有工作表(string v_strDir)

        {

            StringBuilder sb = new StringBuilder();

            Application app = new Application();

            DirectoryInfo dir = new DirectoryInfo(v_strDir);

            try

            {

                foreach (FileInfo f in dir.GetFiles("*.xlsx", SearchOption.AllDirectories))

                {

                    Workbook book = app.Workbooks.Open(f.FullName);

                    sb.Append(book.Name);

                    foreach (Worksheet sheet in book.Worksheets)

                    {

                        sb.Append(sheet.Name + " ");

                    }

                    sb.Append("\n");

                    app.Application.DisplayAlerts = false;

                    book.Save();

                }

            }

            catch (Exception ex)

            {

                throw ex;

            }

            finally

            {

                app.Quit();

                System.Runtime.InteropServices.Marshal.ReleaseComObject(app);

                app = null;

                GC.Collect();

            }

            Console.WriteLine(sb.ToString());

        }

 

 

批量转换Excel的格式

 

代码

        /// 

        /// 批量转换Excel 2003 至Excel 2007/2010格式

        /// 可以修改XlFileFormat枚举的值来转换为想要的格式

        /// 

        /// 工作簿的查找路径

        private static void fn批量转换Excel文件格式(string v_strDir)

        {

            Application app = new Application();

            DirectoryInfo dir = new DirectoryInfo(v_strDir);

            Workbook book;

            app.Application.DisplayAlerts = false;

            try

            {

                foreach (FileInfo fi in dir.GetFiles("*.xls", SearchOption.AllDirectories))

                {

                    book = app.Workbooks.Open(fi.FullName);

                    book.SaveAs(fi.DirectoryName + @"\" + fi.Name.Replace(fi.Extension, string.Empty) + ".xlsx", XlFileFormat.xlOpenXMLWorkbook);

                }

            }

            catch (Exception ex)

            {

                throw ex;

            }

            finally

            {

                app.Quit();

                System.Runtime.InteropServices.Marshal.ReleaseComObject(app);

                app = null;

                GC.Collect();

            }

        }

 

 

比较两个工作表的内容

 

代码

        /// 

        /// 比较两个工作表的内容

        /// 

        /// 第一个工作簿的路径

        /// 第二个工作簿的路径

        private static void fn比较两个工作表的内容(string v_strSourcePath, string v_strDestPath)

        {

            Application app = new Application();

            Workbook srcBook = app.Workbooks.Open(v_strSourcePath);

            Workbook destBook = app.Workbooks.Open(v_strDestPath);

            //记录查找结果到本文文件中

            FileStream fs = new FileStream("log.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);

            StreamWriter sw = new StreamWriter(fs);

            try

            {

                foreach (Worksheet sheet in srcBook.Worksheets)

                {

                    for (int i = 1; i <= sheet.UsedRange.Rows.Count; i++)

           

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1