Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NPOI:创建包含两个不同大小字符串的单元格_C#_Excel_Apache Poi_String Formatting_Npoi - Fatal编程技术网

C# NPOI:创建包含两个不同大小字符串的单元格

C# NPOI:创建包含两个不同大小字符串的单元格,c#,excel,apache-poi,string-formatting,npoi,C#,Excel,Apache Poi,String Formatting,Npoi,因此,正如标题所示,我想在NPOI 2.1.3工作簿中创建一个单元格,其中包含两个字符串:一个“正常”大小的字符串和一个“小”大小的字符串。=>我想更改部分单元格的字体大小 到目前为止,我的代码是: var planCell = planRow.CreateCell(lineCnt + 1); var planCellStyle = workbook.CreateCellStyle(); planCellStyle.WrapText = true; planCellStyle.Vertical

因此,正如标题所示,我想在NPOI 2.1.3工作簿中创建一个单元格,其中包含两个字符串:一个“正常”大小的字符串和一个“小”大小的字符串。=>我想更改部分单元格的字体大小

到目前为止,我的代码是:

var planCell = planRow.CreateCell(lineCnt + 1);

var planCellStyle = workbook.CreateCellStyle();
planCellStyle.WrapText = true;
planCellStyle.VerticalAlignment = VerticalAlignment.Top;
planCellStyle.Alignment = HorizontalAlignment.Left;
var font = workbook.CreateFont();
font.FontName = HSSFFont.FONT_ARIAL;
font.FontHeightInPoints = 16;
font.Boldweight = (short)FontBoldWeight.Bold;
planCellStyle.SetFont(font);
planCell.CellStyle = planCellStyle;

string planTitleContent = string.Empty;
... (some logic to get desired string for planTitleContent)

string planInfoContent = string.Empty;
... (some logic to get desired string for planInfoContent)

planCell.SetCellValue(planTitleContent + "\n\n"+planInfoContent);

准确地说,我希望“planInfoContent”部分以比“planCellContent”部分更小的字体显示。我搜索了很多,但我只找到了适用于整个单元格的CellStyle值。所以我希望我遗漏了一些东西,因为两个细胞不是一个真正的选择

我自己刚想出来:)

首先,创建2种所需格式的字体(对于我来说,为了简单起见,只有字体大小是相关的):

然后,在获得字符串后,使用(N)POIs-方法

其在NPOI中的一个实现具有以下签名:

applyFont(int-startIndex、int-endIndex、IFont-font)

因此,现在有了string
planTitleContent
和string
planInfoContent
,剩下的步骤非常明显:只需创建一个
IRichTextString
的实例,并通过构造函数参数将字符串添加到其中。然后,通过索引应用所需字体,如下所示:

IRichTextString formattedCellContent = new HSSFRichTextString(planTitleContent + "\n"+planInfoContent);
richString.ApplyFont(0, planTitleContent.Length, font1);
richString.ApplyFont(planTitleContent.Length + 1, (planTitleContent + "\n" + planInfoContent).Length, font2);

planCell.SetCellValue(formattedCellContent);
所以,这对我来说很有魅力。希望它能帮助其他人

IRichTextString formattedCellContent = new HSSFRichTextString(planTitleContent + "\n"+planInfoContent);
richString.ApplyFont(0, planTitleContent.Length, font1);
richString.ApplyFont(planTitleContent.Length + 1, (planTitleContent + "\n" + planInfoContent).Length, font2);

planCell.SetCellValue(formattedCellContent);