I am trying to save excel in c# as .csv file. When I open the csv file the "-" is converted to some strange character. I have tried both .xlcsvwindows and xlcsv formats.
e.g. XXXXX2018 - OPS&FE.34AC becomes XXXXX2018 – OPS&FE.34AC
xlWorkbook.SaveAs(outputFile, Excel.XlFileFormat.xlCSV);
Related
Im exporting some data to a .csv file.
.csv file:
Hotel Name;Street;Postal Code;City;Latitude;Longitude
Hotel X;Street 1;00000;City X;15.000000;15.000000
But if i open it in Excel, Excel will Format the Latitude and Longitude automaticly so it cannot be used for copy & paste.
Ignore the 0 at postal Code, i must get rid of the 1.000 at Latitude and Longitude
How can i prevent Excel from doing this?
It should be done in the .csv file and not in Excel.
The Export Code:
foreach(...)
{
StringBuilder_Export.Append(DataRow_Temp[i] + ";");
}
StreamWriter StreamWriter_Export = new StreamWriter(
SaveFileDialog_Geo_Export.FileName,
true,
Encoding.Default
);
StreamWriter_Export.WriteLine(StringBuilder_Export.ToString());
EDIT: Im searching primarly for a solution of my Latitude and Longitude Problem.
Possibly a duplicate of Stop Excel from automatically converting certain text values to dates but if you want to generate an Excel file that looks exactly as you want when opened, do not use csv. Excel will always attempt to guess datatypes of csv columns by looking at the first (15 i think?) rows..
I use EPPlus to generate xlsx files from my apps, but there are many libraries you could use
Change its extension to txt and use the text import wizard. Then use that to tell excel how the columns should be treated (text, currency, date, etc). The text import wizard will start automatically when you open a .txt file
ensure the file is in UTF-8 format when you save it as a .csv
Then open excel, browse to select the file you want and it will automatically run the prompter.
Any other format than utf-8 and excel will try and auto convert.
I have an application, on which i export data from a datagrid to a csv file. I do this with the following steps:
Create a file:
var myFile = File.Create("test.csv");
myfile.Close();
write the data to a string builder(data)
write the data to the created file.
File.WriteAllText(filepath, data);
This works fine. The resulting csv file is opened in excel. I have a column of numbers which may have preceeding zeros, when those data is exported to csv file the preceeding 0's are lost. Is it possible to format the column as text column so the zeros are not lost.
View your file in notepad. The leading zeros are there, intact :-)
You need to tell Excel which format to use when you open the file. Change the file to .txt and use File -> Open in Excel and you should be presented with an import wizard. There you can explicitly tell Excel to treat your column as "text" which will prevent it from stripping leading zeroes.
More info here: http://www.howtogeek.com/howto/microsoft-office/how-to-import-a-csv-file-containing-a-column-with-a-leading-0-into-excel/
I am trying to export data containing Unicode characters from our system using Spreadsheet Gear to csv format. (Fine for excel).
However because the CSV format is not UTF-8 encoded all the Unicode characters are exported as ???
I am aware that Spreadsheet Gear supports Unicode by having a tab-delimited UTF-8 text file, however we require the comma-delimited file.
This is what currently exists (including my check that the Unicode Text file format exports the characters correctly):
public static void ExportToStream(Stream stream, IDataSource data, IEnumerable<ColumnInfo> columns, ExportFormat format)
{
var exporter = new ExportCreator
{
Data = data,
Columns = columns.ToArray(),
EscapeFormulas = format == ExportFormat.Xlsx
};
var workbook = exporter.GetWorkBookForExport();
switch (format)
{
//need to put something in here to enable the csv to be encoded as utf-8 -
//currently spreadsheet gear only supports utf-8 encoded tab delimited text file
case ExportFormat.Csv:
workbook.SaveToStream(stream, FileFormat.UnicodeText);
break;
case ExportFormat.Xlsx:
workbook.SaveToStream(stream, FileFormat.OpenXMLWorkbook);
break;
}
}
As far as I can tell from trawling the Spreadsheet Gear documentation and SO, UnicodeText format would solve the problem but doesn't meet our requirements for the CSV file format.
Is there a way of specifying the encoding of the stream so the CSV File format is saved as UTF-8 encoded?
SpreadsheetGear provides no option to specify the encoding of a CSV file, or option to specify what delimiter is used for a given text-based data file. If you have a CSV encoded with UTF, you would need to build your own routine that reads and handles this file accordingly, then manually insert that data into your worksheet cell-by-cell.
I am wring a csv file with some program.
I am facing a issue when I open it in the excel file and for some column if it exceeds the value more than 256 it automatically truncate it.
I learnt that by default the excel has number format TO 'GENERAL' if I could set it to 'TEXT' then it wouldn't truncate any part.
So is there any programmatic way which can set the number format from GENERAL to TEXT.
csv is a plain text format. You can't embed meta-data such as column handling into that file format.
If you don't want excel to truncate the column, then you'll need to truncate it yourself when you write out the csv file:
var csvFileBuffer = new StringBuilder();
var columns = new List<string>();
csvFileBuffer.AppendLine(
string.Join(
",",
columns.Select(s =>
//truncate column header (change logic as appropriate)
s.Substring(0, 255))));
That said, there may be an option you can set in excel which will change the default column type for a csv file. However, that would be a question for SuperUser.com
The other option is to write out to a native excel file format, such as xlsx. There are a number of tools you can use to this, such as the Open Xml Sdk.
I would like to use EPPlus to create an Excel file but I have a problem: my input data is in a tab-delimited format:
Name Code Grade
------------------------
N1 C22 17.6
N2 C09 18.9
N3 C18 20
How can I add this type (tab format) of data using EPPlus package?
EPPlus is a library to read and write xlsx files only, so you cannot directly parse a file in tabular format.
You need to either write a reader for your format or, even easier, use a CSV reader that supports custom delimiters, and set the delimiter to \t. You can use this reader to read each cell of your data and feed it to EPPlus to re-create the datasheet.
I would simply read the text file in the standard old C# way, load each cell into a cell in EPPlus, and then save it. You'd just have to write a few loops and some formatting code.
Epplus supports importing tab delimited text. You can see it sample9.cs (https://github.com/JanKallman/EPPlus/blob/master/SampleApp/Sample9.cs)
If you are just looking for code,
//Create the format object to describe the text file
var format = new ExcelTextFormat();
format.Delimiter='\t'; //Tab
//Now read the file into the sheet.
Console.WriteLine("Load the text file...");
var csvDir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "csv");
var range = sheet.Cells["A1"].LoadFromText(Utils.GetFileInfo(csvDir, "Sample9-2.txt", false), format);
Direct link to the line # https://github.com/JanKallman/EPPlus/blob/master/SampleApp/Sample9.cs#L138