c# - 读取内存中的 Excel 电子表格

标签 c# linq-to-excel

如何读取刚刚发布到我的服务器的 Excel 电子表格? 我搜索了一些东西,但我只找到了如何读取带有文件名路径的 Excel 电子表格,这不是我的情况。

我需要这样的东西:

public ActionResult Import(HttpPostedFileBase file)
{
     var excel = new ExcelQueryFactory(file); //using linq to excel
}

最佳答案

我遇到了同样的问题,但我不想切换到付费服务,所以这就是我所做的。

public class DataImportHelper : IDisposable
{
    private readonly string _fileName;
    private readonly string _tempFilePath;

    public DataImportHelper(HttpPostedFileBase file, string tempFilePath)
    {
        _fileName = file.FileName;
        _tempFilePath = Path.Combine(tempFilePath, _fileName);
        (new FileInfo(_tempFilePath)).Directory.Create();
        file.SaveAs(_tempFilePath);
    }

    public IQueryable<T> All<T>(string sheetName = "")
    {
        if (string.IsNullOrEmpty(sheetName))
        {
            sheetName = (typeof (T)).Name;
        }

        var excelSheet = new ExcelQueryFactory(_tempFilePath);

        return from t in excelSheet.Worksheet<T>(sheetName)
               select t;
    }

    public void Dispose()
    {
        File.Delete(_tempFilePath);
    }

}

这是一个测试

[Fact]
    public void AcceptsAMemoryStream()
    {
        MemoryFile file;

        using (var f = File.OpenRead("SampleData.xlsx"))
        {
            file = new MemoryFile(f, "multipart/form-data", "SampleData.xlsx");

            using (var importer = new DataImportHelper(file, "Temp/"))
            {
                var products = importer.All<Product>();

                Assert.NotEmpty(products);

            }
        }
    }

这是 MemoryFile.cs。该文件仅用于测试。它只是 HttpPostedFileBase 的一个实现,因此您可以测试您的 Controller 和我的小 helper 。这是从另一个帖子借来的。

 public class MemoryFile : HttpPostedFileBase
    {
        Stream stream;
        string contentType;
        string fileName;

        public MemoryFile(Stream stream, string contentType, string fileName)
        {
            this.stream = stream;
            this.contentType = contentType;
            this.fileName = fileName;
        }

        public override int ContentLength
        {
            get { return (int)stream.Length; }
        }

        public override string ContentType
        {
            get { return contentType; }
        }

        public override string FileName
        {
            get { return fileName; }
        }

        public override Stream InputStream
        {
            get { return stream; }
        }

        public override void SaveAs(string filename)
        {
            using (var file = File.Open(filename, FileMode.Create))
                stream.CopyTo(file);
        }
    }

关于c# - 读取内存中的 Excel 电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13931808/

相关文章:

c# - 来自 CSV 文件的 Linq-To-Excel,缺少小数点分隔符

c# - Linq to Excel 无法解析混合字符串和数字内容

linq-to-excel - 如何获取 LinqToExcel 64 位版本

c# - 在 mvc3 服务器端代码上获取显示注释值

c# - 如何使用ZedGraph绘制方波?

c# - 在 web api Controller 中进行通用查询/getby 规范?

c# - 无法将源接口(interface)类型转换为目标接口(interface)类型

LinqToExcel : Distinct values in excel column

c# - 在 asp.net mvc 中检查上传的 Excel 文件中的非法字符

c# - Azure 函数不会将自定义事件记录到应用程序洞察中