質問

I'm using CsvHelper to write a program that outputs a bunch of data onto a CSV file. The header row is a row of about 35 columns. Here's some of the code here...

[CsvColumn(Name = "Lease Name", FieldIndex = 1)]
public string leaseName2 { get; set; }

[CsvColumn(Name = "Field Name", FieldIndex = 2)]
public string fieldName2 { get; set; }

[CsvColumn(Name = "Reservoir", FieldIndex = 3)]
public string reservoir2 { get; set; }

[CsvColumn(Name = "Operator", FieldIndex = 4)]
public string operator2 { get; set; }

[CsvColumn(Name = "County", FieldIndex = 5)]
public string county2 { get; set; }

[CsvColumn(Name = "State", FieldIndex = 6)]
public string state2 { get; set; }

[CsvColumn(Name = "Majo", FieldIndex = 7)]
public string majo2 { get; set; }

I was just wondering if I could, so to speak, change the columns width or change the background color of the cell or do anything to that matter. I've been searching the web for a while and have not come up with anything. Any help would be greatly appreciated.

役に立ちましたか?

解決

Simple answer is, you can't do that with a CSV. That's not what it is or what it's meant for. It's just comma-delimited-values, that's it.

If you want to control aesthetics, you'll need to choose a format that can support these options, such an actual excel (xls) format.

This is why when you open a CSV in excel and try to save it with formatting, it warns you all formatting will be lost if you keep the .CSV format.

他のヒント

The CSV files do not contain any formatting so Excel has no clue how to format the data that was imported from the file. It simply applies a few default formatting.

What ever you import by CSV, it will never have any fancy color/ font/ column width/ whatever formatings.

As workarounds, 2 ideas come to my mind:

  1. I am not familiar with Excel programming, but I am sure that you could implement a VBA Macro that could be run after the data import that the does the formatting for you.

  2. You could implement stay in C# and implement the import in code. There are SDKs available from Microsoft to help you with it. I would guess that you have all formatting functionality available in there, too. This would surely work, but I have to admit, the work to implement this manually does not seem to reasonable to me.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top