我需要从系统读取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;
///如何将哈希集转换为流。
}
答案 0 :(得分: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;
}