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;