简体   繁体   English

C# 将物理文件转换为iformfile

[英]C# Convert physical file to iformfile

I am trying to read a physical excel file to a IFormFile but the content is always empty when I am reading the file it keep shows byte 0. I am trying to test a method that will read the content of the excel file.我正在尝试将物理 excel 文件读取到 IFormFile 但是当我读取它保留的文件时内容始终为空,显示字节 0。我正在尝试测试一种方法来读取 excel 文件的内容。 Is there anyone that did testing that manage to read an excel file to a iformfile or how do I write unit testing that is reading the file using epplus?有没有人做过测试,设法将 excel 文件读取到 iformfile,或者我如何编写使用 epplus 读取文件的单元测试?

ServiceTest.cs服务测试.cs

    [Fact]
    public async Task saveFile_test()
    {
        string rootPath = Directory.GetCurrentDirectory();
        string testAssetsFolder = @"Assets\";
        string testFile = "test.xlsx";
        string testFilePath = Path.Combine(rootPath, testAssetsFolder, testFile);

        // Arrange.
        var fileMock = new Mock<IFormFile>();
        var physicalFile = new FileInfo(testFilePath);
        var ms = new MemoryStream();
        var writer = new StreamWriter(ms);
        using (FileStream fs = physicalFile.OpenRead())
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);

            while (fs.Read(b, 0, b.Length) > 0)
            {
                writer.WriteLine(temp.GetString(b));
            }
        }
        writer.Flush();
        ms.Position = 0;
        var fileName = physicalFile.Name;
        //Setup mock file using info from physical file
        fileMock.Setup(_ => _.FileName).Returns(fileName);
        fileMock.Setup(_ => _.Length).Returns(ms.Length);
        fileMock.Setup(m => m.OpenReadStream()).Returns(ms);
        fileMock.Setup(m => m.ContentDisposition).Returns(string.Format("inline; filename={0}", fileName));
        //...setup other members as needed

        List<IFormFile> files = new List<IFormFile>();
        files.Add(fileMock.Object);

        await _service.saveFile(files, "directory", "subDirectory", default);
    }

Service.cs服务.cs

    public async Task saveFile(List<IFormFile> files, string directory, string subDirectory, CancellationToken cancellationToken)
    {
        subDirectory = subDirectory ?? string.Empty;
        var target = Path.Combine(directory, subDirectory);

        Directory.CreateDirectory(target);

        foreach (IFormFile file in files)
        {
            if (file.Length <= 0) return;
            var filePath = Path.Combine(target, file.FileName);
            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream, cancellationToken);
                try
                {
                    // add to read encoded 1252 values
                    using (var package = new ExcelPackage(stream))
                    {

                        // Express worksheet
                        ExcelWorksheet worksheet = package.Workbook.Worksheets["Express"];
                        int colCount = worksheet.Dimension.End.Column;  //get Column Count
                        int rowCount = worksheet.Dimension.End.Row;

                        ....

You should be able to achieve this using the ExcelDataReader.ExcelReaderFactory您应该能够使用ExcelDataReader.ExcelReaderFactory来实现这一点

 using (var stream = file.OpenReadStream())
            {
                using (var reader = ExcelReaderFactory.CreateReader(stream))
                {
                    
                        reader.Read(); /* Header row - we ignore this */
                        do
                        {
                            while (reader.Read())
                            {
                                {
                                    var username = reader.GetString(0);
                                    var firstName = reader.GetString(1);
....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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