使用FileHelper将可变长度记录写入文件?

时间:2013-01-21 14:41:59

标签: c# filehelpers

我正在使用FileHelper将记录写入平面文件。 这些记录的前20个字段是固定的,如果值为空,则将填充空格。 而最后4个字段是可选的,如果它们都是空的,那么这些位置不应该用空格填充。但FileHelper继续用空格填充这些位置。

是否可以使用FileHelper实现此目的?

[FixedLengthRecord(FixedMode.AllowVariableLength)] 
public class MyReport
{
    [FieldFixedLength(2)]
    public string field1;

    [FieldFixedLength(10)] 
    public string field2;
    ...
    [FieldFixedLength(96)] 
    [FieldOptional]
    public string field32;

    [FieldFixedLength(96)]
    [FieldOptional]
    public string field33;

    [FieldFixedLength(96)]
    [FieldOptional]
    public string field34;

    [FieldFixedLength(96)]
    [FieldOptional]
    public string field35;
}

2 个答案:

答案 0 :(得分:0)

您可以删除AfterWriteRecord event中的任何额外分隔符。

这是一个有效的例子:

[DelimitedRecord(",")]
public partial class Person
{
    public string FirstName;
    public string LastName;
    [FieldOptional]
    public string Optional1;
    [FieldOptional]
    public string Optional2;
    [FieldOptional]
    public string Optional3;
}      

class Program
{
    private static void Main(string[] args)
    {
        var engine = new FileHelperEngine<Person>();
        engine.AfterWriteRecord += engine_AfterWriteRecord;
        var export = engine.WriteString(
                     new Person[] { 
                       new Person() { FirstName = "Joe", LastName = "Bloggs" } 
                     });
        Assert.AreEqual("Joe,Bloggs" + Environment.NewLine, export);
    }

    static void engine_AfterWriteRecord(EngineBase engine, AfterWriteEventArgs<Person> e)
    {
        // trim trailing empty separators
        e.RecordLine = e.RecordLine.TrimEnd(',');
    }
}

答案 1 :(得分:0)

我怀疑这是因为你在每个可选字段上都有FixedLength()属性。据我所知,可选属性实际上只显示为导入,因此如果没有值(null)则不会设置任何内容。

可以在此处找到确认:http://www.filehelpers.net/docs/html/T_FileHelpers_FieldOptionalAttribute.htm

考虑到你正在尝试做什么,我认为你需要编写一个自定义转换器,为你处理这个并取消fixedwidth属性。

def submit_recipe(request):
    recipe_form = RecipeForm()
    dir_formset = DirInline(instance=Recipe())
    ing_formset = IngInline(instance=Recipe())
    if request.method == 'POST':
        recipe_form = RecipeForm(request.POST or None)
        if recipe_form.is_valid():
            recipe = recipe_form.save(commit=False)
            dir_formset = DirInline(request.POST or None, request.FILES, instance=recipe)
            ing_formset = IngInline(request.POST or None, request.FILES, instance=recipe)
            if dir_formset.is_valid() and ing_formset.is_valid():
                recipe.save()
                dir_formset.save()
                ing_formset.save()
                return HttpResponseRedirect(reverse('submit_recipe'))
            else:
                return render(request, 'recipes-orig/submit_recipe.html', 
                                        {'recipe_form':recipe_form,
                                          'dir_formset':dir_formset,
                                          'ing_formset':ing_formset})


    return render(request, 'recipes-orig/submit_recipe.html', 
                                        {'recipe_form':recipe_form,
                                          'dir_formset':dir_formset,
                                          'ing_formset':ing_formset
                                        })