您好,欢迎访问一九零五行业门户网

JS Excel读取和写入操作(模板操作)实现代码_javascript技巧

本人是在实际项目中摸索出,js读写excel(模板)数据,包括怎么用js把图片插入excel中。
首先,添加一个公用的模板地址js,如下:
addzdaddress.js
复制代码 代码如下:
///////////////////////////////////////////////////////////////////////////////////////////////////
////docname:信任站点地址添加--addzdaddress.js
////author:lify
////company:wavenet
////date:2009-11-04
////editdate:2010-03-11
////maincontent:findings,ajax and so on;
//////////////////////////////////////////////////////////////////////////////////////////////////
var believeaddress='172.18.1.25/psc1';////配置地址配置程序时需要的配置地址
////**********************************excel 导入到web界面模板地址集合*******************************************//
//////////////////////////飞行检测excel模板地址配置/////////////////////////////////////////////////////////////
var template_path_fxjcexceldatasintosqldatas = http://%22+believeaddress+%22/newreports/xls_template/飞行检测城镇污水厂进出水主要污染物浓度.xls;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////***********************************************************************************************************//
////*********************************excel 录入模板导入地址集合**********************************************//
//////////////////////////月报导入excel地址配置//////////////////////////////////
var template_path_month = http://%22+believeaddress+%22/newreports/xls_template/上海市污水处理企业生产运行表.xls;
/////////////////////////////////////////////////////////////////////////////////
//////////////////////////年报导入excel地址配置//////////////////////////////////
var template_path_year = http://%22+believeaddress+%22/newreports/xls_template/上海市污水处理设施信息表.xls;
////*********************************************************************************************************//
//图片地址
var pic_path=http://%22+believeaddress+%22/newreports/;
/////////////////////////////////////////////////////////////////////////////////
再次,怎么样把页面数据读取到excel中js,如下:
yearreportluru.jsyearreportluru.js
复制代码 代码如下:
////docname:污水处理设施信息表(年报)-录入报表--yearreportluru.js
////author:lify
////company:wavenet
////date:2009-08-14
////editdate:2009-08-14
////maincontent:findings,ajax and so on;
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////添加正则表达式类/////////////////////////////////////////////////////////////
document.write(
最后,如何读取到excel模板中数据到页面上共客户编辑数据再保存到数据库中(此步可以用xml发送至后台交互,在此不进一步说明)js,如下:
fxjc_exceldatasintosqldatas.js
复制代码 代码如下:
////docname:导入模板数据功能--fxjc_exceldatasintosqldatas.js
////author:lify
////company:wavenet
////date:2010-03-12
////editdate:2010-03-12
////maincontent:findings,ajax and so on;
//////////////////////////////////////////////////////////////////////////////////////////////////
var excelfilename=; //本地excel地址
var owb;
var idtmr=;
function automateexcel3()
{
//删除pt表的除前2行的所有行
deletetablept();
//alert(pic_path+document.getelementbyid(imgurl).value);//测试
var xls=null;
var newbook;
try{
xls = new activexobject(excel.application);
}catch(e){openbg(0);opents(0);
alert(请您打开activex控件,具体操作:单击ie中的“工具”里“internet 选项”,单击“安全”中的“自定义级别”中“activex控件和插件”中,请选择第三项为启用即可。详细错误:+e.message);
return ;
}
if (xls == null){openbg(0);opents(0);
alert(创建excel文件失败,可能是您的计算机上没有正确安装microsoft office excel软件或浏览器的安全级别设置过高!);
return;
}
try{
newbook = xls.workbooks.open(excelfilename);//这里的add方法里的参数是模板的路径
}catch(e){openbg(0);opents(0);
alert(加载的excel文件路径有错误!详细:+e.message);
return ;
}
var osheet=newbook.activesheet;
////////////////////////////////////////////模板写入数据到web页面中////////////////////////////////////////////////////////////
var xhnum=0;
//alert(osheet.cells(3,18).value=+osheet.cells(3,18).value+|+(osheet.cells(3,5).value==for(var i=3;i{
if(osheet.cells(i,1).value)
{
xhnum++;
var newrow = document.getelementbyid(pt).insertrow(-1);
var newcell;
newrow.align = center;
newrow.height = 35px;
for(var j=0;j{
if(j==4)
{
//什么都不做
}
else if(j{
newcell = newrow.insertcell(j);
if(j==0) newcell.innerhtml = (xhnum).tostring();//序号
else if(j==1)
{
if(!osheet.cells(i,1).value)
newcell.innerhtml =  ;
else if(osheet.cells(i,1).value==——||osheet.cells(i,1).value==--)
newcell.innerhtml =  ;
else
newcell.innerhtml = (osheet.cells(i,1).value==? :osheet.cells(i,1).value);
}
else if(j==2)
{
if(!osheet.cells(i,2).value)
newcell.innerhtml =  ;
else if(osheet.cells(i,2).value==——||osheet.cells(i,2).value==--)
newcell.innerhtml =  ;
else
newcell.innerhtml = (osheet.cells(i,2).value==? :osheet.cells(i,2).value);
//newcell.innerhtml = (osheet.cells(i,2).value==? :osheet.cells(i,2).value);
}
else if(j==3)
{
if(!osheet.cells(i,4).value)
newcell.innerhtml =  ;
else if(osheet.cells(i,4).value==——||osheet.cells(i,4).value==--)
newcell.innerhtml =  ;
else
newcell.innerhtml = (osheet.cells(i,4).value==? :osheet.cells(i,4).value);
//newcell.innerhtml = (osheet.cells(i,4).value==? :osheet.cells(i,4).value);
}
else if(j==9)
{
if(!osheet.cells(i,j+1).value)
newcell.innerhtml =  ;
else if(osheet.cells(i,j+1).value==newcell.innerhtml = 30;
else if(osheet.cells(i,j+1).value==——||osheet.cells(i,j+1).value==--)
newcell.innerhtml =  ;
else
newcell.innerhtml = (osheet.cells(i,j+1).value==? :osheet.cells(i,j+1).value);
}
else
{
if(!osheet.cells(i,j+1).value)
newcell.innerhtml =  ;
else if(osheet.cells(i,j+1).value==newcell.innerhtml = 30;
else if(osheet.cells(i,j+1).value==newcell.innerhtml =  ;
else if(osheet.cells(i,j+1).value==——||osheet.cells(i,j+1).value==--)
newcell.innerhtml =  ;
else
newcell.innerhtml = (osheet.cells(i,j+1).value==? :osheet.cells(i,j+1).value);
}
}
else
{
newcell = newrow.insertcell(j-1);//少一个就可以了,少样品编号插入
if(j==0) newcell.innerhtml = (xhnum).tostring();//序号
else if(j==1)
{
if(!osheet.cells(i,1).value)
newcell.innerhtml =  ;
else if(osheet.cells(i,1).value==——||osheet.cells(i,1).value==--)
newcell.innerhtml =  ;
else
newcell.innerhtml = (osheet.cells(i,1).value==? :osheet.cells(i,1).value);
}
else if(j==2)
{
if(!osheet.cells(i,2).value)
newcell.innerhtml =  ;
else if(osheet.cells(i,2).value==——||osheet.cells(i,2).value==--)
newcell.innerhtml =  ;
else
newcell.innerhtml = (osheet.cells(i,2).value==? :osheet.cells(i,2).value);
//newcell.innerhtml = (osheet.cells(i,2).value==? :osheet.cells(i,2).value);
}
else if(j==3)
{
if(!osheet.cells(i,4).value)
newcell.innerhtml =  ;
else if(osheet.cells(i,4).value==——||osheet.cells(i,4).value==--)
newcell.innerhtml =  ;
else
newcell.innerhtml = (osheet.cells(i,4).value==? :osheet.cells(i,4).value);
//newcell.innerhtml = (osheet.cells(i,4).value==? :osheet.cells(i,4).value);
}
else if(j==9)
{
if(!osheet.cells(i,j+1).value)
newcell.innerhtml =  ;
else if(osheet.cells(i,j+1).value==newcell.innerhtml = 30;
else if(osheet.cells(i,j+1).value==——||osheet.cells(i,j+1).value==--)
newcell.innerhtml =  ;
else
newcell.innerhtml = (osheet.cells(i,j+1).value==? :osheet.cells(i,j+1).value);
}
else
{
if(!osheet.cells(i,j+1).value)
newcell.innerhtml =  ;
else if(osheet.cells(i,j+1).value==newcell.innerhtml = 30;
else if(osheet.cells(i,j+1).value==newcell.innerhtml =  ;
else if(osheet.cells(i,j+1).value==——||osheet.cells(i,j+1).value==--)
newcell.innerhtml =  ;
else
newcell.innerhtml = (osheet.cells(i,j+1).value==? :osheet.cells(i,j+1).value);
}
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
newbook.close();
newbook=null;
xls = null;
openbg(0);opents(0);
///////////////////////////////开启编辑和保存按钮功能///////////////////////////////////
onoroffbtn(1);
////////////////////////////////////////////////////////////////////////////////////////
idtmr = window.setinterval(cleanup();,1);
////////////////////////////////表格数字和字符转化//////////////////////////////////////
tableintodatas();////表格数字和字符转化
////////////////////////////////////////////////////////////////////////////////////////
}
其实做报表很痛苦,但是从中也能经历不少。很高兴以此文章让那些报表高手前来指导和拍砖。谢谢了!
以上为针对excel模板总结的一些方法,欢迎各位前来拍砖!也欢迎大家来交流交流!
其它类似信息

推荐信息