Perform find & replace in excel doc in C#

梦想的初衷 提交于 2019-12-13 16:45:22

问题


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?


回答1:


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.




回答2:


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 a Paragraph 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 entire TextBody element in a gigantic character array (in your case it would be the contents of a Cell element), storing a list of extents that enumerated where in the character array each Run element began and ended, and then walking the character array while paying attention to the Run boundaries appropriately. Mind that if your tag spans across multiple Run elements you'd need to remove any extras and snip content across boundaries before you inserted the replacement Run. 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 cross Paragraph 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.




回答3:


The Powershell script extension PSExcel have an search and replace feature:

PSExcel: https://github.com/RamblingCookieMonster/PSExcel

Import-Module d:\psexcel\trunk\PSExcel\PSExcel

# Get commands in the module
Get-Command -Module PSExcel

# Get help for a command
Get-Help Import-XLSX -Full

 # Modify the path of the Excel file to your local svn ceck-out
 Get-ChildItem "C:\tmp\Source" -Filter *.xlsx | Foreach-Object{
    $fileName = $_.FullName

    # Open an existing XLSX to search and set cells within
    $Excel = New-Excel -Path $_.FullName

    $Workbook = $Excel | Get-Workbook

    #$Excel | Get-Worksheet
    $Worksheet = $Workbook | Get-Worksheet -Name "Sheet1"


    #Search for any cells like 'Chris'.  Set them all to Chris2
    $Worksheet | Search-CellValue {$_ -like 'Chris'} -As Passthru | Set-CellValue -Value "Chris2"

    #Save your changes and close the ExcelPackage
    $Excel | Save-Excel -Close
 }


来源:https://stackoverflow.com/questions/5792681/perform-find-replace-in-excel-doc-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!