-1

我需要从系统中读取一个 excel 文件并修改该文件(例如删除重复项)并将其放到另一个位置。我正在使用IFormFile.

谁能帮我从文件中删除重复项并更新同一文件并使用重复值创建一个新文件。

这是我必须阅读的文件

var result = new StringBuilder();
using (var reader = new StreamReader(iFormFile.OpenReadStream()))
{
  //read till EOF
  while (reader.Peek() >= 0) 
  {
      result.AppendLine(await reader.ReadLineAsync());
  }
}


 public async Task<Stream> ValidateDataFileAsync(Stream stream, CancellationToken token)
    {
        List<string> result = new List<string>();
        using (var reader = new StreamReader(stream))
        {
            while (reader.Peek() >= 0)
           result.Add(await reader.ReadLineAsync());
        }

        HashSet<string> hSet = new HashSet<string>(result);
       return hSet;

//我如何将哈希集转换为流。

    }
4

1 回答 1

0

首先安装Nuget:

Install-Package WindowsAzure.Storage -Version 9.3.3

Install-Package EPPlus -Version 4.5.2.1

IBlobManager.cs创建一个包含所有 Blob 操作的界面:

using System;
using System.IO;
using System.Threading.Tasks;

namespace UploadAzureBlob.Services
{
    public interface IBlobManager
    {
        Task<string> UploadFileToBlobAsync(string fileName, Stream stream);
    }
}

在一个类中实现上述接口BlobManager.cs

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace UploadAzureBlob.Services
{
    public class BlobManager : IBlobManager
    {
        private CloudBlobClient cloudBlobClient;
        public BlobManager()
        {
            // Retrieve the connection string for blob storage
            string storageConnectionString = "";

            CloudStorageAccount.TryParse(storageConnectionString, out CloudStorageAccount storageAccount);

            // Create the CloudBlobClient that represents the Blob storage endpoint for the storage account.
            cloudBlobClient = storageAccount.CreateCloudBlobClient();
        }

        /// <summary>
        /// Uploads a file to blob storage.
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        public async Task<string> UploadFileToBlobAsync(string fileName, Stream stream)
        {
            try
            {
                // Create a container.
                CloudBlobContainer blobContainer = cloudBlobClient.GetContainerReference("test");
                await blobContainer.CreateAsync();

                BlobContainerPermissions perm = await blobContainer.GetPermissionsAsync();
                perm.PublicAccess = BlobContainerPublicAccessType.Container;
                await blobContainer.SetPermissionsAsync(perm);

                // Get a reference to the blob address, then upload the file to the blob.
                var cloudBlockBlob = blobContainer.GetBlockBlobReference(fileName);
                await cloudBlockBlob.UploadFromStreamAsync(stream);

                // Returning the URI of the freshly created resource
                return cloudBlockBlob.Uri.ToString();
            }
            catch (StorageException ex)
            {
                throw;
            }
        }
    }
}

现在终于在你的控制器中:

public async Task<string> ValidateDataFileAsync(IFormFile formFile)
{
        List<string> result = new List<string>();

        using (var reader = new StreamReader(formFile.OpenReadStream()))
        {
            //read till EOF
            while (reader.Peek() >= 0)
                result.Add(reader.ReadLineAsync().Result);
        }

        // Filter by repeated items
        result = result.GroupBy(x => x).Where(x => x.Count() > 1).Select(x => x.Key).ToList();

        // Write the List<string> into the MemoryStream using the EPPlus package

        MemoryStream memoryStream = new MemoryStream();
        using (var package = new ExcelPackage())
        {
            var worksheet = package.Workbook.Worksheets.Add("Worksheet 1");
            worksheet.Cells["A1"].LoadFromCollection(result);
            memoryStream = new MemoryStream(package.GetAsByteArray());
        }

        IBlobManager blobManager = new BlobManager();
        string newResourceUri = await blobManager.UploadFileToBlobAsync(formFile.FileName, memoryStream);
        return newResourceUri;
}
于 2019-04-25T12:05:31.093 回答