Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>by default Excel stores strings in the global (1 per workbook) <code>SharedStringTablePart</code>. So, this is the one you need to target. However, the OpenXML format also allows inline text inside the WorksheetParts. hence, a complete solution needs to look there as well.</p> <p>Here's a sample app (with some inline comments):</p> <pre><code>using DocumentFormat.OpenXml.Packaging; using x = DocumentFormat.OpenXml.Spreadsheet; class Program { private static readonly string placeHolder = "$$value"; static void Main() { var templatePath = @"C:\Temp\template.xlsx"; var resultPath = @"C:\Temp\result.xlsx"; string replacementText = "test"; using (Stream xlsxStream = new MemoryStream()) { // Read template from disk using (var fileStream = File.OpenRead(templatePath)) fileStream.CopyTo(xlsxStream); // Do replacements ProcessTemplate(xlsxStream, replacementText); // Reset stream to beginning xlsxStream.Seek(0L, SeekOrigin.Begin); // Write results back to disk using (var resultFile = File.Create(resultPath)) xlsxStream.CopyTo(resultFile); } } private static void ProcessTemplate(Stream template, string replacementText) { using (var workbook = SpreadsheetDocument.Open(template, true, new OpenSettings { AutoSave = true })) { // Replace shared strings SharedStringTablePart sharedStringsPart = workbook.WorkbookPart.SharedStringTablePart; IEnumerable&lt;x.Text&gt; sharedStringTextElements = sharedStringsPart.SharedStringTable.Descendants&lt;x.Text&gt;(); DoReplace(sharedStringTextElements, replacementText); // Replace inline strings IEnumerable&lt;WorksheetPart&gt; worksheetParts = workbook.GetPartsOfType&lt;WorksheetPart&gt;(); foreach (var worksheet in worksheetParts) { var allTextElements = worksheet.Worksheet.Descendants&lt;x.Text&gt;(); DoReplace(allTextElements, replacementText); } } // AutoSave enabled } private static void DoReplace(IEnumerable&lt;x.Text&gt; textElements, string replacementText) { foreach (var text in textElements) { if (text.Text.Contains(placeHolder)) text.Text = text.Text.Replace(placeHolder, replacementText); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload