用一个大胆的单词replace一个单词使用Excel Interop c#
我正在使用以下search我的整个Excel工作表,并用“要求”replace'请求':
oSheet.Cells.Replace("req", "requirement");
而不是取代req这个词,我想大胆。 我怎样才能做到这一点? 我知道这是错误的,但理论上我想要做到以下几点:
oSheet.Cells.Replace("req", "<b>req</b>");
谢谢。
我假设你想在单元格中设置单独的文本项目为粗体,这比第一次出现要复杂一些。 以下将做的伎俩:
public void FindTextAndSetToBold(string text) { Excel.Range currentFind = null; Excel.Range firstFind = null; // Find the first occurrence of the passed-in text currentFind = oSheet.Cells.Find(text, Missing.Value, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false, Missing.Value, Missing.Value); while (currentFind != null) { // Keep track of the first range we find if (firstFind == null) { firstFind = currentFind; } else if (currentFind.get_Address(Missing.Value, Missing.Value, Excel.XlReferenceStyle.xlA1, Missing.Value, Missing.Value) == firstFind.get_Address(Missing.Value, Missing.Value, Excel.XlReferenceStyle.xlA1, Missing.Value, Missing.Value)) { // We didn't move to a new range so we're done break; } // We know our text is in first cell of this range, so we need to narrow down its position string searchResult = currentFind.get_Range("A1").Value2.ToString(); int startPos = searchResult.IndexOf(text); // Set the text in the cell to bold currentFind.get_Range("A1").Characters[startPos + 1, text.Length].Font.Bold = true; // Move to the next find currentFind = oSheet.Cells.FindNext(currentFind); } }
部分从这里采取和修改。
这是做你想要的吗? (预先设定)
Application.ReplaceFormat.Font.FontStyle = "Bold"