Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

In C# how do I take a csv and seperate data into two tables depending on if the data is line data or header data

I am trying to do the following but am really struggling on coming up with the solution. Please help.

I have a CSV file and this file has data related to invoices from an accounting system.

An invoice can have what I define as header and line data.

Header data is your fields like customer name, reference, document date etc. Line data is repetitive and can be your items/service and prices.

These two types of data are mixed together in a single line.

For example the 3 lines below represent a single invoice:

Line1: Customer Name, reference, document date, item1 price1
Line2: Customer Name, reference, document date, item2 price2
Line3: Customer Name, reference, document date, item3 price3

I want to put the line data into 1 data table and the header in another. However as the above example has 3 lines the header data will be repeated 3 times. how do I add the data into the header table and prevent duplication.


Edit

Hi guys. Sorry, my explanation was poor. Here is an example:

Some data:

Customer1, INV-184691035,Item1, 10.00
Customer1, INV-184691035,Item2, 20.00
Customer1, INV-184691035,Item3, 30.00
Customer2, INV-184691034,Item1, 10.00
Customer2, INV-184691034,Item2, 20.00
Customer2, INV-184691034,Item3, 30.00

I want to spilt this data like this into 2 tables:

Header

Customer1|INV-184691035
Customer2|INV-184691034

Lines

INV-184691035 |Item1 |10.00
INV-184691035 |Item2 |20.00
INV-184691035 |Item3 |30.00
INV-184691034 |Item1 |10.00
INV-184691034 |Item2 |20.00
INV-184691034 |Item3 |30.00

Does this make sense?

question from:https://stackoverflow.com/questions/66063071/in-c-sharp-how-do-i-take-a-csv-and-seperate-data-into-two-tables-depending-on-if

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So you are looking for a way to parse this csv into to tables, one containing the header info and one containing the line data, right?

First read the csv line by line with something like:

using(StreamReader SR = new StreamReader(path2file)){
line = SR.ReadLine();
var entries = line.Split(',');
//now move the elements of entries to the table where they belong, entries should be an array
}

After this reading, loop through the table containing the headers and remove duplicate entries. Is that, what you are looking for?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...