How to set this Code in C#

Posted by Jayakumars under ADO.NET on 1/7/2017 | Points: 10 | Views : 150 | Status : [Member] [MVP] | Replies : 2
Hi

How to set this Excel Code

this is my code below . But I need how to set 1.Autofit,2. Borders in all data
using below code in C# ? any one guide me.

 var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add()


Mark as Answer if its helpful to you


Responses

Posted by: A2H on: 1/7/2017 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hello Jayakumar,

I guess you are using ClosedXML control, if that is the cause then you can use the AdjustToContents method for Autofitand Style property for border in excel file like below

Sample Code
 var workbook = new ClosedXML.Excel.XLWorkbook();
var worksheet = workbook.Worksheets.Add(table, "tab1");

//Setting autofit to each columns used in excel
worksheet.Rows().AdjustToContents();
worksheet.Columns().AdjustToContents();

//Setting borders to each used cell in excel
worksheet.CellsUsed().Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin;
worksheet.CellsUsed().Style.Border.BottomBorderColor = ClosedXML.Excel.XLColor.Red;

worksheet.CellsUsed().Style.Border.TopBorder = ClosedXML.Excel.XLBorderStyleValues.Thin;
worksheet.CellsUsed().Style.Border.TopBorderColor = ClosedXML.Excel.XLColor.Red;

worksheet.CellsUsed().Style.Border.LeftBorder = ClosedXML.Excel.XLBorderStyleValues.Thin;
worksheet.CellsUsed().Style.Border.LeftBorderColor = ClosedXML.Excel.XLColor.Red;

worksheet.CellsUsed().Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Thin;
worksheet.CellsUsed().Style.Border.RightBorderColor = ClosedXML.Excel.XLColor.Red;



Thanks,
A2H
My Blog

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response