I was trying to import data set to excel via creating a temporary .csv file in between to speed up the process.
Approach used:
Create a .csv file with the required data.
Open this .csv file using OpenText() function and save it as exel file.
Delete the temp. created .csv file.
Now the problem is when user opts for export to workbook option in that case i had to keep the newly created workbook open to the user as per my application requirement. But since as per my approach file is still opened in the excel workbook. So it's not allowing me the delete the temporary .csv file for obvious reason.
Can anyone suggest me another approach to do this. For exporting i had to use .csv option only, using library closedXML is not in the picture here.
Thanks in advance
Related
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.
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 );
}
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.
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
I have an automated test which produces a csv with two columns of data.
I have an excel file which I use to gather the results of all the runs in a worksheet.
I want to fully automate the process of updating the Excel file after each test run.
This is probably what I want to do:
1. Read the two columns from the CSV file
2. Paste the two columns in a worksheet in the excel file, in the first
empty column to the right of the existing block of columns.
3. Save the Excel file
EDIT:
Now i understand that i can do steps 1-3 using a macro.
All that is left for me to figure out is how to launch the macro.
You can read the csv and write to the Excel sheet with a single data provider, the OleDb provider. Here is an article on how to write to Excel, and here is one on how to read csv.
Once you write the macro, it should be in the macro list in Excel for that Excel document. You can bind the macro to a keystroke (like Alt-i, Alt-whatever) so that it runs when your press that key combination.
I've done this on a couple of similar projects where I need to import CSV files from other sources and put the data into an Excel sheet (or sheets)