[英]Search string in excel file and return searched cell's rowindex and columnindex using OpenXML in C#
I want to search string in excel file, I used to use Microsoft.Office.Interop.Excel in my program and It works perfectly. 我想在excel文件中搜索字符串,我曾经在程序中使用Microsoft.Office.Interop.Excel,并且效果很好。 I use this code below: 我在下面使用此代码:
findRange1 = range1.Find("apple", LookAt: Excel.XlLookAt.xlWhole);
However, I faced a problem using interop excel in the server side. 但是,在服务器端使用interop excel时遇到了问题。 And I change my program using OpenXML. 然后我使用OpenXML更改程序。 I want to do the searching method as well using OpenXML. 我也想使用OpenXML进行搜索。 how to do it in OpenXML? 如何在OpenXML中做到这一点?
1) Import your excel document .xlsx
or xls
to SpreadsheetDocument
of OpenXMl
. 1)将您的excel文档.xlsx
或xls
导入到OpenXMl
SpreadsheetDocument
中。
public WorkbookPart ImportExcel()
{
try
{
string path = @"your path to excel document";
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
MemoryStream m_ms = new MemoryStream();
fs.CopyTo(m_ms);
SpreadsheetDocument m_Doc = SpreadsheetDocument.Open(m_ms, false);
return m_Doc.WorkbookPart;
}
}
catch (Exception ex)
{
System.Diagnostics.Trace.TraceError(ex.Message + ex.StackTrace);
}
return null;
}
2) Create method to get index in the form of [row, column]
by using specific search criteria. 2)创建使用特定搜索条件以[row, column]
形式获取索引的方法。
public string GetIndexBySearch(string search)
{
WorkbookPart workbookPart = ImportExcel();
var sheets = workbookPart.Workbook.Descendants<Sheet>();
Sheet sheet = sheets.Where(x => x.Name.Value == "you sheet name in excel document").FirstOrDefault();
string index = string.Empty;
if (sheet != null)
{
var worksheetPart = (WorksheetPart)workbookPart.GetPartById(sheet.Id);
var rows = worksheetPart.Worksheet.Descendants<Row>().ToList();
// Remove the header row
rows.RemoveAt(0);
foreach (var row in rows)
{
var cellss = row.Elements<Cell>().ToList();
foreach (var cell in cellss)
{
var value = cell.InnerText;
var stringTable = workbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
value = stringTable.SharedStringTable.ElementAt(int.Parse(value)).InnerText;
bool isFound = value.Trim().ToLower().Contains(search.Trim().ToLower());
if (isFound)
{
index = $"[{row.RowIndex}, {GetColumnIndex(cell.CellReference)}]";
return index;
}
}
}
}
return index;
}
3) Finally this method gives you column index by passing column name. 3)最后,此方法通过传递列名为您提供列索引。
private static int? GetColumnIndex(string cellReference)
{
if (string.IsNullOrEmpty(cellReference))
{
return null;
}
string columnReference = Regex.Replace(cellReference.ToUpper(), @"[\d]", string.Empty);
int columnNumber = -1;
int mulitplier = 1;
foreach (char c in columnReference.ToCharArray().Reverse())
{
columnNumber += mulitplier * ((int)c - 64);
mulitplier = mulitplier * 26;
}
return columnNumber + 1;
}
4) Considering all above 3 methods are in same class called MyClass
. 4)考虑到以上所有三种方法都在称为MyClass
同一类中。 Then you will use GetIndexBySearch
like 然后您将使用GetIndexBySearch
类的
MyClass c = new MyClass();
string index = c.GetIndexBySearch("AFL");
Output: 输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.