如何在C#中将csv格式更改为数字 [英] How to change csv format to number in C#

查看:241
本文介绍了如何在C#中将csv格式更改为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了以.csv格式编写的应用程序。我需要在默认情况下将格式更改为所有列的.csv文件编号。



请帮助我,如何将格式更改为Number。我需要输出如下格式。



例如:



I have developed application to write in .csv format. I need to change the format to number of the .csv file for all columns by default.

Kindly help me out, how to change the format to Number. I need output as below format.

for example:

USER        	Active
0168	        0124341156
0168	        0124341156
0168	        0124341156





但我得到输出为





But iam getting output as

USER        	Active
168	        124341156
168	        124341156
168	        124341156





因此,为了以正确的格式输出,我需要将其更改为数字格式。所以我可以在数字的开头得到0。







So in order to get output in correct format, i need to change it as number format. So that i can get "0" in beginning of number.


var lines = new List<string>();
string[] columnNames = ldatatables.Exportdatatable.Columns.Cast<DataColumn>().
                                         Select(column => column.ColumnName).
                                         ToArray();
var header = string.Join(",", columnNames);
lines.Add(header);
var valueLines = ldatatables.Exportdatatable.AsEnumerable()
                  .Select(row => string.Join(",", row.ItemArray));
lines.AddRange(valueLines);
File.WriteAllLines("Test.csv", lines);





我的尝试:





What I have tried:

var lines = new List<string>();
string[] columnNames = ldatatables.Exportdatatable.Columns.Cast<DataColumn>().
                                         Select(column => column.ColumnName).
                                         ToArray();
var header = string.Join(",", columnNames);
lines.Add(header);
var valueLines = ldatatables.Exportdatatable.AsEnumerable()
                  .Select(row => string.Join(",", row.ItemArray));
lines.AddRange(valueLines);
File.WriteAllLines("Test.csv", lines);

推荐答案

没有一种标准的方法可以做到这一点,无论如何都不能做到这一点yoru方案 - 您不能只假设数据将是数字并使用ToString覆盖前导零格式,因为它不适用于您行中的非数字项。



即使这些项目都是数字,你的方案仍然不可能,因为输出中你想要的位数对于所有行都不一样。你需要一个每种格式都不同。

虽然可以通过一系列格式字符串来解决这个问题,但它是一个混乱的解决方案。



要成为老实说,你可能根本不应该这样做:前导零是一个演示功能,而不是数据存储功能,应该真正应用于读取CSV文件并显示它的软件,而不是存储在数据文件本身。
There isn't a standard way to do that, and it wouldn't be trivial to do anyway with yoru scheme - you can't just assume the data is going to be numeric and use a ToString override with a "leading zero format" because it won't work for non-numeric items in your row.

And even if the items are all numbers, it's still not possible which your scheme, because the number of digits you want in the output isn't the same for all rows., and you need a different format for each.
While it's possible to get round that with an array of format strings, it's a messy solution.

To be honest, you probably shouldn't be doing it at all: leading zeros are a presentation feature, not a data storage feature, and should really be applied by the software that reads the CSV file and displays it, not stored in the data file itself.


As OriginalGriff [ ^ ]已经提到过,没有标准的方法来实现这一点,但是......你们走在了正确的轨道上!



假设 USER Active 字段是整数的类型而你想要添加前导零,您必须将选择语句更改为:

As OriginalGriff[^] already mentioned, there's no standard way to achieve that, but... you're on the right track!

Assuming that USER and Active fields are type of integer and you want to add leading zeros, you have to change your Select statement to:
var valueLines =  dt.AsEnumerable()
					.Select(row =>
						string.Concat( 
							row.Field<int>("USER").ToString("0000"),
							",",
							row.Field<int>("Active").ToString("0000000000"))
					).ToList();





祝你好运!



Good luck!


这篇关于如何在C#中将csv格式更改为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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