0

I am using OpenXml for creating an excel report through c#.I need set all the column width as 20 . I tried using

SheetFormatProperties properties = new SheetFormatProperties() { DefaultColumnWidth = 20 };
worksheet.AppendChild(properties);

but not working.. How to do it?

Roby A
  • 38
  • 8
  • Does this answer your question? [Creating custom column widths in OpenXML (excel)](https://stackoverflow.com/questions/34374785/creating-custom-column-widths-in-openxml-excel) – VietDD Oct 15 '20 at 07:00
  • nope.. I need all the column widths to be same – Roby A Oct 15 '20 at 07:01
  • @RobyA you still have to work with the columns. The column width is a property of the columns, not the sheet. Higher-level libraries like eg EPPlus can autosize columns but underneath, they still modify the columns themselves – Panagiotis Kanavos Oct 15 '20 at 07:03
  • BTW using EPPlus or ClosedXML is a *lot* easier than working with the raw OpenXML SDK – Panagiotis Kanavos Oct 15 '20 at 07:04
  • @PanagiotisKanavos Could you please elaborate? – Roby A Oct 15 '20 at 07:04
  • @RobyA you need to do what the duplicate shows. Either create the columns with the widths you want in advance, or iterate over the columns you want and set their widths – Panagiotis Kanavos Oct 15 '20 at 07:06
  • What do I need to do for getting all columns with width 20? – Roby A Oct 15 '20 at 07:06
  • @PanagiotisKanavos could you please check this https://stackoverflow.com/questions/64334832/coloumn-width-not-reflecting-in-excel-sheet .. my question related to the same topic.. has some extended codes also.. Thanks – Roby A Oct 15 '20 at 07:12
  • 1
    Why don't you use a library instead? With EPPlus you'd only need two lines: `var newRange=sheet.Cells.LoadFromCollection(someList); newRange.AutoFitColumns();`. You can also load from a DataTable or DataReader – Panagiotis Kanavos Oct 15 '20 at 07:25
  • 1
    The [equivalent in ClosedXml](https://github.com/ClosedXML/ClosedXML/wiki/Adjust-Row-Height-and-Column-Width-to-Contents) is `AdjustToContents()`. You can also [add a DataTable or DataSet as a sheet with a single command](https://github.com/ClosedXML/ClosedXML/wiki/Adding-DataTable-as-Worksheet) with `wb.Worksheets.Add(dataSet);` – Panagiotis Kanavos Oct 15 '20 at 07:30

0 Answers0