将单元格格式化为CSV格式的文本 [英] Format cell as text in CSV

查看:342
本文介绍了将单元格格式化为CSV格式的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.txt文件数据,其中100多列和行转换为.csv格式文件,一切正常。但问题是,我得到一些指数值,例如48123E + 1因为那些尺寸较大而我不想要,所以有没有办法将单元格格式化为文本以避免这些值并以简单数字004812334242424的形式获取值。下面是我的代码snipet但没有成功。请帮忙。



预付款。



例如我有column1,column2,column3,column4 ....等



并且只有column3,column5,column100需要格式化为仅文本...它将是对我来说有点好。



我尝试过的事情:



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();

推荐答案

CSV不是格式化的文件:它实际上是一个包含文本的文件用逗号分隔的数据,用行分隔的行:

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

三行,三列,第1行,第2行第2行,第3行第3行。

这就是CSV格式化 - 没有CELL,没有引用,没有这是一个字符串设置。



如果你想要更复杂的格式化 - 如果你想要任何格式化,那么那么CSV是文件格式的错误选择:你需要一个DB,或者JSON,或XML,或者XLSX。

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.


请查看我以前的答案:将长数写入csv [ ^ ]。
Please, check out my past answer: Writing long numbers to csv[^].


这篇关于将单元格格式化为CSV格式的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆