lysj102289 发表于 2013-2-7 14:36:02

js读取电子表格 对日期格式化

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
var excelFileName = "d:/123.xls";
var oWB;

var divid = "";
function showExcel(targetDIVID){
divid = targetDIVID;

   
//objID为表格ID
//需要在浏览器安全级别设置中启用ActiveX
// Start Excel and get Application object.
var oXL=null;
try{
oXL = new ActiveXObject("Excel.Application");

}catch(e){
alert(e.message);
return ;
}
if (oXL == null){
alert("创建Excel文件失败,可能是您的计算机上没有正确安装Microsoft Office Excel软件或浏览器的安全级别设置过高!");
return;
}
try{
// Get a new workbook.

oWB = oXL.Workbooks.Open(excelFileName);

showSheet(1);
//for (i = 1; i <= oWB.Sheets.Count; i++){
//if (oWB.Sheets(i).name.lastIndexOf("月") != -1){

//}
//}
}
catch (e){
alert(e.message);
}
oWB.Close();   //不close工作簿的话,后果还是挺严重的。
oWB = null;
oXL = null;
}
function showSheet(sheetNO){
var oSheet = oWB.Sheets(sheetNO);

var tablebody = "";
var a="";
var temp = "";
tablebody = "<table border=1 align= 'center'   width= '100%' borderColor='#111111' cellSpacing='0' cellPadding='2'>";
var today = new Array("星期天","星期一","星期二","星期三","星期四","星期五","星期六");
for (i = 1; i < oSheet.UsedRange.Rows.Count; i++){
tablebody = tablebody + "<tr>";
for (j = 1; j < oSheet.UsedRange.Columns.Count; j++){
value = oSheet.Cells(i, j).Value;

if(j==1&& (i != 1) && value != undefined && (typeof(oSheet.Cells(i, j).Value) == "date")){
a   =   new   Date( value);
temp = a.getDay();
//day.getDay();根据Date返一个星期中的某一天,其中0为星期日
a   =   a.getFullYear()   +   "年"   +   (a.getMonth()+1)   +   "月"   +   a.getDate() + "日";
value = a + today;
} else {

}
if (value == undefined){
value = " ";
}
if((i == 1 && j == 1))
{
tablebody = tablebody +"<th colspan=2 width=250px align= 'center'><b>";
}
else if((i == 1 && j == 3))
{
tablebody = tablebody +"<th colspan=2 width=550px><b>";
}
else if(i != 1 && j == 1 )
{
tablebody = tablebody + "<tdwidth=180px><b>";
}
else if(i != 1 &&j == 2)
{
tablebody = tablebody + "<tdwidth=100px><b>";
}
else if(i != 1 &&j == 3)
{
tablebody = tablebody + "<tdwidth=200px><b>";
}
else if(i != 1 &&j == 4)
{
tablebody = tablebody + "<tdwidth=680px><b>";
}
//i == 1 ? tablebody = tablebody + "<thnowrap=true><b>" : tablebody = tablebody + "<td>";
tablebody = tablebody + value;
if(i == 1)
{
tablebody = tablebody + "</b></th>";
}
else
{
tablebody = tablebody + "</b></td>";
}

//i == 1 ? tablebody = tablebody + "</b></th>" : tablebody = tablebody + "</td>";
}
tablebody = tablebody + "</tr>";
}
tablebody = tablebody + "</table>";
alert(tablebody);
document.write(tablebody);
oSheet = null;
}



//-->
</SCRIPT>
</HEAD>
<BODY >
<input type="button"value="test"/>
<div id="exl"></div>
</BODY>
</HTML>
页: [1]
查看完整版本: js读取电子表格 对日期格式化