Why is there no attempt to automatically adjust the width of a sheet or a single column on this sheet?

I have one column whose contents are truncated, and I would like you to not have to double-click the right column of the column to see the data. First I tried the auto-preparation of the whole shebang:

private void PopulatePivotTableDataSheet()
{
    if (null == _produceUsagePivotDataList) return;
    AddColumnHeadingRowPivotData();
    foreach (ProduceUsagePivotData pupd in _produceUsagePivotDataList)                
    {
        AddPivotData(pupd.ItemCode, pupd.ItemDescription, pupd.Unit, pupd.MonthYear, pupd.Quantity,
            pupd.TotalPrice, pupd.IsContractItem);
    }
    _xlPivotDataSheet.Cells.AutoFit();
}

... but the last line fails with "Range Class AutoFit Method Error"

Then I tried to apply it to the column in question like this:

private void AddPivotData(String ItemCode, String ItemDescription, String Unit, String MonthYear, int Quantity, Decimal TotalPrice, Boolean IsContractItem)
{
    var itemCodeCell = _xlPivotDataSheet.Cells[_lastRowAddedPivotTableData + 1, 1];
    itemCodeCell.Value2 = ItemCode;

    var itemDescriptionCell = _xlPivotDataSheet.Cells[_lastRowAddedPivotTableData + 1, 2];
    itemDescriptionCell.Value2 = ItemDescription;
    itemDescriptionCell.AutoFitWidth(); 
    . . .

... and this last line fails, "System .__ ComObject" does not contain a definition for "AutoFitWidth" "

What is going on here in Sam Hill o 'beans? Auto fitting should be easy to implement, right?

+4
1

AutoFit , :

_xlPivotDataSheet.Cells.EntireColumn.AutoFit();
+2

Source: https://habr.com/ru/post/1658355/


All Articles