C# - Concat Partial data from a .txt file to an Excel file
I need help with reading in an excel spreadsheet and adding data to the spreadsheet.
I would like to:
- Open/read an excel spreadsheet (if possible to load into a richtextbox that would be prefered).
- Open/read a specified .txt file using an OpenFileDialog.
- Parse the .txt file data using regular expressions/string.Split method to find the proper data needed to concat with the excel spreadsheet.
- Once this happens, I would like to concat the column of data (found using regex or string.Split in the .txt file) to the last column in the excel spreadsheet while matching the the proper lines.
Here is what I mean:
TEXT FILE
1 0010 147221 PCB,LCD DISPLAY,SB5500
1 0020 147417 FER,BEAD,200MA,1000Z,0.8
1 0030 138666 FER BEAD,220R,2A,0805
1 0040 147418-1 CON,TEST POINT,SMD
1 0580 147470 LED,GRN/RED,20mA,4P,SMD
1 0590 147471 DIODE,SCHOT,RECT,1A,60V
1 0600 147472 DIODE,SCHOT,RECT,3A,40V
...
EXCEL SPREADSHE开发者_JAVA技巧ET
Ln PN Description Something Where Comment MName MCode INITIAL COMMENT
1 EC5547 DESCRIPTION 1.00 EA COMMENT
2 EC0303 DESCRIPTION 2.00 EA COMMENT NAME1 BLM18RK102SN1
3 EC0304 DESCRIPTION 2.00 EA COMMENT NAME1 BLM21PG221SN1
4 EC5080 DESCRIPTION 1.00 EA COMMENT NAME2 TP-107
5 EC5071 DESCRIPTION 1.00 EQ COMMENT NAME3 TSW-110-08-S-S-RA
6 EC5072 DESCRIPTION 1.00 EA COMMENT NAME3 TSW-107-08-S-S-RA
7 EC5075 DESCRIPTION 1.00 EA COMMENT NAME4 FH12-40S-0.5SH(55)
...
I would like to take the third column from the text file (ie, 147221, 147417, 138666, 147418, 147470, 147471, 147472) and add them to the excel spreadsheet. This is what I would like it to end up looking like to be exported as a .txt or a .xls file:
FINAL DOCUMENT
Ln PN Description Something Where Comment MName MCode Item
INITIAL COMMENT
1 EC5547 DESCRIPTION 1.00 EA COMMENT 147221
2 EC0303 DESCRIPTION 2.00 EA COMMENT NAME1 BLM18RK102SN1 147417
3 EC0304 DESCRIPTION 2.00 EA COMMENT NAME1 BLM21PG221SN1 138666
4 EC5080 DESCRIPTION 1.00 EA COMMENT NAME2 TP-107 147418-1
5 EC5071 DESCRIPTION 1.00 EQ COMMENT NAME3 TSW-110-08-S-S-RA 147470
6 EC5072 DESCRIPTION 1.00 EA COMMENT NAME3 TSW-107-08-S-S-RA 147471
7 EC5075 DESCRIPTION 1.00 EA COMMENT NAME4 FH12-40S-0.5SH(55) 147472
...
QUESTION
Does anyone have any suggestions?
Is this for 1 time use only? If it is you could export the Excel file to CSV and use LogParser to JOIN the tables.
Or, you could export the CSV file to another sheet and do you have to do put the 1st column of the CSV file to the last column of the Excel sheet.
This seems like a pretty strightforward Google-able task:
- Probably easiest way would be using early binding Office Automation to open and modify and (finally) save Excel file.
- Reading and parsing CSV file is a trivial task, you can peek at my library for example.
精彩评论