简体   繁体   English

不使用C#中的for循环来获取单词表的单元格索引的最佳方法是什么?

[英]What is the best way to get cell index of a word table without using for loop in c#?

I am getting the index of the cell of a word table using for loop which takes a lot of time for bigger tables, is there any way to do this without for loop? 我正在使用for循环获取单词表的单元格索引,而对于较大的表则要花费大量时间,没有for循环有没有办法做到这一点?

public static int[] GetColumnIndex(Xceed.Words.NET.Table table, string columnName, int endRow,int k)
        {
            int[] data = { -1, -1 };
            for (int j = k; j < endRow; j++)
            {
                for (int i = 0; i < table.Rows[j].Cells.Count; ++i)
                {
                    if (table.Rows[j].Cells[i].Paragraphs[0].Text.Equals("«" + columnName + "»"))
                    {
                        data[0] = j;
                        data[1] = i;
                        return data;
                    }
                }
            }

            return data;
        }

and I am calling this function form another function 我把这个功能称为另一个功能

int startRow = 0, endRow = 0;
int[] ind;
DocX doc;
doc = DocX.Load(fileName);
Xceed.Words.NET.Table t;
t = doc.Tables[0];
endRow = t.Rows.Count;
System.Data.DataTable dt = new DataTable();
dt = reader(report.Query);
foreach (DataColumn col in dt.Columns)
{
    ind = GetColumnIndex(t, col.ColumnName, endRow,2);

    //...more code here...
}

A few things you can do to optimise your algorithm (based on your access pattern) is that you search the same table number of times (in fact, since you are searching column names in the table, number of searches increases quickly as the table gets big). 您可以根据您的访问模式来优化算法的几件事是,您搜索相同的表次数(实际上,由于您正在搜索表中的列名,因此随着表的获取,搜索次数会迅速增加)大)。 Hence, it would be worth transforming the data in the table to a data structure indexed by the words (for eg a Sorted Dictionary ). 因此,将表中的数据转换为由单词索引的数据结构(例如, 排序字典 )是值得的。

Firstly, create a class that holds the content of the table. 首先,创建一个保存表内容的类。 This way when you want to search the same table, you can use the same instance of the class and avoid recreating the data structure based on the sorted dictionary: 这样,当您要搜索相同的表时,可以使用该类的相同实例,并避免基于已排序的字典重新创建数据结构:

public class XceedTableAdapter
{
   private readonly SortedDictionary<string, (int row, int column)> dict;

   public XceedTableAdapter(Xceed.Words.NET.Table table)
   {
      this.dict = new SortedDictionary<string, (int, int)>();
      // Copy the content of the table into the dict.
      // If you have duplicate words you need a SortedDictionary<string, List<(int, int)>> type. This is not clear in your question.
      for (var i = 0, i < rowCount; i++)
      {
          for (var j = 0; j < columnCount; j++)
          {
              // this will overwrite the index if the text was previously found: 
              this.dict[table.Rows[i].Cells[j].Paragraphs[0].Text] = (i, j);
          }
      }
   }

   public (int, int) GetColumnIndex(string searchText)
   {
      if(this.dict.TryGetValue(searchText, out var index))
      {
          return index;
      }

      return (-1, -1);
   }
}

Now you loop the entire table only once and the subsequent searches will happen in O(log n). 现在您只循环一次整个表,随后的搜索将在O(log n)中进行。 If Xceed has a function to transform data table to a dictionary, that would be quite handy. 如果Xceed具有将数据表转换为字典的功能,那将非常方便。 I'm not familiar with this library. 我对这个图书馆不熟悉。

Now you can search it like: 现在您可以像搜索它一样:

var searchableTable = new XceedTableAdapter(doc.Tables[0]);

foreach (var col in dt.Columns)
{
   ind = searchableTable.GetColumnIndex(col);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在不使用odbc的情况下将c#与mysql一起使用的最佳方法是什么 - what is the best way to use c# with mysql without using odbc 在C#中填充Word 2007模板的最佳方法是什么? - What is the best way to populate a Word 2007 template in C#? 最好的方法是在C#中的字符串中获取第一个单词和其余单词 - Best way get first word and rest of the words in a string in C# 如何使用C#在没有循环的情况下将矩阵复制到Word表中 - How to Copy a matrix into Ms Word Table without loop with C# 使用行索引和列索引C#获取单元格值 - Get cell value using row index and column index c# 使用 c# 以编程方式设置 Word 文档表格单元格边距 - Setting word document table cell margin programmatically using c# 使用Word或InDesign在c#中生成PDF的最佳方法? - Best way to generate PDF in c# using Word or InDesign? 在C#中循环字母表以输出到Excel的最佳方法是什么? - what is the best way to loop through alphabet in C# to output to Excel? 在C#中的循环内调用参数化方法的最佳方法是什么 - What is the best way to call a parameterized method inside a loop in C# 不使用ORM在C#中创建独立于供应商的dal的最佳方法是什么? - what is the best way to create an vendor independent dal in c# without using ORM?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM