代码之家  ›  专栏  ›  技术社区  ›  Bokambo

在Excel-OpenXml中更改CellValue中文本的字体颜色

  •  0
  • Bokambo  · 技术社区  · 6 年前

    我使用下面的代码来突出显示单元格文本,它可以为CellValue做什么?

    foreach (DocumentFormat.OpenXml.Spreadsheet.Cell currentCell in allCells)
    {    
    Fill fill = new Fill()
        {
             PatternFill = new PatternFill
             {
                 PatternType = PatternValues.Solid,
                 ForegroundColor = new ForegroundColor() { Rgb = "FFFF00" }
              }
        };
        styleSheet.Fills.AppendChild(fill);
    
       //Adding the  CellFormat which uses the Fill element 
        CellFormats cellFormats = styleSheet.CellFormats;
        CellFormat cf = new CellFormat();
        cf.FillId = styleSheet.Fills.Count;
        cellFormats.AppendChild(cf);
        currentCell.StyleIndex = styleSheet.CellFormats.Count;
    }
    

    我在CellValue中看不到任何风格属性

    CellValue currentCellValue = currentCell.GetFirstChild<CellValue>();
    if (currentCell.DataType == CellValues.SharedString) // cell has a cell value that is a string, thus, stored else where
        {
                 data = doc.WorkbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault().SharedStringTable.ElementAt(int.Parse(currentCellValue.Text)).InnerText;
        }
    

    SharedStringTable sharedStringTable1 = new SharedStringTable(){ Count = (UInt32Value)1U, UniqueCount = (UInt32Value)1U };
    
                SharedStringItem sharedStringItem1 = new SharedStringItem();
    
                Run run1 = new Run();
    
                RunProperties runProperties1 = new RunProperties();
                FontSize fontSize3 = new FontSize(){ Val = 11D };
                Color color3 = new Color(){ Rgb = "FFFF0000" };
                RunFont runFont1 = new RunFont(){ Val = "Calibri" };
                FontFamily fontFamily1 = new FontFamily(){ Val = 2 };
                FontScheme fontScheme4 = new FontScheme(){ Val = FontSchemeValues.Minor };
    
                runProperties1.Append(fontSize3);
                runProperties1.Append(color3);
                runProperties1.Append(runFont1);
                runProperties1.Append(fontFamily1);
                runProperties1.Append(fontScheme4);
                Text text1 = new Text();
                text1.Text = "Microsoft";
    
                run1.Append(runProperties1);
                run1.Append(text1);
    
                Run run2 = new Run();
    
                RunProperties runProperties2 = new RunProperties();
                FontSize fontSize4 = new FontSize(){ Val = 11D };
                Color color4 = new Color(){ Theme = (UInt32Value)1U };
                RunFont runFont2 = new RunFont(){ Val = "Calibri" };
                FontFamily fontFamily2 = new FontFamily(){ Val = 2 };
                FontScheme fontScheme5 = new FontScheme(){ Val = FontSchemeValues.Minor };
    
                runProperties2.Append(fontSize4);
                runProperties2.Append(color4);
                runProperties2.Append(runFont2);
                runProperties2.Append(fontFamily2);
                runProperties2.Append(fontScheme5);
                Text text2 = new Text(){ Space = SpaceProcessingModeValues.Preserve };
                text2.Text = " is great";
    
                run2.Append(runProperties2);
                run2.Append(text2);
    
                sharedStringItem1.Append(run1);
                sharedStringItem1.Append(run2);
    
                sharedStringTable1.Append(sharedStringItem1);
    
                sharedStringTablePart1.SharedStringTable = sharedStringTable1;
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   pfx    6 年前

    SharedStringItem
    这样的 共享字符串项 可以 包含 Run 元素。
    元素。

    共享字符串项 不包含任何 当单元格只包含文本,没有任何格式化的子元素时,就是这种情况。
    在这里,您必须创建一个新梯段才能应用样式。

    下面的代码使用Excel文件将单词RED的颜色设置为第一行单元格的红色,如下图所示。
    A1 包含 元素,单元格 B1

    input

    最终结果是

    result

    String pathToYourExcelFile = @"C:\Folder\ExcelFile.xlsx";
    using (SpreadsheetDocument document = SpreadsheetDocument.Open(pathToYourExcelFile, true))
    {
        WorkbookPart workbook =  document.WorkbookPart;                
        WorksheetPart firstWorksheet = document.WorkbookPart.WorksheetParts.FirstOrDefault();
        SharedStringTablePart stringTable = workbook.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();                              
    
        IEnumerable<Row> rows = firstWorksheet.Worksheet.GetFirstChild<SheetData>().Elements<Row>();
        Row firstRow = rows.FirstOrDefault();
    
        foreach (Cell cell in firstRow.Elements<Cell>())
        {                    
            foreach (CellValue cellValue in cell.Elements<CellValue>())
            {   
                IEnumerable<SharedStringItem> sharedStrings = 
                    stringTable.SharedStringTable.Elements<SharedStringItem>()
                        .Where((o, i) => i == Convert.ToInt32(cellValue.InnerText));
    
                foreach (SharedStringItem sharedString in sharedStrings)
                { 
                    IEnumerable<Run> runs = sharedString.Elements<Run>();
                    if (runs.Count() > 0)
                    {                                
                        foreach (Run run in runs)
                        {
                            if (run.InnerText == "RED")
                            {
                                RunProperties properties = run.RunProperties ?? new RunProperties();
                                Color color = properties.Elements<Color>().FirstOrDefault();
                                if (color != null)
                                {
                                    properties.RemoveChild<Color>(color);
                                }
    
                                properties.Append(new Color { Rgb = "FFFF0000" }) ;
                            }
                        }
                    }
                    else
                    {       
                        // No Runs, only text; create a Run.                                                     
                        Text text = new Text(sharedString.InnerText);                                
                        sharedString.RemoveAllChildren();
                        Run run = new Run();
                        run.Append(text);
                        run.RunProperties = new RunProperties();
                        run.RunProperties.Append(new Color { Rgb = "FFFF0000" }) ;
                        sharedString.Append(run);
                    }
                }
            }
        }
    
        document.Save();
    

    ( 我将把上面代码中的清理和异常处理留给您。。。 )


    针对您的具体情况,使用单元格值“Microsoft is great”, 你必须把这个字符串分成不同的部分,然后创建一个 只有在具有文本值“Microsoft”的部件上,才应用自定义字体颜色。


    // No Runs, only text.              
    
    const String MS = "Microsoft";
    String innerText = sharedString.InnerText;
    if (innerText.IndexOf(MS, StringComparison.OrdinalIgnoreCase) >= 0)
    { 
        sharedString.RemoveAllChildren();
    
        String[] parts = innerText.Split(' ');
        for (Int32 i = 0; i < parts.Length; i++)
        {
            String part = parts[i];
            Text text = new Text((i > 0 ? " " : String.Empty) + part);
            text.Space = SpaceProcessingModeValues.Preserve;         
    
            Run run = new Run();                                        
            run.Append(text);
    
            if (part.Equals(MS, StringComparison.OrdinalIgnoreCase))
            {
                run.RunProperties = new RunProperties();
                run.RunProperties.Append(new Color { Rgb = "FFFF0000" }) ;
            }
    
            sharedString.Append(run);                                        
        }
    

    下图显示了之前和之后。

    Before and after


    String pathToYourExcelFile = @"C:\Folder\ExcelFile.xlsx";
    using (SpreadsheetDocument document = SpreadsheetDocument.Open(pathToYourExcelFile, true))
    {
        WorkbookPart workbook =  document.WorkbookPart;
    
        // Loop over all worksheets.
        IEnumerable<WorksheetPart> worksheets = document.WorkbookPart.WorksheetParts;
        foreach (WorksheetPart worksheet in worksheets)
        {
            // Loop over all rows.
            IEnumerable<Row> rows = worksheet.Worksheet.GetFirstChild<SheetData>().Elements<Row>();   
            foreach (Row row in rows) 
            {
                // Loop over all cells.
                foreach (Cell cell in row.Elements<Cell>())
                {
                    // Loop over all cell values.
                    foreach (CellValue cellValue in cell.Elements<CellValue>())
                    {
                        // Apply content formatting as in code above ...
                    }
                }
            }
        }
    }