【问题标题】:Create a temporary excel file with epplus?用epplus创建一个临时excel文件?
【发布时间】:2015-05-16 03:57:22
【问题描述】:

使用epplus将所有我想要的内容添加到新创建的excel文件后,我该如何打开它?我不想先保存文件然后打开它,这可能吗?我希望它只是打开并让用户决定是否要保存它。

到目前为止我发现并尝试过的唯一代码会生成一个文件名,保存 excel 文件,然后打开它。

Byte[] bin = p.GetAsByteArray();
string file = Guid.NewGuid().ToString() + ".xlsx";
File.WriteAllBytes(file, bin);
ProcessStartInfo pi = new ProcessStartInfo(file);
Process.Start(pi);

【问题讨论】:

    标签: c# excel epplus


    【解决方案1】:

    EPPlus 在重命名的 zip 文件中生成 xml,因此没有将其传输到 Excel 而不将其保存在某处的机制。但是您始终可以保存到用户临时文件夹 - 这是大多数程序在某些时候必须做的,以便在彼此之间传输文件。可以使用System.IO.Path.GetTempPath() 做这样的事情:

    [TestMethod]
    public void TempFolderTest()
    {
        var path = Path.Combine(Path.GetTempPath(), "temp.xlsx");
        var tempfile = new FileInfo(path);
        if (tempfile.Exists)
            tempfile.Delete();
    
        //Save the file
        using (var pck = new ExcelPackage(tempfile))
        {
            var ws = pck.Workbook.Worksheets.Add("Demo");
            ws.Cells[1, 2].Value = "Excel Test";
            pck.Save();
        }
    
        //open the file
        Process.Start(tempfile.FullName);
    }
    

    (取自:Open ExcelPackage Object with Excel application without saving it on local file path

    【讨论】:

      【解决方案2】:

      你不可能用 epplus 创建一个临时的 excel 文件

      【讨论】:

        猜你喜欢
        • 2022-11-14
        • 1970-01-01
        • 1970-01-01
        • 2017-06-24
        • 1970-01-01
        • 2013-01-25
        • 1970-01-01
        • 2013-01-16
        • 2016-03-08
        相关资源
        最近更新 更多