javascript操作excel实例代码
发布时间:2015-08-10 编辑:jiaochengji.com
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;
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;
您可能感兴趣的文章:
javascript excel操作类实现代码
javascript 操作excel代码示例
[C#]结束Excel的进程的方法
javascript操作excel文件方法详解
c#(asp.net)连接excel的实例代码
javascript操作excel文件的例子
.net中清除EXCEL进程最有效的方法
javascript操作excel知识点总结
解决php下载excel无法打开的问题
php读取excel的实例代码
[关闭]