Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How can I generate CSV without header and control the default value of Integer? #1288

Closed
roksanaShimu opened this issue Apr 11, 2019 · 7 comments

Comments

@roksanaShimu
Copy link

roksanaShimu commented Apr 11, 2019

I am trying to create CSV string, But I am facing few issues.
`
private static void writeDataTest()
{
var records = new List
{
new Foo { Id = 1, Name = "one" },
new Foo { Id = 2, Name = "two" },
new Foo { Id = 3, Name = "three" },
};

         using (var writer = new StringWriter())
         using (var csv = new CsvWriter(writer))
         {
            csv.Configuration.RegisterClassMap<FooMap>();
            csv.WriteRecords(records);
           var s = writer.ToString();
         }
    }

   public class Foo
     {
        public int Id { get; set; }
        public int key { get; set; }
        public int key2 { get; set; }
        public string str { get; set; }
        public string Name { get; set; }
        public string Version { get; set; } 
        public Guid guid { get; set; }
    }

    public class FooMap : ClassMap<Foo>
    {
        public FooMap()
        {
            Map(m => m.Id).Index(0);
            Map(m => m.key).Index(1);
            Map(m => m.Name).Index(2);
            Map(m => m.str).Index(3);
            Map(m => m.Version).Index(4);
            Map(m => m.guid).Index(5);
        }
    } `

I am getting the output string s as

Id,key,Name,str,Version,guid
1,0,one,,,00000000-0000-0000-0000-000000000000
2,0,two,,,00000000-0000-0000-0000-000000000000
3,0,three,,,00000000-0000-0000-0000-000000000000

But I want the output as follows

1,,one,,,
2,,two,,,
3,,three,,,

  1. Create the csv without header
  2. If on value is provided (say, there is no guid), keep the field empty. Don't put zero
  3. Can I also convert a string to byte Array using TypeConversion like:
    var converter = new ByteArrayConverter(ByteArrayConverterOptions.None); var result = converter3.ConvertFromString("abc", null, null);
@roksanaShimu
Copy link
Author

roksanaShimu commented Apr 11, 2019

updating FooMap also didn't work.
public class FooMap : ClassMap<Foo> { public FooMap() { Map(m => m.Id).Index(0).Default(""); Map(m => m.key).Index(1).Default(""); Map(m => m.Name).Index(2).Default(""); Map(m => m.str).Index(3).Default(""); Map(m => m.Version).Index(4).Default(""); Map(m => m.guid).Index(5).Default(""); } }

@AltruCoder
Copy link

AltruCoder commented Apr 11, 2019

  1. Set the configuration for HasHeaderRecord to false.
csv.Configuration.HasHeaderRecord = false;
  1. Change your int and Guid properties to nullable. Otherwise they will have the default values for those properties.
public class Foo
{
    public int Id { get; set; }
    public int? key { get; set; }
    public int? key2 { get; set; }
    public string str { get; set; }
    public string Name { get; set; }
    public string Version { get; set; }
    public Guid? guid { get; set; }
}
  1. I'm not quite sure what you are trying to do.

@roksanaShimu
Copy link
Author

Thank you so much. It resolved my #2 issue.

@AltruCoder
Copy link

I updated my response to answer your first question. I'm not quite sure what you are trying to do with question # 3. Trying to use the converter outside of CsvHelper is not going to work. It needs the 2nd two options you have set to null.

@roksanaShimu
Copy link
Author

Thanks once again. For question 3: In my code, I often need to convert string to byte [] and vice versa (As i store my data in DB as byte[]. and display data in string) So currently I am using Encoding.UTF8.GetBytes() or Encoding.UTF8.GetString() for conversions. I was wondering if CSVHelper has any type conversion method that performs better than what I am using now.

@AltruCoder
Copy link

AltruCoder commented Apr 11, 2019

I'm looking at the code and my other statement was incorrect. You can use it as your code suggests, you don't need the other two parameters. The problem is that it only appears to convert from Base64 strings or Hex strings.

public override object ConvertFromString( string text, IReaderRow row, MemberMapData memberMapData )
{
	if( text != null )
	{
		return ( options & ByteArrayConverterOptions.Base64 ) == ByteArrayConverterOptions.Base64
			? Convert.FromBase64String( text )
			: HexStringToByteArray( text );
	}

	return base.ConvertFromString( text, row, memberMapData );
}

ByteArrayConverter link.

@roksanaShimu
Copy link
Author

Thank you for saving my day. It helped a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants