Multiple Formats in one cell using c#

后端 未结 3 809
既然无缘
既然无缘 2021-01-14 01:53

I want to have mutple format types in one cell in my workbook. For example I want my A1 cell to display \" Name: Aaron Kruger \". When I programmatically

相关标签:
3条回答
  • 2021-01-14 02:08

    I recorded a macro, it shouldn't be hard to translate it to C#:

    ActiveCell.FormulaR1C1 = "Test test"
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "Test test"
    With ActiveCell.Characters(Start:=1, Length:=5).Font
        .Name = "Calibri"
        .FontStyle = "Regular"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
    With ActiveCell.Characters(Start:=6, Length:=4).Font
        .Name = "Calibri"
        .FontStyle = "Bold"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
    
    0 讨论(0)
  • 2021-01-14 02:15

    Use Microsoft.Office.Interop.Excel.Range which gives you each character as Characters type. Now use its Font and other properties to style them.

    Refer an example here: http://www.bloodforge.com/post/Extract-Formatted-Text-From-Excel-Cell-With-C.aspx

     Microsoft.Office.Interop.Excel.Range Range = (Microsoft.Office.Interop.Excel.Range)Cell;
       int TextLength = Range.Text.ToString().Length;
       for (int CharCount = 1; CharCount <= TextLength; CharCount++)
       {
           Microsoft.Office.Interop.Excel.Characters charToTest = Range.get_Characters(CharCount, 1);
           bool IsBold = (bool)charToTest.Font.Bold;
           bool IsItalic = (bool)charToTest.Font.Italic;
           // other formatting tests here
    
       }
    
    0 讨论(0)
  • 2021-01-14 02:26
    Range rng1 = ws.getRange("A1","E10");
    for(int i=0;i<10;i++)
    {
        Range rngTst=rng.cells[i,i];
        for(int j=0;j<rngTst.get_characters().count;j++)
        {
            rngTst.application.activecell.get_characters(j,j).font.color
        }
    }
    

    or

    int sFirstFoundAddress = currentFind.FormulaR1C1Local.ToString().IndexOf("NOT FOR CIRCULATION ");
    
    get_Range(excel.Cells[1, 1],
        excel.Cells[1, dtData.Columns.Count])
            .get_Characters(sFirstFoundAddress, 20).Font.Color =
                System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
    
    0 讨论(0)
提交回复
热议问题