azure 使用C#和Selenium查看blob文件

5hcedyr0  于 2023-06-24  发布在  C#
关注(0)|答案(1)|浏览(114)

我们有一个创建报告文件的过程,当用户单击“保存”按钮时,该报告文件准备作为xlsx文件下载,我想查看并验证该文件的内容,作为 selenium 回归测试的一部分。按钮的html为

<a class="n-pad btn" download="test-report" href="blob:https://www.testurl.co.uk/9fe176ea-d339-46eb-ac46-95bf300520e5">
<span class="icon-cp-download"></span>
<span>Save</span>
</a>

我想在这一点上阅读文件,任何人都可以建议的最佳方法来实现这一点。
谢谢

vuktfyat

vuktfyat1#

根据您的问题,您希望读取Excel文件(xlsx)。读取它的内容的最好方法是使用名为ExcelDataReader的包。安装到你的nuget包中。它是免费和开源的。
get excel sheet row id using exceldatareader into dataset
或者使用此代码作为示例。

public static DataTable ReadExcelFile(string filePath)
  {
     using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
      {
         using (var reader = ExcelReaderFactory.CreateReader(stream))
        {
        // Choose the specific sheet in the Excel file (e.g., 
         reader.AsDataSet().Tables["Sheet1"])
        var result = reader.AsDataSet().Tables[0];

        return result;
    }
    }
   }

如果不起作用,

string url = "blob:testPie.co.uk/c7a70bf0-7495-48f8-b989-be0035eb79fd";
 string fileName = Path.GetFileName(url);

 using (WebClient client = new WebClient())
{
  client.DownloadFile(url, fileName);
  using (FileStream stream = File.OpenRead(fileName))
  {
    // Use the file stream here as needed
    // ...
  }
   // Once you're done with the file, you can delete it
   File.Delete(fileName);
  }

相关问题