使用c#在一个单元格中使用多种格式

时间:2011-04-14 18:29:09

标签: c# excel

我想在工作簿中的一个单元格中使用mutple格式类型。例如,我希望我的A1单元格显示“名称: Aaron Kruger”。当我以编程方式将名称“Aaron Kruger”添加到模板时,它会自动将其设置为粗体。所以它看起来像“名字:Aaron Kruger ”。所以我想在同一个单元格中加粗和非粗体。也许在将来我会想要在同一个单元格中有两种不同的文本大小。 谢谢, 亚伦克鲁格

以下是我创建的函数:

    public void inputData(int row, int column, string cellName, System.Windows.Forms.TextBox textBox, Excel.Worksheet sheet)
    {
        sheet.Cells[row, column] = sheet.get_Range(cellName, Type.Missing).Text + " " + textBox.Text; // adds value to sheet
    }

以下是我传递的参数:

        inputData(5, 1, "A5", tbTagNumber, xlSheet);
        inputData(6, 1, "A6", tbCustomer, xlSheet);
        inputData(7, 1, "A5", tbDataFile, xlSheet);
        inputData(3, 6, "F3", tbJobNumber, xlSheet);
        inputData(4, 6, "F4", tbMeterSN, xlSheet);
        inputData(6, 6, "F6", tbPO, xlSheet);
        inputData(7, 6, "F7", tbFlowplate, xlSheet);
        inputData(4, 9, "I4", tbElectronicSN, xlSheet);

3 个答案:

答案 0 :(得分:3)

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
    }
}

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);

答案 1 :(得分:1)

使用Microsoft.Office.Interop.Excel.Range为每个字符提供Characters类型。现在使用其Font和其他属性来设置它们的样式。

请参阅此处的示例: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

   }

答案 2 :(得分:0)

我录制了一个宏,将它翻译成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