private Dictionary <short, List <string> > AnalysisCellString(XSSFCell cell) { Dictionary <short, List <string> > _cellList = new Dictionary <short, List <string> >(); XSSFRichTextString rich = (XSSFRichTextString)cell.RichStringCellValue; int formattingRuns = cell.RichStringCellValue.NumFormattingRuns; for (int i = 0; i < formattingRuns; i++) { int startIdx = rich.GetIndexOfFormattingRun(i); int length = rich.GetLengthOfFormattingRun(i); string st = rich.String.Substring(startIdx, length); IFont font; if (i == 0) { short fontIndex = cell.CellStyle.FontIndex; font = _workBook.GetFontAt(fontIndex); } else { font = rich.GetFontOfFormattingRun(i); } if (_cellList.Keys.Where(t => t == font.Color).Count() == 0) { _cellList.Add(font.Color, new List <string>() { st }); } else { _cellList[font.Color].Add(st); } } return(_cellList); }
public void TestEmpty() { XSSFRichTextString rt = new XSSFRichTextString(); Assert.AreEqual(0, rt.GetIndexOfFormattingRun(9999)); Assert.AreEqual(-1, rt.GetLengthOfFormattingRun(9999)); Assert.IsNull(rt.GetFontAtIndex(9999)); }
public void TestApplyFont() { XSSFRichTextString rt = new XSSFRichTextString(); rt.Append("123"); rt.Append("4567"); rt.Append("89"); Assert.AreEqual("123456789", rt.String); Assert.AreEqual(false, rt.HasFormatting()); XSSFFont font1 = new XSSFFont(); font1.IsBold = (true); rt.ApplyFont(2, 5, font1); Assert.AreEqual(true, rt.HasFormatting()); Assert.AreEqual(4, rt.NumFormattingRuns); Assert.AreEqual(0, rt.GetIndexOfFormattingRun(0)); Assert.AreEqual("12", rt.GetCTRst().GetRArray(0).t); Assert.AreEqual(2, rt.GetIndexOfFormattingRun(1)); Assert.AreEqual("345", rt.GetCTRst().GetRArray(1).t); Assert.AreEqual(5, rt.GetIndexOfFormattingRun(2)); Assert.AreEqual(2, rt.GetLengthOfFormattingRun(2)); Assert.AreEqual("67", rt.GetCTRst().GetRArray(2).t); Assert.AreEqual(7, rt.GetIndexOfFormattingRun(3)); Assert.AreEqual(2, rt.GetLengthOfFormattingRun(3)); Assert.AreEqual("89", rt.GetCTRst().GetRArray(3).t); Assert.AreEqual(-1, rt.GetIndexOfFormattingRun(9999)); Assert.AreEqual(-1, rt.GetLengthOfFormattingRun(9999)); Assert.IsNull(rt.GetFontAtIndex(9999)); }
private void AnalysisCellString(XSSFCell cell) { XSSFRichTextString rich = (XSSFRichTextString)cell.RichStringCellValue; string st2 = rich.String; int formattingRuns = cell.RichStringCellValue.NumFormattingRuns; if (formattingRuns > 0) { st2 = string.Empty; } XSSFFont font = (XSSFFont)_workBook.CreateFont(); for (int i = 0; i < formattingRuns; i++) { int startIdx = rich.GetIndexOfFormattingRun(i); int length = rich.GetLengthOfFormattingRun(i); string st = rich.String.Substring(startIdx, length); if (i == 0) { short fontIndex = cell.CellStyle.FontIndex; font = (XSSFFont)_workBook.GetFontAt(fontIndex); } else { font = (XSSFFont)rich.GetFontOfFormattingRun(i); } if (font.Color != IndexedColors.White.Index && font.Color != 0) { st2 += st; } } font.Color = IndexedColors.Black.Index; XSSFRichTextString rich2 = new XSSFRichTextString(); rich2.Append(st2, font); cell.SetCellValue(rich2); }
public void TestBug56511_values() { XSSFWorkbook wb = XSSFTestDataSamples.OpenSampleWorkbook("56511.xlsx"); ISheet sheet = wb.GetSheetAt(0); IRow row = sheet.GetRow(0); // verify the values to ensure future Changes keep the returned information equal XSSFRichTextString rt = (XSSFRichTextString)row.GetCell(0).RichStringCellValue; Assert.AreEqual(0, rt.NumFormattingRuns); Assert.IsNull(rt.GetFontOfFormattingRun(0)); Assert.AreEqual(-1, rt.GetLengthOfFormattingRun(0)); rt = (XSSFRichTextString)row.GetCell(1).RichStringCellValue; Assert.AreEqual(0, row.GetCell(1).RichStringCellValue.NumFormattingRuns); Assert.IsNull(rt.GetFontOfFormattingRun(1)); Assert.AreEqual(-1, rt.GetLengthOfFormattingRun(1)); rt = (XSSFRichTextString)row.GetCell(2).RichStringCellValue; Assert.AreEqual(2, rt.NumFormattingRuns); Assert.IsNotNull(rt.GetFontOfFormattingRun(0)); Assert.AreEqual(4, rt.GetLengthOfFormattingRun(0)); Assert.IsNotNull(rt.GetFontOfFormattingRun(1)); Assert.AreEqual(9, rt.GetLengthOfFormattingRun(1)); Assert.IsNull(rt.GetFontOfFormattingRun(2)); rt = (XSSFRichTextString)row.GetCell(3).RichStringCellValue; Assert.AreEqual(3, rt.NumFormattingRuns); Assert.IsNull(rt.GetFontOfFormattingRun(0)); Assert.AreEqual(1, rt.GetLengthOfFormattingRun(0)); Assert.IsNotNull(rt.GetFontOfFormattingRun(1)); Assert.AreEqual(3, rt.GetLengthOfFormattingRun(1)); Assert.IsNotNull(rt.GetFontOfFormattingRun(2)); Assert.AreEqual(9, rt.GetLengthOfFormattingRun(2)); }