Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am working with a .txt file data with 100+ columns and rows transferring into .csv format file and everything is working fine. but problem is , i am getting some exponential values e.g. 48123E+1 because those are larger in size which i don't want , so is there any way to format cell as text to avoiding these values and get the values in the form of simple number 004812334242424. Below is my code snipet but no success . please help.

thanks in Advance.

e.g. i have column1,column2,column3,column4.... etc

and only column3,column5,column100 needs to be format as text only ... it would be slightly great for me .

What I have tried:

var result = new StringBuilder();
            StreamWriter objWriter = new StreamWriter(destPath, false);
            int cnt = 0;
            foreach (DataColumn dc in dt.Columns)
            {
                result.Append(dc.ColumnName);
                result.Append(cnt == dt.Columns.Count - 1 ? Environment.NewLine : ",");
                cnt = cnt + 1;
            }

            foreach (DataRow row in dt.Rows)
            {
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    result.AppendFormat("=TEXT(C1," + row[i] + "),");
                    result.Append(row[i].ToString().Replace("\n", ""));
                    result.Append(i == dt.Columns.Count - 1 ? "\n" : ",");
                    
                }
                //result.Append();
            }
            objWriter.WriteLine(result.ToString());
            objWriter.Close();
Posted
Updated 13-Oct-22 3:29am
v2

CSV is not a "formatted" file: it is literally a text file containing data that is separated by commas, with rows delimited by lines:
1, Hello there, 3
2, Goodbye, 4
Is two rows, each of three columns.
To allow commas and newlines to be embedded in a column, you can use double quotes to surround text:
1, Hello there, 3
2, "Goodbye, 4"
3, "Hello
there", 5
Is three rows, 3 columns, on row 1, 2 on row 2, and 3 on row 3.
That's it for CSV formatting - no CELL, no references, no "this is a string" setting.

If you want more complicated formatting - heck if you want any formatting - then CSV is the wrong choice of file format: you need a DB, or JSON, or XML, or XLSX.
 
Share this answer
 
Comments
Maciej Los 20-Sep-18 8:29am    
5ed!
Member 11143448 21-Sep-18 2:45am    
Thanks Griff for your Response.
however i found to use various method to create a csv file but we can not avoid the scientific notation for larger number while opening the csv file hence csv have nature to convert these number into scientific notations , so what i did is , i just created one program to read the same CSV file whatever i have created and reading the same data in correct format in Datatable.
Cheers !!
Please, check out my past answer: Writing long numbers to csv[^].
 
Share this answer
 
Comments
Maciej Los 21-Sep-18 3:42am    
I love downvoters, especially those whose flinch to write comment about the reason of down-vote.
Thanks!
Did you came to solution since I am stuck on same , please help!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900