导数据到EXCEL数字前面的0丢失问题
我用下面的函数把页面表单中的数据导到EXCEL中,第一列是“学号”的值,比如061274045,导到EXCEL后会变为61274045,即前面的0没了,请问要怎么样才能不让前面的0丢失?
function AutomatoExcel()
{
// Start Excel and get Application object.
var oXL = new ActiveXObject( "Excel.Application ");
// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var table = document.all.MainTable; //MainTable;为表格id
var hang = table.rows.length;
var lie = table.rows(0).cells.length;
// Add table headers going cell by cell.
for (i=0;i <hang;i++)
{
for (j=0;j <lie;j++)
{
oSheet.Cells(i+1,j+1).value = table.rows(i).cells(j).innerText;
}
}
oXL.Visible = true;
oXL.UserControl = true;
}
------解决方案--------------------asp里的写法:
<% '生成对象,可见,新增%>
set obj=CreateObject( "Excel.Application ")
obj.Visible=True
obj.Workbooks.Add
With obj.ActiveWorkBook.Activesheet
.columns(1).NumberFormatLocal= "@ " '第一列格式化为字符串
.Range( "A:D ").NumberFormatLocal= "@ " '把第1-4列格式化为字符串
'或者用写的时候 在字符串前见 单引号即可!与以上同样效果
'response.write ".Cells(1,2)= " " "& " ' "&字符串 & " " " "&chr(13)
.Range( "E:F ").NumberFormatLocal= "0.0 " '把5-6列格式化为数字保留一位小数
End with
set obj=Nothing
-------------------以上代码我用了1年了 100%可以用有效!