0

我有一个非常大的 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# 中创建的书签的代码

有什么帮助吗?问候

4

1 回答 1

0

如果您提取 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();

}


}
}
于 2016-08-01T03:14:44.070 回答