Reputation:
I have an XLWorkbook but the cell widths are to small. I want to give a cell width by default but don't know how. This is the code where i think the styles need to be applied.
var workbookBytes = new byte[0];
using (XLWorkbook wb = new XLWorkbook())
{
for (int i = 0; i < ds.Tables.Count; i++)
{
wb.Worksheets.Add(ds.Tables[i], ds.Tables[i].TableName);
}
wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
wb.Style.Font.Bold = true;
using (var ms = new MemoryStream())
{
wb.SaveAs(ms);
workbookBytes = ms.ToArray();
}
}
Thanks in advance, your help is really apprecieated
Upvotes: 0
Views: 3763
Reputation: 66
Is it ClosedXML.Excel library? Also, can't catch the moment where you are trying to set a cell width in your code sample. May be it is needed to iterate the worksheets and you can select columns there. For example:
IXLColumn col = worksheet.Column("A");
col.Width = 10;
Assuming this is ClosedXML.Excel, I couldn't find 'Width' property for cell in that documentation IXLCell documentation
UPD: After your comment I tried this:
using (XLWorkbook wb = new XLWorkbook())
{
for (int i = 0; i < ds.Tables.Count; i++)
{
wb.Worksheets.Add(ds.Tables[i], ds.Tables[i].TableName);
}
wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
wb.Style.Font.Bold = true;
foreach (var ws in wb.Worksheets)
{
ws.ColumnWidth = 50;
}
using (var ms = new MemoryStream())
{
wb.SaveAs(ms);
workbookBytes = ms.ToArray();
}
}
And it worked, if no further actions with columns called.
Upvotes: 2