Open File using Notepad and Copy data to Excel in c# - 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");

Related

Programmatically C# remove special characters from HTML source

Issue - I am downloading an excel file from an online service. The problem is that it downloads as an XLS extention, but when you do a Save As its actually a WebPage type HTML. What I have to do is open excel and save as Excel file. When I import the file programmatically to Postgres, the data imports fine no problem. The problem is when clicking the save from PgAdmin and the CSV shows those special characters. They look like foreign characters.
However, lets go back to the online service. If I manually copy the list and paste it into Notepad (which strips those characters)...then copy the list back into an excel file. Then there is no issue.
Question is: How to strip these characters programmatically instead of doing it manually with Notepad?
Example of the issue: Look at the Image. The date time has the special character and also the other cell should say "FILLER 5"

How to identify an excel file(xls, xlsx, csv) which has been converted from .DLL file in c# dot net core

Basically need to identify the excel is originally created from a valid excel. I have tried using Microsoft.Interop.Excel File format.
Seems like I cannot use this without Microsoft office being installed. So need another approach for this problem.
EDIT :
Basically I want to be able to distinguish a valid excel from invalid one like an excel file which is converted from DLL. Because the file extension will say it is xls. If you try to open that file, it will open an empty workbook without any sheets. But I cannot decide an excel with no sheets is an invalid one.
If you want to idenify wheter file is a valid excel file or not, you can use 'trid file identifier'. TrID - File Identifier.
You can do it own your own also by reading magic number or file signaure bytes. Please go through this question Original file bytes from StreamReader, magic number detection.

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 );
}

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