FileHelpers:如何跳过第一和最后一行读取固定宽度的文本 [英] FileHelpers: How to skip first and last line reading fixed width text

查看:118
本文介绍了FileHelpers:如何跳过第一和最后一行读取固定宽度的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

code以下是用来在ASP .NET MVC2使用FileHelpers读取固定宽度上传文件内容的文本文件

第一次和最后一行的长度较小,ReadStream导致由于此异常。
所有其他的线都正确的固定宽度。
如何skipt第一和最后一个行或其他方式无一例外读取数据?

  [FixedLengthRecord()]
    银行类
    {
        [FieldFixedLength(4)]
        公共字符串AINETUNNUS;
        [FieldFixedLength(16)]
        公共字符串TEKST1;
        [FieldFixedLength(3)]
        公共字符串opliik;
        [FieldFixedLength(2)]
        公共字符串hinnalis;
    };    的[AcceptVerbs(HttpVerbs.Post)
    [授权]
    公众的ActionResult LoadStatement(HttpPostedFileBase uploadFile)
    {        FileHelperEngine发动机=新FileHelperEngine(typeof运算(银行));
        VAR解析度= engine.ReadStream(新的StreamReader(uploadFile.InputStream,
             Encoding.GetEncoding(1257)))作为银行[];
  }


解决方案

您可以使用这些属性。

IgnoreFirst :表示线条的数量在一个文件的开头被忽略或当发动机阅读流

  [IgnoreFirst(1)]
公共类OrdersVerticalBar
{...

IgnoreLast :指示线的数量在文件的结尾被忽略或当发动机阅读流

  [IgnoreLast(1)]
公共类OrdersVerticalBar
{...

您可以在以后访问值

  engine.HeaderText
engine.FooterText

Code below is used to read fixed width uploaded file content text file using FileHelpers in ASP .NET MVC2

First and last line lengths are smaller and ReadStream causes exception due to this. All other lines have proper fixed width. How to skipt first and last lines or other way to read data without exception ?

    [FixedLengthRecord()]
    class Bank
    {
        [FieldFixedLength(4)]
        public string AINETUNNUS;
        [FieldFixedLength(16)]
        public string TEKST1;
        [FieldFixedLength(3)]
        public string opliik;
        [FieldFixedLength(2)]
        public string hinnalis;
    };

    [AcceptVerbs(HttpVerbs.Post)]
    [Authorize]
    public ActionResult LoadStatement(HttpPostedFileBase uploadFile)
    {

        FileHelperEngine engine = new FileHelperEngine(typeof(Bank));
        var res = engine.ReadStream(new StreamReader(uploadFile.InputStream,
             Encoding.GetEncoding(1257))) as Bank[];
  }

解决方案

You can use these attributes

IgnoreFirst: Indicates the numbers of lines to be ignored at the begining of a file or stream when the engine read it.

[IgnoreFirst(1)] 
public class OrdersVerticalBar 
{ ...

IgnoreLast: Indicates the numbers of lines to be ignored at the end of a file or stream when the engine read it.

[IgnoreLast(1)] 
public class OrdersVerticalBar 
{ ...

You can access the values later with

engine.HeaderText
engine.FooterText

这篇关于FileHelpers:如何跳过第一和最后一行读取固定宽度的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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