How to control Excel column size in Javascript?

2019-08-29 01:33发布

问题:

I have a function in Javascript that Exports data to a new Excel file. I'm using ActiveXObejct("Excel.Application") to work with Excel. After The Export of the data is done and I see the result, the data overflows the width of the columns. This is causing some of the data look cut in the table. For example if I have the data '1234567890' in a cell I would see '123456' or '######' in the cell. So how can I control the width of the columns to fit the data they contain? This is a code example of what I'm generally doing:

var Excel = new ActiveXObject("Excel.Application");
var Sheet = new ActiveXObject("Excel.Sheet");
for (var i = 0 ; i < DataArray.length ; i++)
    Sheet.ActiveSheet.Cells(i+1,1).value = DataArray[i];
Excel.visible = true;

回答1:

After the loop:

Sheet.Cells(,1).EntireColumn.ColumnWidth = 20

will change the width of column A.



回答2:

I've managed to find an answer.

Sheet.ActiveSheet.Cells('A:A').ColumnWidth=11;

This will change the width of the first cloumn.