Open excel file in a temporary file - c#

I have been using excel interop for a while now but decided to start using EPPLUS library instead for some reasons.
I like it but I would like to open the excel files the same way Excel Interop does: Open the file as a temporary file that does not really exist anywhere.
So far, EPPLUS must save the file somewhere so that I can open it using:
System.Diagnostics.Process.Start()
What I have tried so far is delete the file after I open it:
excelPack.SaveAs(new FileInfo(name));
File.SetAttributes(name, FileAttributes.ReadOnly); //Force the user to save file As
System.Diagnostics.Process.Start(name);
File.Delete(name); //Crash here. File is in use
But as you can see, it crashed at last line because the file is opened.

The Solution is quite simple. Simply set the file attributes to normal before deleting it. This will somehow tell windows that the file is no longer in use:
excelPack.SaveAs(new FileInfo(name));
File.SetAttributes(name, FileAttributes.ReadOnly); //Force the user to save file As
System.Diagnostics.Process.Start(name);
File.SetAttributes(name, FileAttributes.Normal);
File.Delete(name);
The file will be deleted but it will still be open in Excel in Read Only. The user will be able to save it.

Related

How to open created WorkBook with C# using memory, without saving it?

I'm creating an Excel file using ClosedXML and i would want to open the created workbook without saving it to the server. So is there a chance to open ClosedXML created WorkBook without saving it anywhere ? my guess would be using memory, but how do u do it ?
wb.SaveAs(filePath);
System.Diagnostics.Process.Start(filePath);
im now opening it while saving it, but since few people could be using the program and trying to get the excel file at the same time it would lead to error. So is there a way to open the WorkBook file created with ClosedXML without saving it ?
You can save the WorkBook to a MemoryStream.
wb.SaveAs(new MemoryStream());
But then you need some program to watch the file (bytes)
To avoid getting an exception when an other user tries to access the file you should write a method to wait for the file to get released.
Suggestion: https://www.codeproject.com/Tips/164428/C-FileStream-Lock-How-to-wait-for-a-file-to-get-re
If your primary problem is that "few people could be using the program and trying to get the excel file at the same time it would lead to error" then you can simply generate unique file name each time (for example using GUIDs).
ClosedXML does support saving file in a memory stream, but Excel will not accept this as a valid data source.

Accessing Excel File Located at Properties.Resources and keep here template

I have a big Excel file with multi sheets that I am using as a template to write data on and then save as a new file to disk. In debug mode, I read it from disk add data to it and save it in a different location without any problems. However, now I need to creat release to my client, but I want to prevent him from reaching the file Excel... so I tried to add my excel file to the Resources but I cann't reread it becaus when I try to read from resources i get it like list of string and i cann't read the forme of template.
I used
NPOI
for read/write the excel file.
So how can I open it from the Resources folder? Is there a better alternative for including such template files to a solution?
I find this solution and it work with me,
for use file Excel, it must already exist physically, so i saved excel file like temp file and i used this temp file for read my data,
string sPath = System.IO.Path.GetTempFileName();
System.IO.File.WriteAllBytes(sPath, Properties.Resources.data_base);
note: Properties.Resources.data_base this is my Excel file.
at the end I delete this temp file for more security
if (System.IO.File.Exists(sPath ))
{
System.IO.File.Delete(sPath );
}

Open File using Notepad and Copy data to Excel in c#

I have a csv file that is pipe delimited. I want to know if there is any way in c# so that it will open the file in notepad, then copy the data to excel. I want this specific step to be performed, not just a simple copy and paste.
The issue is that the file is getting corrupted if I open it directly through excel.
Hence, opening it first using notepad, copying it to a new excel file and then doing the remaining operations in excel. This gives correct output, unlike opening the file via Excel.
Can someone please let me know if this can be achieved in c#?
You can read a whole file as a System.Text.StringBuilder and replace | with ,. Now try to open the file with Excel, it will open without any issue.
Sample Code
System.Text.StringBuilder str= File.ReadAllText(#"C:\temp\test.csv");

Reading non-standard excel file with C#

What do i mean by 'non-standard'?
Take a look at these images: http://imgur.com/a/tFqHQ
The first one is the non-standard excel file. I'm pretty sure it's not an excel file, but the file's extension is .xls and for some reason Excel can open it, and understand it's structure.
The second image is the same file after it was opened in excel, and saved out to .xls (97-2003).
If excel can open it, and view it correctly, i should be able to do as well. Any tips how to approach this?
I have to mention that, my app have to use and read the non-standard excel files, because otherwise the user have to open the files one-by-one in (excel/libre office) and save it out in a correct format, which i would like to avoid for convenience.

How can I force the user to save an excel file in excel?

I am create an .xls file programatically and opening it in excel
for example:
Process.Start("c:/blabla.xls");
I am deleting the file when excel is closed, so I would like to prompt the user if he wants to save the file when excel before it is closed, and ideally make him save it to a new location.
I'm hoping there is an argument I can feed to excel during the Process.Start
Instead of opening Excel with an Excel file (.xls), you could open Excel with a Excel template (.xlt). This should open a new, unnamed file in Excel, using your xlt as the template. Since the file is unnamed, the user will be prompted to choose a location and file name if he made any changes.
(I'm not sure if renaming the file suffices; you might have to save the file as a template.)
EDIT: In fact, there is a command-line switch lets you do exactly that (open a normal Excel file as a template):
excel.exe /t C:\blabla.xls
Handle the BeforeClosed event.
This is assuming you are using Excel automation. Which, after reading your question again it appears you are not.
http://j-walk.com/ss/excel/tips/tip78.htm

Categories