I have an application that creates a csv file and I want it to display it in excel after creation.
I've used to following code - which works well :
Expand|Select|Wrap|Line Numbers
- using ExcelInterop = Microsoft.Office.Interop.Excel;
- ExcelInterop._Worksheet ExcelWorkSheet; //represents the resulting worksheet.
- Microsoft.Office.Interop.Excel.Application excelApp = null;
- try
- {
- excelApp = new ExcelInterop.ApplicationClass();
- excelApp.Visible = true;
- string path = txtOutputPath.Text; //full file path is given.
- excelApp.Workbooks.Open(path, 0, false, 2, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
- ExcelWorkSheet = (ExcelInterop._Worksheet)(excelApp.ActiveWorkbook.ActiveSheet);
- ExcelWorkSheet.Columns.AutoFit();
- ExcelWorkSheet.Columns.HorizontalAlignment = ExcelInterop.XlHAlign.xlHAlignLeft; //aligns values to the left.
- ExcelWorkSheet._DisplayRightToLeft = 0; //makes the excel to display as left-to-right.
- }
- catch (Exception ex) //Used to catch exception of type "System.Runtime.InteropServices.COMException"
- {
- if (excelApp != null)
- excelApp.Quit();
- MessageBox.Show(ex.Message);
- }
The thing is - my CSV file contains some fields with long numbers. I want this numbers to be displayed as text (WYSIWYG) - but Excel chagnes them - for eg. : 909490266168 ----> 9.0949E+11
I want to add something to my code that'll set the format of all cells to be "Text" (So everything will be displayed just as it is in the CSV file - not altering anything).
I tried : ExcelWorkSheet.Cells.NumberFormat = "Text" but this did not work properly and messed up some Date cells.
I'm not so familier with Excel Programming , So help will be appreciated. Thanks in advance.