方法说明:OUTPath寻找路径,newName需要寻找到的sheet名。
具体思路:1、找文件是否存在。2、找指定的sheet名是否存在
public int findReport(string OUTPath, string newName)
{
//type:1-find sheet 2-find file 0-not find
int type = 0;
string searchPattern = $"123.xlsx";
DirectoryInfo directoryInfo = new DirectoryInfo(OUTPath);
FileInfo[] fileSs = directoryInfo.GetFiles(searchPattern);
if (fileSs.Any())
{
type = 2;
string fullPath = Path.Combine(OUTPath, searchPattern);
IWorkbook workbook;
using (FileStream file = new FileStream(fullPath, FileMode.Open, FileAccess.Read))
{
if (searchPattern.EndsWith(".xlsx"))
{
workbook = new XSSFWorkbook(file);
}
else
{
throw new Exception("Unsupported file types");
}
bool sheetExists = workbook.GetSheet(newName) != null;
if (sheetExists)
{
type = 1;
Console.WriteLine("找到sheet");
}
else
{
Console.WriteLine("未找到sheet");
}
workbook.Close();
}
}
else
{
Console.WriteLine("未找到文件。");
}
return type;
}