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 );
}
Related
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.
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");
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 have a C# program which generates a CSV file, then opens an XLSM file in Excel (using ProcessStartInfo and Process.Start). The macro in the XLSM loads the CSV file and distributes data from it to specific cells. Eventually this will be C# generating a spreadsheet without macros that can be loaded in any office suite, and the CSV will disappear.
My problem is that this system now needs to support running the XLSM from anywhere on disk. It finds the CSV via a hardcoded relative path, which doesn't work from a user-specified location. I'd like to do something similar to passing a compiler parameter to define a constant.
EDIT 1:
Alternative approaches I have considered and rejected:
Copy the XLSM to a standard location near the CSV and run it from there.
I would need to copy the file back to its original location, but I don't know when Excel is closed to do that... Except maybe in Excel, but it doesn't know the original location, hence this question.
Create the CSV file next to the user-specified XLSM location.
The CSV file's location is specified in an NLOG configuration file, and changing the surrounding implementation to use a hardcoded path would be expensive. This is the only idea I've had that could work, but it would hurt.
Create a Windows-based hard link to the custom location from the standard location.
I can't assume I'm running on an NTFS filesystem.
Porting your CSV generating program to a user machine you could:
set an environment variable on the user machine to the target directory; e.g. (cmd line) SET CSVPATH=C:\My\CSV
let the macro from your CSV processing XLSM read this environment variable using the Environ() function; e.g. (VBA) MyPath = Environ("CSVPATH") & "\"
alternatively, you could
use a directory selection dialog in an "installation XLSM" to write the correct file path(s) to the target machine's registry using the SaveSetting statement
let the macro from your CSV processing XLSM read the registry using the GetSetting() function to get the correct path
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