执行查找&在 C# 中替换 Excel 文档
我正在尝试创建一个在 C# 中动态填充的邮资标签。
我认为我可以做的是在 Excel 中创建标签,以便获得完美的布局,然后使用 OpenXML 替换字段。即我有一些名为 XXAddressLine1XX
的字段,我想在其中编辑 XML 并将该文本替换为数据库中的实际 Address Line 1
。
有没有人以前做过类似的事情并且可以发布一些我可以尝试的代码?我以前曾使用 OpenXML 对 Word 文档执行此操作,但在 C# 中使用 OpenXML 时,我似乎无法找到 Excel 文档的 XML 数据,因此我正在努力取得进展。
或者说有什么更好的方法我可以尝试实现这一目标?
I am trying to create a postage label that is populated dynamically in C#.
What I thought I could do was create the label in Excel so I could get the layout perfect, then use OpenXML to replace the fields. i.e. I have fields called things like XXAddressLine1XX
, where I want to edit the XML and replace that text with the actual Address Line 1
from the database.
Has anyone actually done something similar to this before and could post some code up that I could try? I've used OpenXML to do this with word documents before, but I can't seem to find the XML data for the Excel document when using OpenXML in c# so am struggling to make progress.
Either that, or are there any better methods I could try to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
时不时地,会出现一个最好用非编程解决方案来解决的编程问题。长期以来,企业一直需要批量打印邮件标签,近几十年来,Microsoft Word 等程序通过“邮件合并”功能使这一过程变得非常简单。请参阅 http://office.microsoft.com/en-us/word-help/demo-use-mail-merge-to-format-and-print-mailing-labels-HA001190394.aspx 用于一个例子。
Word 的邮件合并将允许您连接到各种数据源。给定的示例使用 Excel 电子表格,但您也可以使用 Access 或 SQL 数据库等。
Every now and then, there's a programming problem that is best solved with a non-programming solution. Businesses have had the need for printing mailing labels en masse for a long time, and in recent decades programs like Microsoft Word make this really simple via the "mail merge" feature. See http://office.microsoft.com/en-us/word-help/demo-use-mail-merge-to-format-and-print-mailing-labels-HA001190394.aspx for an example.
Word's mail merge will allow you to connect to a variety of data sources. The given example uses an Excel spreadsheet, but you can also use Access or SQL Databases, etc.
纯粹的学术答案是:有可能。我编写了一组模板解析类,用于搜索 PowerPoint 演示文稿,用从数据库获取的图表和动态文本对象替换我发明的标记语言中的标签。字符串替换位中最棘手的部分是处理
Paragraph
元素内的Run
元素中出现的标签。如果您在标记中使用特殊字符(例如“{”或空格),通常会发生这种情况。我能够通过将整个TextBody
元素的文本存储在一个巨大的字符数组中(在您的情况下它将是Cell
元素的内容)来解决这个问题,存储范围列表,枚举字符数组中每个Run
元素的开始和结束位置,然后遍历字符数组,同时适当注意Run
边界。请注意,如果您的代码跨越多个Run
元素,则在插入替换Run
之前,您需要删除所有多余内容并跨边界剪切内容。不幸的是,我无法发布任何代码,因为这项工作是为一家公司完成的,但这就是如何实现它的总体思路。我无法处理任何换行符情况(即标签中出现换行符),因为这需要编写一个跨Paragraph
索引器,这超出了我想要实现的范围。这也可以做到,但我认为这会困难得多。The purely academic answer is: it's possible. I wrote a set of template parsing classes that search through a PowerPoint presentation replacing tags from a mark-up language I invented with charts and dynamic text objects fetched from a database. The trickiest part of the string replacement bit was handling tags that occurred across
Run
elements inside aParagraph
element. This occurs usually if you use special characters such as '{' or spaces in your tags. I was able to solve it by storing the text of the entireTextBody
element in a gigantic character array (in your case it would be the contents of aCell
element), storing a list of extents that enumerated where in the character array eachRun
element began and ended, and then walking the character array while paying attention to theRun
boundaries appropriately. Mind that if your tag spans across multipleRun
elements you'd need to remove any extras and snip content across boundaries before you inserted the replacementRun
. Unfortunately I cannot post any code because the work was done for a company, but that's the general idea of how to achieve it. I was not able to handle any newline cases (i.e. a tag occurs with a newline in it) because that would require writing a crossParagraph
indexer, which was beyond the scope of what I wanted to achieve. That could be done as well, but it would be significantly more difficult I think.Powershell 脚本扩展 PSExcel 具有搜索和替换功能:
PSExcel:https://github.com/RamblingCookieMonster/PSExcel
The Powershell script extension PSExcel have an search and replace feature:
PSExcel: https://github.com/RamblingCookieMonster/PSExcel