473,387 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Displaying CSV File in excel from within a .net application

Hi There ,

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
  1. using ExcelInterop = Microsoft.Office.Interop.Excel;
  2.  
  3.   ExcelInterop._Worksheet ExcelWorkSheet; //represents the resulting worksheet.
  4.  
  5.             Microsoft.Office.Interop.Excel.Application excelApp = null;
  6.             try
  7.             {
  8.                 excelApp = new ExcelInterop.ApplicationClass();
  9.  
  10.  
  11.                 excelApp.Visible = true;
  12.  
  13.                 string path = txtOutputPath.Text; //full file path is given.
  14.                 excelApp.Workbooks.Open(path, 0, false, 2, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
  15.                 ExcelWorkSheet = (ExcelInterop._Worksheet)(excelApp.ActiveWorkbook.ActiveSheet);
  16.                 ExcelWorkSheet.Columns.AutoFit();
  17.                 ExcelWorkSheet.Columns.HorizontalAlignment = ExcelInterop.XlHAlign.xlHAlignLeft; //aligns values to the left.
  18.                 ExcelWorkSheet._DisplayRightToLeft = 0; //makes the excel to display as left-to-right.
  19.             }
  20.             catch (Exception ex) //Used to catch exception of type "System.Runtime.InteropServices.COMException"
  21.             {
  22.                 if (excelApp != null)
  23.                     excelApp.Quit();
  24.                 MessageBox.Show(ex.Message);
  25.             }
(The code above is just a segment)

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.
Jan 4 '08 #1
6 1925
camel
55
I believe you need to go after Range.NumberFormat, i.e., for a particular column set the Range("A1").NumberFormat = "0.0". You can set this after loading the data with your existing code. Alternatively you could preformat a template if it will always be the same column that needs the special formatting.
Jan 5 '08 #2
I believe you need to go after Range.NumberFormat, i.e., for a particular column set the Range("A1").NumberFormat = "0.0". You can set this after loading the data with your existing code. Alternatively you could preformat a template if it will always be the same column that needs the special formatting.

Thanks for your reply.
Unfortunately, my CSV files are not constant , So I basiclly want Excel to just display what it sees in the CSV (not doing any formatting of its own). I can't tell which column resides in each range , since as I just said , the application generates many different CSV files.
Jan 5 '08 #3
camel
55
Any dynamic approach in terms of knowing nothing of the structure of the input data columns presents a challenge, which is why Excel's own algorithms of inspecting a few rows and making a guess on structure being similar for folowing records is not always perfect (though usually displays something!)

If you can't go for a specific range at the outset I would look at applying a filter expression to find the cells of interest and then apply the NumberFormat to the resulting cells (which can be handled via a Range object).

You could in principle iterate every populated cell in the active workbook with a switch statement checking its likely datatype and length, i.e., to avoid date patterns, though this would be fairly expensive compared to a filter approach.

Another option is to load the populated data into an ADO data structure where you would have opportunity to apply an appropriate datatype to columns containing the values, you could then populate Excel direct from the recordset (Excel.CopyRecordset). For this to work you still need to come up a with rules to work out best datatypes based on data coming in, just using different tools
Jan 5 '08 #4
poolboi
170 100+
hi,
may i know what code you are using?
'cos i also have an application that display my data as a csv file and i want to display it in excel without the commas?
any idea how i can achieve this?
Feb 15 '08 #5
romcab
108 100+
Hi,

I think I have encountered that problem before. What i did was to change data I read from strings -> double or integer then set the cell numberformat. This works for me.
Feb 15 '08 #6
poolboi
170 100+
ok so basically the commas u outsput as intergers instead of strings so that all the commas will be gone alright..
but u got any knowledge of how i might be able to get all the datas from csv file to an excel?
cos i got this csv file generated from an applications with columns of information and i need to put it in excel for easy referencing
Feb 15 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Rich Wallace | last post by:
Hello all, Looking for suggestions and tips if possible. I have an application running on a file server that utilizes the FileSystemWatcher to trap when any Excel files are saved by a user. I...
0
by: gjohnson2221 | last post by:
I am reading an Excel spreadsheet from a C# application, when I execute the Workbook.Open method it displays the modeless dialog, (which is the menu dialog within the Excel Workbook). How do I stop...
0
by: robin9876 | last post by:
In an .aspx (VB) page that displays that data in Excel which shows the data in excel within the browser. The following code is in the page to identify the file information. ...
5
by: =?Utf-8?B?U3R1YXJ0?= | last post by:
Hi There I have been having a play around with the following code to display a datagrid in Excel (all from Steve Orr's site): Private Sub btnTechServAccred_Click(ByVal sender As System.Object,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.