如何将左右边框样式设置为Excel单元格(Spire.xls库)

时间:2016-07-20 11:57:15

标签: c# excel xls spire.xls

我正在使用iceblue Spire.XLS dll使用C#创建和生成Excel文档

我想知道如何设置特定单元格或单元格范围的边框样式(只是左边或右边的样式!)。

在上面的代码中,我设法设置了字体和对齐方式 但我不知道如何设置左右边框样式:

Workbook workbook = new Workbook();
workbook.LoadFromStream(templateDocument);
Worksheet sheet = workbook.Worksheets["sheet_name"];

sheet.Range[row, 1, row, 4].Merge();
sheet.Range[row, 1, row, 4].Style.HorizontalAlignment = HorizontalAlignType.Center;
sheet.Range[row, 1, row, 4].Style.VerticalAlignment = VerticalAlignType.Center;
sheet.Range[row, 1, row, 4].Style.Font.Size = 14;
sheet.Range[row, 1, row, 4].Style.Font.Color =Color.White;
sheet.Range[row, 1, row, 4].Style.Interior.Color = Color.Gray;
// syntax to achive styling border ?? 
//sheet.Range[row, 1, row, 4].Borders. = ?

问候。

1 个答案:

答案 0 :(得分:4)

是的,你可以这样做:

适用于所有国家/地区:

一个单元格:

sheet.Range[row,col].Style.Borders.LineStyle = LineStyleType.Thin;
sheet.Range[row,col].Style.Borders.Color = Color.Black;

细胞范围:

sheet.Range[beginRow, beginCol, endRow, endCol].Style.Borders.LineStyle =LineStyleType.Thin;
sheet.Range[beginRow, beginCol, endRow, endCol].Style.Borders.Color = Color.Black;

对于特定边框边缘:(左上方顶部):

一个单元格

//right border  
sheet.Range[row,col].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
sheet.Range[row,col].Style.Borders[BordersLineType.EdgeRight].Color = Color.Black;    
//left border
sheet.Range[row,col].Style.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
sheet.Range[row,col].Style.Borders[BordersLineType.EdgeLeft].Color = Color.Black;

细胞范围:

//right border
 sheet.Range[beginRow, beginCol, endRow, endCol].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
sheet.Range[beginRow, beginCol, endRow, endCol].Style.Borders.Color = Color.Black;
//left border
sheet.Range[beginRow, beginCol, endRow, endCol].Style.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
sheet.Range[beginRow, beginCol, endRow, endCol].Style.Borders.Color = Color.Black;