[英]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.