C#处理固定宽度的文件

标签 c# file width fixed

我有一组具有不同列数和字段大小的固定宽度文件。

文件的顶部以这样一行开头:

AAAAABBCCCCCCCCCCDDD 等等

字符的变化表示一个域的结束和另一个域的开始。我猜有人可以用代码计算字段大小,然后将相同的值应用于下面的实际数据行。

然后我想将所有读取的数据输出到一个 XLS 文件甚至一个 DataGrid 中,但我的问题是我不知道如何对此进行编码。

任何帮助将不胜感激:)


/编辑:

我实现了 Cuong 的解决方案,虽然它在我的家用 PC 上测试时运行良好,但我不得不使用 c# v4 编译它,因为我们的工作 PC 有 Windows XP。

无论如何,在读取输入文件时出现以下错误:

************** 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
----------------------------------------

最佳答案

下面以csv文件(excel类型)为例,重点是需要读取第一行并计算宽度:

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#处理固定宽度的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12543223/

相关文章:

file - 从 Common Lisp 中的文本文件中读取序列

css - 在 :active without moving surrounding elements 上变大的元素

html - 如何用 div 填充边距空间并赋予它样式?

c# - MVC 2 中使用 JQuery 的验证摘要

c# - 如何在另一个类的 CacheItemPolicy 上实现 UpdateCallback?

c# - 如何从列表时间中获取最接近的项目

java - 如何在java中使用StringTokenizer将int从文件存储到Arraylist?

java - 列出android中一种类型的所有文件

jquery - 如何让我的样式选择菜单成为 Chrome 中父容器的 100% 宽度?

c# - 如何防止 Visual Studio 在 Web 项目中使用 "publishing"XML 文档文件?