如何解析文本文件并将结果写入xls文件?
大家好,我是一名初级 SQL 开发人员。我遇到的情况是,我有一个包含 1100 行搜索结果的文本文件,每行都包含一个文件路径和一个与该文件关联的存储过程。每行的结构如下:
abc\def\ghi\***.cs(40): jkl=******.*****.******, "proc_pqrst", parms);
其中 abc\def\ghi\***.cs
是文件 ***.cs
的文件路径。存储过程名称以 proc_
开头。我必须提取 ***.cs
和以 proc_
开头的相应存储过程名称,并将它们写入 .xls 文件。有人可以帮助我编写解析程序来做到这一点吗?另外,我可以获得关于应该在哪里编写 c# 代码以及应该在哪里编译它的详细概要吗?这可能会很有帮助,因为我对 C# 没有任何了解。
谢谢你, 贝克。
Hi all i am a junior level SQL developer. I have a situation where I have a text file with 1100 lines of a search result with each line containing a file path and a stored procedure associated with that file. Each line has a structure like the one below:
abc\def\ghi\***.cs(40): jkl=******.*****.******, "proc_pqrst", parms);
Where abc\def\ghi\***.cs
is file path of the file ***.cs
. The stored procedure names begin with proc_
. I have to extract the ***.cs
and the corresponding stored procedure name begining with proc_
and write them to a .xls file. Can some body help me in writing the parsing program to do this? Also can I get a detailed outline on where should I write the code for c# and where should I compile it? This could be a great help as I don't have any knowledge of C#.
Thank you,
BK.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这可能听起来很愚蠢,但更好的选择可能是将原始 CSV 导入 Excel,然后编写一个宏来进行提取。可能比 C#、Visual Studio 等的学习曲线更温和。
This might sounds silly, but a better option might be to import the raw CSV into Excel and then write a macro to do the extraction. Might be a gentler learning curve than C#, Visual Studio, etc.
如果您绝对需要 xls 文件(而不仅仅是 csv),FileHelpers 库可以轻松编写xls 文件。
FileHelpers 采用强类型类并将其写入 csv 或 xls,或者可以直接连接到 SQL 服务器并在那里保存记录。
基本上,您会得到类似的结果:
您可以使用 Robert Harvey 的代码或正则表达式来提取路径和过程名称,创建一个新的
Record
,并将它们放入新对象中。然后,您可以使用 ExcelStorage 类来保存文件。
If you absolutely need an xls file (instead of just csv), the FileHelpers library makes it easy to write an xls file.
FileHelpers takes a strongly typed class and writes it as a csv or xls, or can connect directly to a SQL server and save records there.
Basically, you'd have something like:
You could use Robert Harvey's code or a regex to extract the path and procedure names, create a new
Record
, and drop them in the new object.You can then use the ExcelStorage class to save the file.
至于解析,您可以使用正则表达式,例如这个:(
顺便说一句,我没有测试它,所以它可能不起作用)
在导出到 XLS 文件方面,这有点困难。如果您要在安装了 Excel 的 Windows 机器上运行它,那么我相信您可以使用 COM 让 Excel 为您完成此操作,但否则,您必须: 1. 实现 XLS 文件格式(很难),2. 购买 XLS 文件格式的实现(昂贵),或者 3. 做其他事情,例如自动化 OpenOffice 之类的。
As for parsing, you could use regular expressions, such as this one:
(by the way, I didn't test that, so it might not work)
On the exporting to XLS file side of things, that's a bit harder. If you're going to be running it on a Windows box that has Excel installed, then I believe you can use COM to make Excel do it for you, but otherwise, you'll have to either: 1. Implement the XLS file format (hard), 2. buy an implementation of the XLS file format (expensive), or 3. do something else, such as automating OpenOffice or something.
要创建可在 Excel 中读取的文件,只需写出逗号分隔文件 (CSV)。
To create a file readable in Excel, just write out a comma-delimited file (CSV).