C#处理固定宽度的文件 [英] C# Processing Fixed Width Files

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

问题描述

我有与可变数量的列和字段大小的固定宽度文件的集合



文件的顶部开始以类似的行:

AAAAABBCCCCCCCCCCDDD等



的特性的变化表示一个场的结束和另一个开始。我猜这可能有人被用来制定出什么样的场大小的代码,然后在相同的值适用于低于实际数据线。



然后我想输出所有的数据读入一个XLS文件,甚至一个DataGrid,但我的问题是,我不知道如何编写这一点。



任何帮助将不胜感激:)






/编辑:



我实现的Cuong的解决方案,虽然我家里的电脑上运行良好的测试,我不得不用C#V4编译它作为我们工作的电脑是Windows XP。



不管怎么说,读取输入文件时,我得到以下错误:

  **************异常文本****** ******** 
System.ObjectDisposedException:不能从一个封闭的TextReader读取。
在System.IO .__ Error.ReaderClosed()
在System.IO.StreamReader.ReadLine()
在有System.IO.File< InternalReadLines> d__0.MoveNext()
在System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()在System.IO.File.InternalWriteAllLines
(TextWriter的作家,IEnumerable`1内容)
在System.IO.File.WriteAllLines(字符串路径,IEnumerable`1内容)的FixedWidthFiles.Main.buttonProcessFile_Click
(对象发件人,EventArgs五)
在System.Windows.Forms.Control.OnClick(EventArgs五)
的系统。 Windows.Forms.Button.OnClick(EventArgs五)
在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在System.Windows.Forms.Control.WmMouseUp(消息和M,MouseButtons按钮,点击的Int32)在System.Windows.Forms.Control.WndProc(消息和
,M)
在System.Windows.Forms.ButtonBase.WndProc(消息和M)
在System.Windows .Forms.Button.WndProc(消息&放大器;米)
。在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息&放大器;米)
在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和M)
在System.Windows.Forms.NativeWindow.Callback(IntPtr的的HWND,味精的Int32,IntPtr的WPARAM,LPARAM的IntPtr)


**************加载的程序集**************
mscorlib程序
大会版本:4.0.0.0
Win32版本:4.0.30319.1(RTMRel.030319-0100)
基本代码:文件:/// C:/WINDOWS/Microsoft.NET/Framework/v4.0.30319/ mscorlib.dll中
----------------------------------------
FixedWidthFiles
议会版本:1.0.0.0
Win32版本:1.0.0.0
基本代码:文件:/// C:/TEMP/FixedWidthFiles.exe
----- -----------------------------------
System.Windows.Forms的
集版本:4.0.0.0
Win32版本:4.0.30319.1通过内置:RTMRel
基本代码:文件:/// C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4。 0_4.0.0.0__b77a5c561934e089 / System.Windows.Forms.dll的
--------------------------------- -------
System.Drawing中
议会版本:4.0.0.0
Win32版本:4.0.30319.1通过内置:RTMRel
基本代码:文件:/// C :/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------- -----------------------
系统
议会版本:4.0.0.0
Win32版本:4.0.30319.1由建:RTMRel
基本代码:文件:/// C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
------- ---------------------------------
System.Core程序
议会版本:4.0.0.0
Win32版本:4.0.30319.1通过内置:RTMRel
基本代码:文件:/// C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/ System.Core.dll
$ ---------------------------------------- b $ b


解决方案

下面的例子如何在csv文件写入(Excel类型) ,主要的一点是,你需要阅读的第一行,并计算宽度:

  VAR线= File.ReadLines(C: \\input.txt); 

VAR widthList = lines.First()GROUPBY(C => C)
。选择(G => g.Count())
.ToList() ;

无功名单=新名单,LT; KeyValuePair< INT,INT>>();

INT的startIndex = 0;

的for(int i = 0; I< widthList.Count();我++)
{
变种对=新KeyValuePair< INT,INT>(的startIndex,widthList [一世]);
list.Add(对);

的startIndex + = widthList [I]
}

变种csvLines = lines.Select(线= GT;的string.join(,,
list.Select(对= GT; line.Substring(对。键,pair.Value))));

File.WriteAllLines(C:\\test.csv,csvLines);


I have a collection of fixed width files with a varying number of columns and field sizes.

The top of the file starts with a line like:

AAAAABBCCCCCCCCCCDDD and so on

The change in character denotes the end of one field and the start of another. I am guessing this can someone be used to work out what the field sizes are with code and then apply the same values to the actual data lines below.

I then want to output all the read data into a XLS file or even a DataGrid, but my issue is I have no idea how to code this.

Any help would be greatly appreciated :)


/Edit:

I implemented Cuong's solution and although that worked fine for testing on my home PC, I had to compile it with c# v4 as our work PCs have Windows XP.

Anyways, when reading the input file I get the following error:

************** Exception Text **************
System.ObjectDisposedException: Cannot read from a closed TextReader.
   at System.IO.__Error.ReaderClosed()
   at System.IO.StreamReader.ReadLine()
   at System.IO.File.<InternalReadLines>d__0.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.IO.File.InternalWriteAllLines(TextWriter writer, IEnumerable`1 contents)
   at System.IO.File.WriteAllLines(String path, IEnumerable`1 contents)
   at FixedWidthFiles.Main.buttonProcessFile_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
    CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
FixedWidthFiles
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/TEMP/FixedWidthFiles.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------

解决方案

Below example how to write in csv file (excel type), the main point is you need to read the first line and calculate width:

var lines = File.ReadLines("C:\\input.txt");

var widthList = lines.First().GroupBy(c => c)
                             .Select(g => g.Count())
                             .ToList();

var list = new List<KeyValuePair<int, int>>();

int startIndex = 0;

for (int i = 0; i < widthList.Count(); i++)
{
    var pair = new KeyValuePair<int, int>(startIndex, widthList[i]);
    list.Add(pair);

    startIndex += widthList[i];
}

var csvLines = lines.Select(line => string.Join(",", 
                    list.Select(pair => line.Substring(pair.Key, pair.Value))));

File.WriteAllLines("C:\\test.csv", csvLines);

这篇关于C#处理固定宽度的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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