C# 如何使用 ClosedXml 在公式单元格中制作 Excel 换行文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15198445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make Excel wrap text in formula cell with ClosedXml
提问by horgh
The problem is that the cell content is not wrapped, when that cell contains a formula referring to a cell with some long string.
问题是单元格内容没有换行,当该单元格包含一个公式时,该公式引用了一个带有一些长字符串的单元格。
On CodePlexI found a thread on this issue and a simple code to see the problem:
在CodePlex 上,我找到了一个关于这个问题的线程和一个简单的代码来查看问题:
var generated = new XLWorkbook();
var workSheet = generated.AddWorksheet("Test");
workSheet.Cell("B1").Style.Alignment.WrapText = true;
workSheet.Cell("B1").Value = "hello hello hello hello hello";
workSheet.Cell("A3").FormulaA1 = "B1";
workSheet.Cell("A3").Style.Alignment.WrapText = true;
generated.SaveAs("Generated.xlsx");
I also tried to set row height manually instead of wrapping the cell:
我还尝试手动设置行高而不是包装单元格:
workSheet.Row(3).Height = workSheet.Row(1).Height;
However to no success either.
然而也没有成功。
Is there anything I can do about this?
我能做些什么吗?
Following the comment by Peter Albert, I tried to make the set row's AutoFit
. The only thing I managed to find to do this in ClosedXML is workSheet.Row(3).AdjustToContent();
. But this did not work either (neither adjusting the content of certain column).
按照 Peter Albert 的评论,我尝试将设置行的AutoFit
. 我设法在 ClosedXML 中找到的唯一方法是workSheet.Row(3).AdjustToContent();
. 但这也不起作用(既不调整某些列的内容)。
回答by bubi
Sorry, I can't still write comments... AutoFit is not a property of ClosedXML. About AdjustToContents, in my version (26/07/2014, I think 0.72.3) ignores WordWrap property (that split long lines). This is the main check
抱歉,我仍然不能写评论... AutoFit 不是 ClosedXML 的属性。关于 AdjustToContents,在我的版本 (26/07/2014,我认为 0.72.3) 中忽略 WordWrap 属性(拆分长行)。这是主要检查
if (c.HasRichText || textRotation != 0 || c.InnerText.Contains(Environment.NewLine))
{
// omissis...
}
else
thisHeight = c.Style.Font.GetHeight( fontCache);
This implementation ignores the exact height in case a cell is more than one line because of autowrap. So, AdjustToContents + AutoWrap does not work. If you need to have the height of the size of the content you need to avoid to call AdjustToContents. This behaviour is not compatible with XL IsAutoHeight property.
如果单元格由于自动换行而超过一行,则此实现会忽略确切的高度。因此,AdjustToContents + AutoWrap 不起作用。如果您需要具有内容大小的高度,则需要避免调用 AdjustToContents。此行为与 XL IsAutoHeight 属性不兼容。
回答by Moayad Myro
I use this
我用这个
xlWorkSheet.Range["A4:A4"].Cells.WrapText = true;
回答by Smit Patel
Instead of Applying the Adjust to Contents, you can apply the Wraptext like this
您可以像这样应用 Wraptext,而不是将调整应用于内容
var generated = new XLWorkbook();
var workSheet = generated.AddWorksheet("Test");
worksheet.Cell(3, 2).Value = "Hello Hello Hello Hello Hello Hello Name";
worksheet.Cell(3, 2).Style.Alignment.WrapText = true;
And if you want to apply both use it after AdjustToContents.
如果您想同时应用两者,请在 AdjustToContents 之后使用它。
var generated = new XLWorkbook();
var workSheet = generated.AddWorksheet("Test");
worksheet.Columns(2, 20).AdjustToContents();
worksheet.Cell(3, 2).Value = "Hello Hello Hello Hello Hello Hello Name";
worksheet.Cell(3, 2).Style.Alignment.WrapText = true;
回答by Francois Botha
Note also that on that very same Codeplex page, the author of the library states:
另请注意,在同一个 Codeplex 页面上,库的作者声明:
This one took a while to figure out.
Excel is actually cheating when you set the wrap text on a cell that points to another. It calculates the required height and then sets row height property. This is something I can't do here.
You'll have to do without.
这个花了一段时间才弄明白。
当您在指向另一个单元格的单元格上设置换行文本时,Excel 实际上是在作弊。它计算所需的高度,然后设置行高属性。这是我在这里做不到的。
你将不得不这样做。
To me this implies that this feature is not possible.
对我来说,这意味着此功能是不可能的。
回答by Ali Nouman
The SetWrapText();
worked for me
在SetWrapText();
为我工作