</pre><pre name="code" class="javascript">function ExproterAll() {
var rows = $('#dg').datagrid('getRows');
//var rows=$('#dg').datagrid("getChecked");
var columns = $("#dg").datagrid("options").columns[0];
var oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel
var oWB = oXL.Workbooks.Add(); //获取workbook对象
var oSheet = oWB.ActiveSheet; //激活当前sheet
oSheet.name = "导出Excel报表";
var addNum=0;
for (var i = 0; i < columns.length-1; i++) {
oSheet.Cells(1, i+1).value = columns[i+1].title;
oSheet.Cells(1, i+1).Borders.Weight = 2;
oSheet.Cells(1, i+1).Font.Size = 12;
oSheet.Cells(1, i+1).Font.Name = "黑体";
oSheet.Cells(1, i+1).WrapText=true;
oSheet.Cells(1, i+1).rowHeight=30;
oSheet.Cells(1, i+1).HorizontalAlignment=3;
oSheet.Cells(1, i+1).VerticalAlignment=2;
}
oSheet.Cells(1,1).columnWidth=10;
oSheet.Cells(1,2).columnWidth=20;
oSheet.Cells(1,3).columnWidth=20;
oSheet.Cells(1,4).columnWidth=50;
oSheet.Cells(1,5).columnWidth=50;
for (var i = 0; i < rows.length; i++) {
for (var j = 0; j < columns.length-1; j++) {
oSheet.Cells(i + 2, j+1).value = rows[i][columns[j+1].field];
oSheet.Cells(i + 2, j+1).Borders.Weight = 2;
oSheet.Cells(i + 2, j+1).Font.Size = 10;
oSheet.Cells(i + 2, j+1).Font.Name = "宋体";
oSheet.Cells(i + 2, j+1).WrapText=true;
oSheet.Cells(i + 2, j+1).HorizontalAlignment=1;
oSheet.Cells(i + 2, j+1).VerticalAlignment=1;
}
}
oXL.Visible = true; //设置excel可见属性
}
更多excel样式参考
http://wallimn.iteye.com/blog/1674658