【问题标题】:How to access excel bookmarks using C#如何使用 C# 访问 Excel 书签
【发布时间】:2012-12-10 11:37:39
【问题描述】:

我有一个非常大的 excel 文件,我们必须将其导入数据库。导入工作完美,但导入它需要 35 秒。客户等不及了。我在其中创建了书签。

这里是创建书签的链接。 http://www.youtube.com/watch?v=9n4g_l7h8jc

现在我想从 C# 代码中读取该书签。正如我所说,我们有不同的部分,例如: 1. 分布
2. 工作人员 3. 资源 4. 预算等等……

我可以直接从此代码访问此标题/部分,我得到行和列

_Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Range xlRange = xlWorksheet.UsedRange;
var externalDprDistribution = xlRange.Find("Distribution");
var colDist = externalDprDistribution.Column;
var rowDist = externalDprDistribution.Row;

但我仍然想在 excel 工作表中创建书签,然后我想在 c# 代码中访问书签并从那里获取行和列。所以我需要一个可以访问在 c# 中创建的书签的代码

有什么帮助吗? 问候

【问题讨论】:

  • 你应该提到你使用什么库来读取excel文件。
  • 我已经给出了 office dll 的参考,这里是 Microsoft.Office.Interop.Excel;
  • 您所说的书签是指命名范围吗?
  • 如果您只对数据感兴趣,直接访问 xml 而不是通过互操作可能会更快。从 2007 年(可能是 2003 年)开始的 Excel 工作簿只是简单的压缩 XML 文件。

标签: c# excel bookmarks


【解决方案1】:

如果您提取 Excel 文件,您会看到书签是超链接。

<hyperlinks>
  <hyperlink ref="G18" location="Test_Mark1" display="Bookmark1"/>
  <hyperlink ref="G10" r:id="rId1"/>
</hyperlinks>

最近为了一个项目,写了下面的测试代码来查找书签并更新,希望对你有帮助

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System.IO.Packaging;
using System.Xml;
using System.IO;


namespace OpenXMLTest
{
class Program
 {
static void Main(string[] args)
{
    string fileName = @"c:\temp\book1.xlsx";

    Console.WriteLine("Start reading bookmark of excel...");


    using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
    {
        using (SpreadsheetDocument doc = SpreadsheetDocument.Open(fs, true))
        {
            WorkbookPart workbookPart = doc.WorkbookPart;

            //find bookmarks
            foreach (var workSheetPart in workbookPart.WorksheetParts)
            {
                var temp = workSheetPart.RootElement.Descendants<Hyperlinks>();
                IEnumerable<Hyperlink> hyperLinks = null;
                if (temp.Count() > 0)
                {
                    hyperLinks = temp.First().Cast<Hyperlink>();
                }

                var workSheet = workSheetPart.Worksheet;

                var cells = workSheet.Descendants<Cell>();

                //loop each cell, find bookmark
                foreach (var c in cells)
                {

                    if (hyperLinks != null && hyperLinks.Count() > 0)
                    {
                        var hyperLink = hyperLinks.SingleOrDefault(x => x.Reference.Value == c.CellReference.Value);
                        if (hyperLink != null)
                        {

                            if (!string.IsNullOrEmpty(hyperLink.Location))
                            {
                                Console.WriteLine("Bookmark.DisplayName : " + hyperLink.Display);
                                Console.WriteLine("Bookmark.Location : " + hyperLink.Location);


                                //update bookmark

                                hyperLink.Location = "BookMark_Test";
                                hyperLink.Display = "updated bookmark";

                                Console.WriteLine("Bookmark.DisplayName after updated : " + hyperLink.Display);
                                Console.WriteLine("Bookmark.Location after updated : " + hyperLink.Location);
                            }


                            //for normal hyperlinks
                            //var hr = workSheetPart.HyperlinkRelationships.SingleOrDefault(x => x.Id == hyperLink.Id);
                            //if (hr != null)
                            //{
                            //    Console.WriteLine(hr.Uri.ToString());
                            //}
                        }
                    }


                    workSheet.Save();
                }

                workbookPart.Workbook.Save();

            }



        }


    }

    Console.ReadKey();

}


}
}

【讨论】:

    猜你喜欢
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    相关资源
    最近更新 更多