在使用hssf可以直接按照行列插入。
HSSFWorkbook book = new HSSFWorkbook(); HSSFSheet sheet1 = (HSSFSheet)book.CreateSheet("Sheet1"); HSSFPatriarch patriarch = (HSSFPatriarch)sheet1.CreateDrawingPatriarch(); HSSFClientAnchor a1 = new HSSFClientAnchor(255, 125, 1023, 150, 0, 5, 2, 2); HSSFSimpleShape line1 = patriarch.CreateSimpleShape(a1); line1.ShapeType = HSSFSimpleShape.OBJECT_TYPE_LINE; line1.LineStyle = HSSFShape.LINESTYLE_SOLID; //在NPOI中线的宽度12700表示1pt,所以这里是0.5pt粗的线条。 line1.LineWidth = 6350; using (FileStream fileStream = new FileStream("sss.xlsx", FileMode.Create)) { book.Write(fileStream); }
但是呢,在使用XSSF得时候,使用同样方式得写法就不生效了,根据chatgpt给出的答案。
我也不知道这个答案是否准确,但是我接纳他的建议,画一条斜向下得线,然后垂直翻转过来。文章来源:https://www.toymoban.com/news/detail-640039.html
IWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet("Sheet1");
XSSFDrawing drawing = (XSSFDrawing)sheet.CreateDrawingPatriarch();
XSSFClientAnchor anchor = (XSSFClientAnchor)drawing.CreateAnchor(0, 0, 0, 0, 0, 3, 4, 7);
XSSFSimpleShape line1 = drawing.CreateSimpleShape(anchor);
line1.ShapeType = 1;
line1.SetFillColor(255, 255, 0);
line1.LineStyle = NPOI.SS.UserModel.LineStyle.None;
line1.LineWidth = 1;
line1.SetLineStyleColor(255, 255, 0);
// line1.GetCTShape().spPr.xfrm.flipH = true;
line1.GetCTShape().spPr.xfrm.flipV = true;
// line1.GetCTShape().spPr.xfrm.rot = 45;
// XSSFRichTextString richText = new XSSFRichTextString("Rotated Shape");
// richText.ApplyFont(0, richText.Length, new XSSFFont() { IsItalic = true });
XSSFClientAnchor anchor2 = (XSSFClientAnchor)drawing.CreateAnchor(0, 0, 0, 0, 0, 3, 4, 7);
XSSFSimpleShape line12 = drawing.CreateSimpleShape(anchor2);
line12.ShapeType = 1;
line12.SetFillColor(255, 255, 0);
line12.LineStyle = NPOI.SS.UserModel.LineStyle.None;
line12.LineWidth = 1;
line12.SetLineStyleColor(255, 255, 0);
// line1.GetCTShape().spPr.xfrm.flipH = true;
// line12.GetCTShape().spPr.xfrm.flipV = true;
// 将富文本字符串应用到形状
//line1.Text = richText;
using (FileStream fileStream = new FileStream("dcf.xlsx", FileMode.Create))
{
workbook.Write(fileStream);
}
Console.WriteLine("Excel file created.");
文章来源地址https://www.toymoban.com/news/detail-640039.html
到了这里,关于关于npoi插入一条斜向上得线记录一下的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!