简体   繁体   English

使用 EPPlus 打开 excel 而不保存到文件

[英]Open excel with EPPlus without saving to file

Rright now I'm opening excel using System.Diagnostics.Process.Start(fileInfo);现在我正在使用 System.Diagnostics.Process.Start(fileInfo); 打开 excel;

to open my package after saving it to a file.保存到文件后打开我的包。

Is it possible to open excel without having to save the file to a location?是否可以打开excel而无需将文件保存到某个位置? I would like the user to decide whether or not to save the file.我希望用户决定是否保存文件。

If you're generating the file yourself using EPPlus (or any other libraries that generate the file directly), you'll need to save it before you can open it in Excel.如果您使用 EPPlus(或任何其他直接生成文件的库)自己生成文件,则需要先保存它,然后才能在 Excel 中打开它。 I'd recommend just saving it in the temp directory, then showing it to the user and letting them choose what to do with it.我建议将它保存在临时目录中,然后将其显示给用户并让他们选择如何处理它。

If you generate the file using Office Automation, you can display it to the user before saving it.如果使用 Office Automation 生成文件,则可以在保存之前将其显示给用户。

I think this answer will help new developer.我认为这个答案将帮助新的开发人员。 I think best option for viewing Excel without saving is Microsoft.Office.Interop.Excel我认为无需保存即可查看 Excel 的最佳选择是 Microsoft.Office.Interop.Excel

Open Nuget Package Console Install-Package Microsoft.Office.Interop.Excel打开 Nuget 包控制台Install-Package Microsoft.Office.Interop.Excel

create Excel file here is the official documentation Excel在这里创建Excel文件是Excel的官方文档

at the end of filling Excel file just type app.Visible = true;在填充 Excel 文件的最后输入app.Visible = true; app is the object name app 是对象名称

I know this is late, but I had the same issue.我知道这已经晚了,但我遇到了同样的问题。

I used to use Microsoft.Office.Interop.Excel to export data to an xlsx file without saving it.我曾经使用Microsoft.Office.Interop.Excel将数据导出到xlsx文件而不保存它。 It would present itself nicely as Book1.xlsx to the end-user who could them save it or close it without saving as required.它将自己作为Book1.xlsx很好地呈现给最终用户,最终用户可以保存或关闭它,而无需根据需要进行保存。

I have also moved to EPPlus to improve the speed of my exports and therefore, as you have experienced, cannot present the open file to the end-user without it first being saved.我还转移到EPPlus以提高我的导出速度,因此,正如您所经历的那样,如果没有先保存打开的文件,就无法将打开的文件呈现给最终用户。

The approach I have taken, using the SaveFileDialog component is to prompt the user for a name before the EPPlus file is created.我采用的方法是使用SaveFileDialog组件创建EPPlus文件之前提示用户输入名称。 This at least allows the end-user to identify where the file should be saved, rather than using a 'hard-coded' directory.这至少允许最终用户确定应该保存文件的位置,而不是使用“硬编码”目录。

Private Sub btnExportXlsxEPPlus_Click(sender As Object, e As EventArgs) Handles btnExportXlsxEPPlus.Click

    Try
        Dim filInf As FileInfo = New FileInfo(GetFileToSave())

        Using excelPackage As ExcelPackage = New ExcelPackage
            excelPackage.Workbook.Properties.Author = "enLIGHTen"
            excelPackage.Workbook.Properties.Title = "enLIGHTen Report"
            excelPackage.Workbook.Properties.Subject = "enLIGHTen export data"
            excelPackage.Workbook.Properties.Created = Date.Now

            Dim worksheet As ExcelWorksheet = excelPackage.Workbook.Worksheets.Add("Sheet 1")
            worksheet.Cells("A1").Value = "My EPPlus spreadsheet!"
            worksheet.Cells(1, 2).Value = "This is cell B1!"
            excelPackage.SaveAs(filInf)
        End Using

        Using excelPackage As ExcelPackage = New ExcelPackage(filInf)
            Dim firstWorksheet As ExcelWorksheet = excelPackage.Workbook.Worksheets(1)
            Dim namedWorksheet As ExcelWorksheet = excelPackage.Workbook.Worksheets("SomeWorksheet")
            Dim anotherWorksheet As ExcelWorksheet = excelPackage.Workbook.Worksheets.FirstOrDefault(Function(x) x.Name Is "SomeWorksheet")
            Dim valA1 As String = firstWorksheet.Cells("A1").Value.ToString
            Dim valB1 As String = firstWorksheet.Cells(1, 2).Value.ToString
            excelPackage.Save()
        End Using

        Dim proc As Process

        Try
            proc = New Process()
            Process.Start(filInf.FullName)
        Catch ex As Exception
            MsgBox("File cannot be opened", MsgBoxStyle.Information, "Cannot open file")
        End Try
    Catch

    End Try
End Sub

Public Function GetFileToSave()
    Dim strFilename As String = ""
    SaveFileDialog1.Filter = "Excel Workbook (*.xlsx)|*.xlsx"
    If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        strFilename = SaveFileDialog1.FileName
        Return strFilename
    End If
End Function

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

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