javascript操作excel

本文介绍了一种使用JavaScript操作Excel的方法,通过创建ActiveXObject对象来实现客户端的Excel文档操作,包括设置单元格样式、合并单元格及自动调整列宽等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

javascript确实很强,在其中能够操作多种的文档,包括word,excel,读写xml,很多通常我们觉得要写在服务端的代码也能够利用javascript在客户端得到很好的实现

下面是自己利用javascript来操作excel文档的例子,希望对需要的人能够有所帮助!

	try{
		var oXL = new ActiveXObject("Excel.Application");
	}catch(Exception){
		alert("请将你的浏览器安全设置为最低或者你的浏览器不支持此功能!");
		window.close();
	}
	
		var oWB = oXL.Workbooks.Add();
		var oSheet = oWB.ActiveSheet;
    var title = "产品信息请妥善保存";
    
    var i = 1;

    oSheet.Cells(i,1).numberformatlocal="@";
    oSheet.Cells(i,1).Font.size="14";//设置单元格字体大小
    oSheet.Cells(i,1).Font.Bold = true;//设置字体是否为粗体
    oSheet.Cells(i,1).value=title;
    oSheet.Cells(i,1).Borders.LineStyle = 1;
    oSheet.Range("A1:B1").merge() ;//合并单元格
    oSheet.Range("A1:B1").Borders.LineStyle = 1;//设置单元格边线

    i++;//2
		oSheet.Cells(i,1).numberformatlocal="@";
		oSheet.Cells(i,1).Font.size="12";
		oSheet.Cells(i,1).value="纳税人识别号";
		oSheet.Cells(i,1).Borders.LineStyle = 1;
		
		oSheet.Cells(i,2).numberformatlocal="@";
		oSheet.Cells(i,2).Font.size="12";
		oSheet.Cells(i,2).value=nsrsbh;
		oSheet.Cells(i,2).Borders.LineStyle = 1;
		
    i++;//3
		oSheet.Cells(i,1).numberformatlocal="@";
		oSheet.Cells(i,1).Font.size="12";
		oSheet.Cells(i,1).value="纳税人名称";
		oSheet.Cells(i,1).Borders.LineStyle = 1;
		
		oSheet.Cells(i,2).numberformatlocal="@";
		oSheet.Cells(i,2).Font.size="12";
		oSheet.Cells(i,2).value=nsrmc;
		oSheet.Cells(i,2).Borders.LineStyle = 1;
		
    i++;//4
		oSheet.Cells(i,1).numberformatlocal="@";
		oSheet.Cells(i,1).Font.size="12";
		oSheet.Cells(i,1).value="序列号";
		oSheet.Cells(i,1).Borders.LineStyle = 1;
		
		oSheet.Cells(i,2).numberformatlocal="@";
		oSheet.Cells(i,2).Font.size="12";
		oSheet.Cells(i,2).value=xlh;
		oSheet.Cells(i,2).Borders.LineStyle = 1;
		
    //设置自动调整列宽
    oSheet.Columns.AutoFit;
    try {
      with (oSheet.PageSetup) {
        Orientation  = 1;        //1:纵向 2横向 打印
        HeaderMargin = 0/0.035;  //页眉1cm
        FooterMargin = 0/0.035;  //页脚0cm
        TopMargin    = 0/0.035;  //顶边距1cm
        BottomMargin = 0/0.035;  //底边距0cm
        LeftMargin   = 0/0.035;  //左边距0cm
        RightMargin  = 0/0.035;  //右边距0cm
      }
    } catch (e) {}

		oXL.Visible = true;
		oXL.UserControl = true;
    oXL = null;
    oWB = null;
    oSheet = null;

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值