如何在C#中编写固定长度的文本文件 [英] How to write text file with fixed length in C#

查看:198
本文介绍了如何在C#中编写固定长度的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将数据写入指定位置的文件,每列具有固定长度,包括起始位置,结束位置和列长度。你可以改变我的代码或建议吗。我给每列的样本长度



i need to write data to file in specified position with fixed length for each column with start position,end position and length of column. could you please alter my code or suggest.I have given sample length for each column

Column	startposition  endposotion  Length
     1		1	        9	         9	
     2		10	       20	         10
     3		21          29           8
     4      30          31           1
     5		32	        42          10











输出我需要在文本文件中写作:








Output i needed to write in text file as:

00000	  2014-09-23 01:36:22 F	2014-09-23
000000	  2014-09-23 01:36:22 F	2014-09-23
000000000 2014-09-23 01:36:22 F	2014-09-23





以下是我创建的代码





below is code which i have created

string txt = string.Empty;
DataTable employeeTable = new DataTable("Employee");
employeeTable.Rows.Add("00000", "2014-09-23", "01:36:22", "F", "2014-09-23");
employeeTable.Rows.Add("000000", "2014-09-23", "01:36:22", "F", "2014-09-23");
employeeTable.Rows.Add("000000000", "2014-09-23", "01:36:22", "F", "2014-09-23");
DataSet ds = new DataSet("Organization");
ds.Tables.Add(employeeTable);
string filepath = Application.StartupPath + "\\" + "file.txt" ;
StreamWriter sw = null;
sw = new StreamWriter(filepath, false);
foreach (DataRow row in ds.Tables[0].Rows)
{
foreach (var item in row.ItemArray)
{
//Add the Data rows.
txt += item.ToString() + "\t";
}
txt += "\r\n";
}
sw.Write(txt);
sw.Close();





从这段代码中得到的OutPut是,但这是错误的。



OutPut which iam getting from this code is, but this is wrong.

00000	2014-09-23	01:36:22	F	2014-09-23	
000000	2014-09-23	01:36:22	F	2014-09-23	
000000000	2014-09-23	01:36:22	F	2014-09-23





请纠正我的问题。



我尝试过的事情:





kindly rectify my problem.

What I have tried:

string txt = string.Empty;
DataTable employeeTable = new DataTable("Employee");
employeeTable.Rows.Add("00000", "2014-09-23", "01:36:22", "Falseis1", "2014-09-23", "01:36:22");
employeeTable.Rows.Add("000000", "2014-09-23", "01:36:22", "F", "2014-09-23", "01:36:22");
employeeTable.Rows.Add("000000000", "2014-09-23", "01:36:22", "F", "2014-09-23", "01:36:22");
DataSet ds = new DataSet("Organization");
ds.Tables.Add(employeeTable);
string filepath = Application.StartupPath + "\\" + "file.txt" ;
StreamWriter sw = null;
sw = new StreamWriter(filepath, false);
foreach (DataRow row in ds.Tables[0].Rows)
{
foreach (var item in row.ItemArray)
{
//Add the Data rows.
txt += item.ToString() + "\t";
}
txt += "\r\n";
}
sw.Write(txt);
sw.Close();

推荐答案

将对象转换为字符串后,可以使用PadLeft和PadRight方法将它们证明为固定长度的字符串:

After you have converted an object to a string, you can use the PadLeft and PadRight methods to justify them into a fixed length string:
object o1 = "0000";
object o2 = "00000";
Console.WriteLine("\"" + o1.ToString().PadLeft(8) + "\"");
Console.WriteLine("\"" + o2.ToString().PadRight(8) + "\"");

会给你:

Will give you:

"    0000"
"00000   "

你可以在每个字段,为您提供所需的固定宽度。

You can use this on each field to give you the fixed widths you need.


不要在循环中使用字符串连接!你已经有一个 StreamWriter ,你可以用它来写每个字符串的部分。



And固定宽度输出的最简单选项是使用字符串格式化功能的对齐选项:

复合格式| Microsoft Docs [ ^ ]

Don't use string concatenation in a loop! You've already got a StreamWriter which you can use to write each part of the string to.

And the simplest option for fixed-width output is to use the alignment option of the string formatting feature:
Composite Formatting | Microsoft Docs[^]
using (StreamWriter sw = new StreamWriter(filepath, false))
{
    foreach (DataRow in ds.Tables[0].Rows)
    {
        writer.WriteLine("{0,-9}{1,-10}{2,-8}{3}{4,-10}", row.ItemArray);
    }
}

输出:

00000    2014-09-2301:36:22F2014-09-23
000000   2014-09-2301:36:22F2014-09-23
0000000002014-09-2301:36:22F2014-09-23


这篇关于如何在C#中编写固定长度的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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