c#可以使用NPOI来查询“你好”这个中文出现在哪个单元格上吗
时间: 2024-03-21 09:39:59 浏览: 35
可以的,你可以使用 NPOI 操作 Excel 文件,并使用其中的方法来查询单元格中是否包含指定的文本内容。
以下是一个示例代码,可以查询一个 Excel 文件中是否包含指定的文本内容:
```csharp
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;
class Program
{
static void Main(string[] args)
{
// 打开 Excel 文件
FileStream fs = new FileStream("test.xlsx", FileMode.Open, FileAccess.Read);
XSSFWorkbook wb = new XSSFWorkbook(fs);
ISheet sheet = wb.GetSheetAt(0);
// 查询指定的文本内容
string searchText = "你好";
for (int row = 0; row <= sheet.LastRowNum; row++)
{
IRow currentRow = sheet.GetRow(row);
if (currentRow == null)
{
continue;
}
for (int col = 0; col < currentRow.LastCellNum; col++)
{
ICell currentCell = currentRow.GetCell(col);
if (currentCell == null)
{
continue;
}
if (currentCell.CellType == CellType.String && currentCell.StringCellValue.Contains(searchText))
{
Console.WriteLine($"找到了文本“{searchText}”在单元格 {currentCell.Address} 中");
}
}
}
// 关闭 Excel 文件
wb.Close();
fs.Close();
}
}
```
在这个示例代码中,我们打开了一个名为 "test.xlsx" 的 Excel 文件,并查询其中是否包含指定的文本内容 "你好"。如果找到了指定的文本内容,则输出包含该文本的单元格地址。