其他分享
首页 > 其他分享> > Export 之 ClosedXML 评论(Comments)篇

Export 之 ClosedXML 评论(Comments)篇

作者:互联网

链接与安装

GitHub ClosedXML wiki

nuget

nuget 安装 :dotnet add package ClosedXML --version 0.95.4


visibility 可见性

var wb = new XLWorkbook();
var ws = wb.Worksheets.Add("Visibility");

// 默认Comment是隐藏的,当鼠标划过的时候显示
ws.Cell("A1").SetValue("I have a hidden comment").Comment.AddText("Hidden");

// 设置Comment可见
ws.Cell("A2").Comment.SetVisible().AddText("Visible");

// 使用SetZOrder设置Comment显示顺序,参数越大越靠前。不设置就按调用顺序依次增大
ws.Cell("A3").Comment.SetZOrder(5).SetVisible().AddText("On Top");

ws.Cell("A4").Comment.SetZOrder(4).SetVisible().AddText("Underneath");
// 设置Comment样式垂直靠底
ws.Cell("A4").Comment.Style.Alignment.SetVertical(XLDrawingVerticalAlignment.Bottom);

// 可以设置所有Comment可见
// foreach (var cell in ws.CellsUsed(XLCellsUsedOptions.Comments)) cell.Comment.SetVisible();

ws.Columns().AdjustToContents();

wb.SaveAs("CommentsVisibility.xlsx");


Position 位置

ws.Cell("A1").Comment.AddText("This is an unusual place for a comment...");
ws.Cell("A1").Comment.Position
    .SetColumn(3) // 从第3列开始
    .SetColumnOffset(5) //偏移
    .SetRow(5) // 从第5行开始
    .SetRowOffset(10); 


Signatures 签名

// 默认签名为系统登入名
ws.Cell("A1").Comment.AddSignature().AddText("Hello World!");

// 通过SetAuthor设置签名
ws.Cell("A2").Comment
    .SetAuthor("MDeLeon")
    .AddSignature()
    .AddText("Hello World!");

ws.Cell("A2").Comment.Position
    .SetColumn(3) // 从第3列开始
    .SetColumnOffset(5) //偏移
    .SetRow(5) // 从第5行开始
    .SetRowOffset(10);


style 之 文字方向

// 自动调整Comment大小以适应内容
ws.Cell("A1").Comment.Style.Alignment.SetAutomaticSize();
ws.Cell("A1").Comment.AddText("Things are pretty tight around here");

// 默认
ws.Cell("A3").Comment
  .AddText("Default Alignments:").AddNewLine()
  .AddText("Vertical = Top").AddNewLine()
  .AddText("Horizontal = Left").AddNewLine()
  .AddText("Orientation = Left to Right");

// 设置文字在右下角
ws.Cell("A8").Comment
  .AddText("Vertical = Bottom").AddNewLine()
  .AddText("Horizontal = Right");
ws.Cell("A8").Comment.Style
  .Alignment.SetVertical(XLDrawingVerticalAlignment.Bottom)
  .Alignment.SetHorizontal(XLDrawingHorizontalAlignment.Right);

// 大小随内容自适应  设置文字方向从下至上
ws.Cell("D3").Comment.AddText("Orientation = Bottom to Top");
ws.Cell("D3").Comment.Style
  .Alignment.SetOrientation(XLDrawingTextOrientation.BottomToTop)
  .Alignment.SetAutomaticSize();

// 大小随内容自适应  设置文字方向从上至下
ws.Cell("E3").Comment.AddText("Orientation = Top to Bottom");
ws.Cell("E3").Comment.Style
  .Alignment.SetOrientation(XLDrawingTextOrientation.TopToBottom)
  .Alignment.SetAutomaticSize();

// 大小随内容自适应  设置文字方向竖版
ws.Cell("F3").Comment.AddText("Orientation = Vertical");
ws.Cell("F3").Comment.Style
  .Alignment.SetOrientation(XLDrawingTextOrientation.Vertical)
  .Alignment.SetAutomaticSize();


style 之 颜色和线条 (感觉用不上啊)

ws.Cell("A2").Comment
  .AddText("Now ")
  .AddText("THIS").SetBold().SetFontColor(XLColor.Red)
  .AddText(" is colorful!");

ws.Cell("A2").Comment.Style
  .ColorsAndLines.SetFillColor(XLColor.RichCarmine)
  .ColorsAndLines.SetFillTransparency(0.25) // 25% opaque
  .ColorsAndLines.SetLineColor(XLColor.Blue)
  .ColorsAndLines.SetLineTransparency(0.75) // 75% opaque
  .ColorsAndLines.SetLineDash(XLDashStyle.LongDash)
  .ColorsAndLines.SetLineStyle(XLLineStyle.ThickBetweenThin)
  .ColorsAndLines.SetLineWeight(7.5);


style 之 size

// 自动大小
ws.Cell("A2").Comment.AddText("Things are very tight around here.");
ws.Cell("A2").Comment.Style.Size.SetAutomaticSize();

ws.Cell("A4").Comment.AddText("Different size");
ws.Cell("A4").Comment.Style
  .Size.SetHeight(30) 
  .Size.SetWidth(30); 


style 之 Margins

ws.Cell("A2").Comment
  .SetVisible()
  .AddText("Lorem ipsum dolor sit amet, adipiscing elit. ").AddNewLine()
  .AddText("Nunc elementum, sapien a ultrices, commodo nisl. ").AddNewLine()
  .AddText("Consequat erat lectus a nisi. Aliquam facilisis.");

ws.Cell("A2").Comment.Style
  .Margins.SetAll(0.25)
  .Size.SetAutomaticSize();


总结

所有api都很清晰明朗,通过名字便可以猜个八九不离十。这篇由于用的不多,所以很枯燥。
队了,还有几个关于设置评论属性的东西没写,这里就不介绍了,感兴趣的朋友可以看一看github。
拜了个拜!!!

标签:Comment,Style,AddText,Comments,Cell,ws,Export,ClosedXML,Alignment
来源: https://www.cnblogs.com/chilli-with-fish/p/15130317.html