4

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.

rgshenoy
  • 386
  • 1
  • 4
  • 18
  • Cant understand your question. How it is possible to open a file without save it first? Please, describe your scenarios step-by-step – Ewerton Oct 09 '12 at 12:55
  • 1
    @Ewerton I understand it sounds quite strange, but excel interop works that way. I was wondering if it was possible to do that with EPPlus. – rgshenoy Oct 09 '12 at 13:26

3 Answers3

4

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.

Tim Copenhaver
  • 3,282
  • 13
  • 18
  • Exactly! I want it to show up as Book1.xlsx and not as somefile.xlsx. Maybe this is not possible unless I save it :( I used to do this using PIAs, but since I moved to EPPlus because of speed issues, I cant do it anymore. – rgshenoy Oct 09 '12 at 13:23
  • 2
    The problem is that EPPlus (and most similar libraries) don't use Excel to generate the file. They take all the data you feed them, then save the file in the format Excel expects. How they store it in memory is completely up to them. Excel doesn't know anything about the data until it's written to the xlsx file. – Tim Copenhaver Oct 09 '12 at 13:31
  • 1
    I agree with Tim Copenhaver, System.Diagnostics.Process.Start, starts a new office process, like a user clicking on a .doc, .xls file. EPPlus, uses OpenXML format to create and manipulate files, in fact, he creates a específic XML thats is saved in disk with the xlsx extension, so, windows recognizes it as excell file. – Ewerton Oct 09 '12 at 17:15
0

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

Ahmed Ghazey
  • 479
  • 1
  • 9
  • 22
0

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
GoodJuJu
  • 1,296
  • 2
  • 16
  • 37