C# - 将 .txt 文件中的部分数据连接到 Excel 文件
我需要阅读 Excel 电子表格并将数据添加到电子表格的帮助。
我想:
- 打开/读取 Excel 电子表格(如果可能的话,加载到首选的 Richtextbox 中)。
- 使用 OpenFileDialog 打开/读取指定的 .txt 文件。
- 使用正则表达式/字符串解析 .txt 文件数据。Split 方法找到与 Excel 电子表格连接所需的正确数据。
- 一旦发生这种情况,我想将数据列(使用 .txt 文件中的正则表达式或 string.Split 找到)连接到 Excel 电子表格中的最后一列,同时匹配正确的行。
我的意思是:
文本文件
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 SPREADSHEET
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)
...
我想从文本文件中获取第三列(即 147221、147417、138666、147418、147470、147471、147472)并将它们添加到Excel 电子表格。这就是我希望它最终导出为 .txt 或 .xls 文件的样子:
最终文档
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
...
问题
有人有什么建议吗?
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 SPREADSHEET
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个只能使用1次吗?如果是,您可以将 Excel 文件导出为 CSV 并使用 LogParser 来加入表。
或者,您可以将 CSV 文件导出到另一个工作表,并且是否必须将 CSV 文件的第一列放入 Excel 工作表的最后一列。
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.
这似乎是一个非常简单的谷歌任务:
早期绑定办公自动化
打开并修改(最后)
保存 Excel 文件。
琐碎的任务,您可以查看 我的
例如,库。
This seems like a pretty strightforward Google-able task:
early binding Office Automation
to open and modify and (finally)
save Excel file.
trivial task, you can peek at my
library for example.