C# 获取Excel里引用的外部其他excel文件清单

关键方法:mySheet.Application.ActiveWorkbook.LinkSources(XlLink.xlExcelLinks);

主要代碼如下

        Application myExcel = null;//引用Excel Application类別
        Workbook myBook = null;//引用活页簿类別
        Worksheet mySheet = null;//引用工作表类別            
        Range myRange = null;//引用Range类別
        string excelPath = "";
 
        public ExcelModify(string filePath)
        {
            excelPath = filePath;
            try
            {
 
                myExcel = new Application();//实例化Excel Application
                myExcel.DisplayAlerts = false;//停用警告
                myExcel.Visible = false;      //Excel 不可见  
                // 3 時,打開excel沒有更新提示,詳情見Office官方說明
                myBook = myExcel.Workbooks._Open(excelPath, "3", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);                          
                // Microsoft.Office.Interop.Excel 引用的屬性 Embed interop type 改成false才可以
                mySheet = (Worksheet)myBook.Worksheets[1];//Excel文件打开工作簿的第一个文件
            }
            catch (Exception ex)
            {
                this.Close();
                throw ex;
            }
        }
        /// <summary>
        /// 將object[*]轉化為object[]
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        private object[] ConvertArray(Array arr)
        {
            int lb = arr.GetLowerBound(0);
            var ret = new object[arr.GetUpperBound(0) - lb + 1];
            for (int ix = 0; ix < ret.Length; ++ix)
            {
                ret[ix] = arr.GetValue(ix + lb);
            }
            return ret;
        }
 
        public void ReadFile()
        {
            string notex = "";
 
            if (mySheet != null && mySheet.Application.ActiveWorkbook != null)
            {
                object oj = mySheet.Application.ActiveWorkbook.LinkSources(XlLink.xlExcelLinks);
                //object[] obj = (object[])oj; // 報錯
                object[] obj = ConvertArray(oj as Array);
 
 
                if (obj != null && obj.Length > 0)
                {
                    for (int i = 0; i < obj.Length; i++)
                    {
                        string fi = (string)obj[i];
                        if (!File.Exists(fi))
                        {
                            notex += fi;
                        }
                    }
                }
            }
        }
 

遍歷外部鏈接的的單元格清單

public Dictionary<string, string> GetSheetLinksData(string xlsPath)
        { 
             Application _appliation=null;
            Workbook _workbook = null;
            Object missing = System.Reflection.Missing.Value;
            Dictionary<string, string> linksData = new Dictionary<string, string>();
            try
            {
                FileInfo xlsInfo = new FileInfo(xlsPath);
                _appliation = new Application();
                _appliation.DisplayAlerts = false;//停用警告
                _appliation.Visible = false;      //Excel 不可见                 
                // 採用 3,和另存為,保證新文件數據是更新后的數據
                _workbook = (Workbook)_appliation.Workbooks.Open(xlsPath, "3", missing, missing, missing, missing, missing, missing,
                                                                           missing, missing, missing, missing, missing, missing, missing);
                Worksheet mySheet = (Worksheet)_workbook.Worksheets[1]; //引用工作表类別  
                try
                {
                    Range cs = mySheet.Cells.SpecialCells(XlCellType.xlCellTypeFormulas);
                    if (cs != null)
                    {
                        foreach (Range c in cs)
                            try
                            {
                                string col = convertToCharacter(c.Column) + (c.Row).ToString();
                                if (c.Formula.ToString() != "" && c.Formula.ToString().Contains("$"))
                                {
                                    linksData.Add(col, c.Formula.ToString());
                                }
                            }
                            catch { }
                    }
                }
                catch
                {
                }                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally {
                if (_workbook != null)
                {
                    //關掉當前sheet
                    _workbook.Close(missing, missing, missing);
                    //關掉excel
                    _appliation.Workbooks.Close();
                    //退出程序
                    _appliation.Quit();
                }
            }
            return linksData;
        }
 
        /// <summary>
        /// 十進制轉化為26進制
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        private string convertToCharacter(int i)
        {
            char[] list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
            StringBuilder sb = new StringBuilder();
            while ((i - 1) / 26 != 0)
            {
                sb.Append(list[i / 26 - 1]);
                i = i % 26;
            }
            i = (i - 1) % 26;
            sb.Append(list[i]);
            return sb.ToString();
        }
 

相关推荐

  1. C# 获取Excel引用外部其他excel文件清单

    2023-12-19 10:08:04       54 阅读
  2. React前端解析excel文件,获取excel文件数据

    2023-12-19 10:08:04       61 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-19 10:08:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-19 10:08:04       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-19 10:08:04       87 阅读
  4. Python语言-面向对象

    2023-12-19 10:08:04       96 阅读

热门阅读

  1. Latex编译出来的pdf文件缺少参考文献和交叉引用

    2023-12-19 10:08:04       48 阅读
  2. 1641:【例 1】矩阵 A×B

    2023-12-19 10:08:04       43 阅读
  3. MySQL

    2023-12-19 10:08:04       49 阅读
  4. 【算法集训】基础数据结构:十、矩阵

    2023-12-19 10:08:04       71 阅读
  5. 登录测试用例详解

    2023-12-19 10:08:04       68 阅读
  6. leetcode242. 有效的字母异位词

    2023-12-19 10:08:04       59 阅读
  7. 怎么有效防护服务器被入侵

    2023-12-19 10:08:04       53 阅读
  8. 第二百一十四回

    2023-12-19 10:08:04       58 阅读
  9. React中渲染html结构---dangerouslySetInnerHTML

    2023-12-19 10:08:04       69 阅读
  10. Linux中命令添加-r的作用

    2023-12-19 10:08:04       66 阅读
  11. 理解并实现C语言中的strcpy函数

    2023-12-19 10:08:04       59 阅读
  12. Docker容器与JVM比较

    2023-12-19 10:08:04       76 阅读