MiniExcel
                                
                                 MiniExcel copied to clipboard
                                
                                    MiniExcel copied to clipboard
                            
                            
                            
                        AutoFit for Column Width
Excel Type
- [X] XLSX
- [ ] XLSM
- [ ] CSV
- [ ] OTHER
Description
In other Projects like closedXML there is a function like AutoFit the ColumnWidth to the Content. In ClosedXML it is called AdjustToContents.
Do you have plans to implement it in MiniExcel? I would be glad to see this feature.
According to the documentation the width unit is a single character in the default font. So setting the column.Width = text.Length is a good place to start. If you want to support arbitrary fonts, it's going to be much more work.
So you need to loop through all the values in each column finding the Math.Max and then pass that one as the width directly.
@A9G-Data-Droid that's a very good point, but it's not satisfactory when using templated excel However in that case you can set the width of the column manually in the template to match the expected width, which might work for most use cases
@anteeek The people having this issue, like me, have widths that are dynamic. There is no "expected width" because it's different every time. It's an option we are asking for. You don't need to use the AutoFit option if you don't need it.
@A9G-Data-Droid yes I know, but in some use cases the variability of the width is low Now, I'm not a maintainer, but I think this feature might be surprisingly hard to implement since in similar libraries (including in other languages, for example Exceljs) I have yet to see it implemented, despite large demand for it
@anteeek your speculation adds nothing to the conversation. You can let the maintainers respond. The technique I suggested column.Width = text.Length is what I used and it's good enough for now. Like I said in my original post; the only complication is if you want to support arbitrary fonts.