How to control Excel column size in Javascript?

2019-08-29 00:53发布

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;

2条回答
地球回转人心会变
2楼-- · 2019-08-29 01:30

After the loop:

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

will change the width of column A.

查看更多
聊天终结者
3楼-- · 2019-08-29 01:32

I've managed to find an answer.

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

This will change the width of the first cloumn.

查看更多
登录 后发表回答