简体   繁体   中英

C# - how to search for a string in Excel with NPOI

So I've just been introduced to NPOI, with the aim of eliminating the usage of interop - particularly for Excel files. I'm a fairly novice programmer, but I've put in a good shift trying to find some examples or documentation for how to use NPOI. I am unable to find an example of how to search for specific text in an Excel file, and return its cell address. I would achieve this through strings, and not regular expressions.

The goal of this, is to locate a cell from a query, and then extract the entire column of data, under the row in which the column header (from the query criteria) is located.

I can't offer guidance on how to use NPOI, because I'm not familiar with it. However, from your description is sounds like you are trying to select a single data column out of a spreadsheet. I maintain a couple libraries that can do this pretty easily: Sylvan.Data , and Sylvan.Data.Excel .

using Sylvan.Data;
using Sylvan.Data.Excel;
using System.Data.Common;

// Create a DbDataReader over all the columns in the Excel file.
DbDataReader r = ExcelDataReader.Create("data.xlsx");
// Creates a new, wrapper DbDataReader that exposes just the column with the header "myCol"
r = r.Select("myCol");

// loop over all rows in the file
while (r.Read())
{
    // output the string in the first (only) column.
    Console.WriteLine(r.GetString(0));
}



The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM