Javascript 前端生成Word文档

最近做项目遇到要在Web上生成Word文件的需求。一般方案是用报表系统或者在后台生成。由于报表系统中生成的页面再转换成Word难以排版,这里就用前端生成的办法作为临时解决方案。直接上代码了,提供文字排版和表格设计,满足基本功能。如果需要其他功能,需要在Word上录制宏,再把代码转换成JS。
环境:只能是IE浏览器,使用ActiveXObject(“Word.Application”)

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>JS调用Word</title>
</head>
<body>
<button onclick = 'showWord()'>显示Word</button>

<script>
function showWord(){
	var wobj=new ActiveXObject("Word.Application");
	var docs=wobj.Documents.Add();
	wobj.visible=true;//是否显示Word
	
	//设置页边距    2.79   0.49    2.96   2.89 cm  转换为磅
	//可以vba里运行debug.print CentimetersToPoints(2.79)得到磅值
	docs.PageSetup.TopMargin = 79.08662;
    docs.PageSetup.BottomMargin = 13.88976;
    docs.PageSetup.LeftMargin = 83.90551;
    docs.PageSetup.RightMargin = 81.92126;

    var docp = docs.Paragraphs(1).Range;	
	docp.Text = '编号:001';
	docp.Font.Name = "宋体";
	docp.Font.Size = 12;
	docp.Font.Bold = 0;
	docp.ParagraphFormat.LineUnitBefore = 0;
	docp.ParagraphFormat.LineUnitAfter = 0;
	docp.ParagraphFormat.Alignment = 2;
	docp.InsertParagraphAfter();//换行

	//标题  
	docp = docs.Paragraphs(2).Range;
	docp.Text = '告业主通知书';
	docp.Font.Name = "宋体";
	docp.Font.Size = 22;
	docp.Font.Bold = 1;
	docp.ParagraphFormat.LineUnitBefore = 0;
	docp.ParagraphFormat.LineUnitAfter = 0;
	docp.ParagraphFormat.SpaceAfter = 0;
	docp.ParagraphFormat.SpaceBefore = 0;
	docp.ParagraphFormat.Alignment = 1;
	docp.InsertParagraphAfter();
	//第三行空行
	docp = docs.Paragraphs(3).Range;
	docp.Text = ''
	docp.Font.Name = "宋体";
	docp.Font.Size = 12;
	docp.InsertParagraphAfter();
	//第四行    
	docp = docs.Paragraphs(4).Range;
	docp.Text = '各位业主:';
	docp.Font.Name = "宋体";
	docp.Font.Size = 14;
	docp.Font.Bold = 0;
	docp.ParagraphFormat.Alignment = 3;
	docp.InsertParagraphAfter();

	docp = docs.Paragraphs(4).Range;
	docp.Text = '1.请广大业主在接到或看到告知书即日起,自行将您的非机动车辆入库停放或停放在划定的停放区域。机动车入库停放或在地上按位停放,坚决杜绝乱停乱放现象。';
	docp.Font.Name = "宋体";
	docp.Font.Size = 14;
	docp.Font.Bold = 0;
	docp.ParagraphFormat.Alignment = 3;
	//缩进两个字符
	docp.ParagraphFormat.CharacterUnitFirstLineIndent = 2;
	docp.InsertParagraphAfter();

	docp = docs.Paragraphs(5).Range;
	docp.Text = '2.对于不入库、按位停放的车辆,住建局物监办将联合市公安局,消防大队、城管执法大队等部门集中整治,将违章停放的各类车辆托运到市消防大队和交警大队停放。';
	docp.Font.Name = "宋体";
	docp.Font.Size = 14;
	docp.Font.Bold = 0;
	docp.ParagraphFormat.Alignment = 3;
	docp.ParagraphFormat.CharacterUnitFirstLineIndent = 2;
	docp.InsertParagraphAfter();

	docp = docs.Paragraphs(6).Range;
	docp.Text = '3.各物业企业加强小区内宣传,及时将告知书分发各业主并在小区公告栏张贴,组织保安进行整治,对在集中整治中出现阻挠、谩骂行为的业主进行登记造册。将由公安,消防、城建执法部门依法依规进行处罚。';
	docp.Font.Name = "宋体";
	docp.Font.Size = 14;
	docp.Font.Bold = 0;
	docp.ParagraphFormat.Alignment = 3;
	docp.ParagraphFormat.CharacterUnitFirstLineIndent = 2;
	docp.InsertParagraphAfter();

	docp = docs.Paragraphs(7).Range;
	docp.Text = '';
	docp.InsertParagraphAfter();

	//第8行 落款时间
	docp = docs.Paragraphs(8).Range;
	var d = '20200102';
	docp.Text = d.substr(0,4) +"年" +Number(d.substr(4,2))+"月"+Number(d.substr(6,2))+"日";
	docp.Font.Name = "宋体";
	docp.Font.Size = 14;
	docp.ParagraphFormat.Alignment = 2;
	docp.InsertParagraphAfter();

	//分页符
	docp = docs.Paragraphs(9).Range;
	docp.InsertBreak();


    var docp = docs.Paragraphs(10).Range;	
	docp.Text = '';
	docp.InsertParagraphAfter();

	docp = docs.Paragraphs(11).Range;
	docp.Text = '送 达 回 执';
	docp.Font.Name = "宋体";
	docp.Font.Size = 22;
	docp.Font.Bold = 1;
	docp.ParagraphFormat.LineUnitBefore = 0;
	docp.ParagraphFormat.LineUnitAfter = 0;
	docp.ParagraphFormat.SpaceAfter = 0;
	docp.ParagraphFormat.SpaceBefore = 0;
	docp.ParagraphFormat.Alignment = 1;
	docp.InsertParagraphAfter();

	docp = docs.Paragraphs(12).Range;	
	docp.Text = '';
	docp.Font.Name = "宋体";
	docp.Font.Size = 14;
	docp.Font.Bold = 0;
	docp.InsertParagraphAfter();
	
	docp = docs.Paragraphs(13).Range;
	mytable = docs.Tables.Add(docp,12,4)
	//表格加上线条
	mytable.Style = "网格型";

	var cell = mytable.Cell(1,1).Range;
	cell.Text = '文件名称';
	cell.ParagraphFormat.Alignment = 1;
	var cells = docs.Range(mytable.Cell(1,2).Range.Start,mytable.Cell(1,4).Range.End);
	cells.Cells.Merge();

	cell = mytable.Cell(2,1).Range;
	cell.Text = '文件编号';
	cells = docs.Range(mytable.Cell(2,2).Range.Start,mytable.Cell(2,4).Range.End);
	cells.Cells.Merge();

	cell = mytable.Cell(3,1).Range;
	cell.Text = '受送达单位';
	cells = docs.Range(mytable.Cell(3,2).Range.Start,mytable.Cell(3,4).Range.End);
	cells.Cells.Merge();

	cell = mytable.Cell(4,1).Range;
	cell.Text = '受送达员工';
	cells = docs.Range(mytable.Cell(4,2).Range.Start,mytable.Cell(4,4).Range.End);
	cells.Cells.Merge();

	cell = mytable.Cell(5,1).Range;
	cell.Text = 'EMS编号';

	cell = mytable.Cell(5,3).Range;
	cell.Text = '发出时间';

	cell = mytable.Cell(6,1).Range;
	cell.Text = '单位收件人签名';

	cell = mytable.Cell(6,3).Range;
	cell.Text = '收到时间';

	cell = mytable.Cell(7,1).Range;
	cell.Text = 'EMS编号';

	cell = mytable.Cell(7,3).Range;
	cell.Text = '发出时间';

	cell = mytable.Cell(8,1).Range;
	cell.Text = '职工签名';

	cell = mytable.Cell(8,3).Range;
	cell.Text = '收到时间';

	cell = mytable.Cell(9,1).Range;
	cell.Text = '代收人';
	cell.InsertParagraphAfter();
	cell_docp = cell.Paragraphs(2).Range;
	cell_docp.Text = '请注明理由';
	//设置段落 固定值  18磅
	cell_Range = docs.Range(cell.Paragraphs(1).Range.Start,cell.Paragraphs(2).Range.End);
	cell_Range.ParagraphFormat.LineSpacing = 18;
	cell_Range.ParagraphFormat.LineSpacingRule = 4;
	cells = docs.Range(mytable.Cell(9,2).Range.Start,mytable.Cell(9,4).Range.End);
	cells.Cells.Merge();
	

	cell = mytable.Cell(10,1).Range;
	cell.Text = '公告时间';
	cells = docs.Range(mytable.Cell(10,2).Range.Start,mytable.Cell(10,4).Range.End);
	cells.Cells.Merge();

	cell = mytable.Cell(11,1).Range;
	cell.Text = '送达人';
	cells = docs.Range(mytable.Cell(11,2).Range.Start,mytable.Cell(11,4).Range.End);
	cells.Cells.Merge();

	cell = mytable.Cell(12,1).Range;
	cell.Text = '备注';
	cells = docs.Range(mytable.Cell(12,2).Range.Start,mytable.Cell(12,4).Range.End);
	cells.Cells.Merge();

	//表格行高固定值1.5厘米  1.5厘米转化为42.51968
	//可以vba里运行debug.print CentimetersToPoints(1.5)得到
	mytable.Rows.HeightRule = 2;
    mytable.Rows.Height = 42.51968;   
    //水平居中
    cells = docs.Range(mytable.Cell(1,1).Range.Start,mytable.Cell(12,1).Range.End);
    cells.ParagraphFormat.Alignment = 1;
    cells.Cells.VerticalAlignment = 1;

	var n = docs.Paragraphs.Count;
	docp = docs.Paragraphs(n).Range;	
	docp.Text = '备注:用人单位、职工或者其近亲属委托代理人签收的,应当提交授权委托书';
	docp.Font.Name = "宋体";
	docp.Font.Size = 11;
	docp.Font.Bold = 0;
	docp.ParagraphFormat.Alignment = 3;


	/* 保存,且关闭Word
	docs.SaveAs("E:\\通知.doc");
	docs.Close(savechanges=false);
	wobj.Application.Quit();*/

}
</script>
</body>
</html>
  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值