How to Create CSV Excel File C#

Writing data into CSV file in C#

UPDATE

Back in my naïve days, I suggested doing this manually (it was a simple solution to a simple question), however due to this becoming more and more popular, I'd recommend using the library CsvHelper that does all the safety checks, etc.

CSV is way more complicated than what the question/answer suggests.

Original Answer

As you already have a loop, consider doing it like this:

//before your loop
var csv = new StringBuilder();

//in your loop
var first = reader[0].ToString();
var second = image.ToString();
//Suggestion made by KyleMit
var newLine = string.Format("{0},{1}", first, second);
csv.AppendLine(newLine);

//after your loop
File.WriteAllText(filePath, csv.ToString());

Or something to this effect.
My reasoning is: you won't be need to write to the file for every item, you will only be opening the stream once and then writing to it.

You can replace

File.WriteAllText(filePath, csv.ToString());

with

File.AppendAllText(filePath, csv.ToString());

if you want to keep previous versions of csv in the same file

C# 6

If you are using c# 6.0 then you can do the following

var newLine = $"{first},{second}"

EDIT

Here is a link to a question that explains what Environment.NewLine does.

Export Excel sheet data to csv format

your error may be arising because of excel driver issues im not 100% sure about that however this error arising because of some required dll's are missing form you computer.

you might be able to solve this issue by installing MDAC to your computer and try to execute this.

if it's not solved you can use the below pasted code which is written by me just as a answer to your question but first of all i should tell you this below code part is not efficient if the conversion file has quite considerable number of record's ex 65000

To use the below code part you need to add below reference

using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;

Function

 private void EXCELTOCSV()
{
OpenFileDialog excelSheetToOpen = new OpenFileDialog();
excelSheetToOpen.Filter = "Excel 97- 2003 WorkBook (*.xls)| *.xls | Excel 2007 WorkBook (*.xlsx) | *.xlsx | All files (*.*)|*.*";
excelSheetToOpen.FilterIndex = 3;
excelSheetToOpen.Multiselect = false;

if (excelSheetToOpen.ShowDialog() == DialogResult.OK)
{

Excel.Application excelApp = new Excel.Application();
String workbookPath = excelSheetToOpen.FileName;
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath);
Excel.Sheets excelWorkBookSheets = excelWorkbook.Sheets;

Excel.Range _UsedRangeOftheWorkSheet;

foreach (Excel.Worksheet _Sheet in excelWorkBookSheets)
{
if (_Sheet.Name =="ExcelSheetName")
{

_UsedRangeOftheWorkSheet = _Sheet.UsedRange;

Object[,] s = _UsedRangeOftheWorkSheet.Value;

System.IO.StreamWriter sw = new System.IO.StreamWriter("FileName.csv", true);

for (int b = 0; b < s.Length; b++)
{
StringBuilder sb = new StringBuilder();
for (int c = 0; c < s.Rank; c++)
{
if (sb == null)
{
sb.Append((String)s[b, c]);
}
else
{
sb.Append("," + (String)s[b, c]);
}
}
sw.WriteLine(sb.ToString());
}
sw.Close();

}
}

}
}

Hope this will work for you

Thanks.

I Want to convert a .xls or.xlsx file to .csv format using C#

You have to specify the FileFormat explicitely, see here:

xlsheet.SaveAs(@"C:\Users\AbrahamSamuel\Desktop\sample\new.csv", XlFileFormat.xlCSV);

Depending on your needs, instead of xlCSV, xlCSVUTF8 or xlCSVWindows might be the right choice.



Related Topics



Leave a reply



Submit