I have a excel template, where we have different predefined fields like CustomerName, CompanyName, Address etc. All this things are in single cell and this cell is named as “Customer” and similarly we have another cell called “Sender” having fields related to sender details. My job is to find Customer and Sender cells and replace the values that they contain with the actual values, For example i need to find “Customer” Cell and will replace CustomerName with actual name “John”, ComanyName with “XYZ” and so on for other fields, same task is for Sender Cell.
I am using Microsoft.Office.Interop.Excel to implement this task as follows.
xlWorkSheet.Cells.Replace(“CustomerName”, “John”, Excel.XlLookAt.xlPart, missingValue, missingValue, missingValue, missingValue, missingValue);
xlWorkSheet.Cells.Replace(“CompanyName”, “XYZ”, Excel.XlLookAt.xlPart, missingValue, missingValue, missingValue, missingValue, missingValue);
The problem is all this fields in a single cell, and i need to find different fields and replace their values, if a fields values is not then shifting the remaining fields up to fill the empty space.
Please provide solution.
I would pull the data into a string, do the string manipulation and then set it back to the cell value…
You can make it even more efficient by not repeating the StrToReplace.replace on multiple lines such as:
Or, perhaps using a stringbuilder, but this should at least point you in the right direction to solve the current problem.
Hope this helps