Home » excel » c# – Open excel with EPPlus without saving to file

c# – Open excel with EPPlus without saving to file

Posted by: admin April 23, 2020 Leave a comment

Questions:

Rright now I’m opening excel using
System.Diagnostics.Process.Start(fileInfo);

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? I would like the user to decide whether or not to save the file.

How to&Answers:

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. 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.

Answer:

I think this answer will help new developer.
I think best option for viewing Excel without saving is Microsoft.Office.Interop.Excel

Open Nuget Package Console Install-Package Microsoft.Office.Interop.Excel

create Excel file here is the official documentation Excel

at the end of filling Excel file just type app.Visible = true; app is the object name

Answer:

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. It would present itself nicely as Book1.xlsx to the end-user who could them save it or close it without saving as required.

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.

The approach I have taken, using the SaveFileDialog component is to prompt the user for a name before the EPPlus file is created. 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