首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用JavaScript在word文档中的特定单元格中插入图像

如何使用JavaScript在word文档中的特定单元格中插入图像
EN

Stack Overflow用户
提问于 2014-07-30 07:00:22
回答 1查看 2K关注 0票数 0

我有一个客户端应用程序,它在浏览器(IE)上呈现地图。在该应用程序中,我有一个HTML表单,它将捕获一些文本内容,然后应用程序将生成一个Word文档,其中包含用户以表格形式提交的所有字段。我能够做到这一点,但是我必须从浏览器中插入一张图片(地图的截图)。

我将问题分为两部分: 1)使用javascript以表格形式编写一些表格内容--我能够做到这一点。2)把地图的屏幕截图插入到文字文档中。

问题是,我希望这个图像位于第四行和第一列。但是"word.Selection“指向表中的第一个单元格。

Itable.columns(1).cells(5).Range.Text=“something”;我们不能使用类似的东西在特定的单元格中插入文本或放置"word.Selection“。

下面的页面满足我的表单要求。

代码语言:javascript
复制
        <!DOCTYPE html>
        <html>

        <head>
        <script src="jquery-1.11.0.js"></script>
        <script type="text/javascript">

        function myFunction()
        {
        var table = document.getElementById("myTable");
        var row = table.insertRow(1);
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        cell1.innerHTML = getFieldValue("FirstName");
        cell2.innerHTML = getFieldValue("lastName");
        }
         var JPEGName = "C:\\all data\\MyBT\\tasks\\dp manifold.jpg";
        function saveAsWord() {
        word = new ActiveXObject("Word.Application");
        word.visible=false;
        word.Documents.Add();
         word.Application.Visible = true;
        word.ActiveDocument.PageSetup.LineNumbering.Active=false;
        word.ActiveDocument.PageSetup.TopMargin=30;
        word.ActiveDocument.PageSetup.BottomMargin = 30;
        word.ActiveDocument.PageSetup.LeftMargin = 35;
        word.ActiveDocument.PageSetup.RightMargin = 30;
        word.ActiveDocument.PageSetup.Gutter = 0.0;
        word.Selection.Font.Bold = true;
        word.Selection.Font.Size = 14;
        word.Selection.ParagraphFormat.Alignment = 1;
        word.Selection.TypeText("form details");
        word.Selection.Font.Size = 12;
        var itable=word.ActiveDocument.Tables.Add(word.ActiveDocument.Application.Selection.Range, 16, 4);
        //itable.AutoFormat(16);

        word.ActiveDocument.Tables(1).Range.ParagraphFormat.Alignment = 0;
        itable.columns(2).cells(1).Range.ParagraphFormat.Alignment =1;
        itable.columns(2).cells(1).Range.Text="Exchange Area"
        itable.columns(3).cells(1).Range.ParagraphFormat.Alignment =0;
        itable.columns(1).cells(2).Range.Text="CSS Job No ";
        itable.columns(3).cells(2).Range.Text="Sr. No. : ";
        itable.columns(1).cells(3).Range.Text="Customer Details : "+getFieldValue("firstName");
        itable.columns(1).cells(4).Range.Text="Engineers Details    : "+getFieldValue("lastName");
        itable.columns(3).cells(4).Range.ParagraphFormat.Alignment =0;
        itable.columns(3).cells(4).Range.Text="Date : ";
        //itable.columns(3).cells(4).Range.Content.InlineShapes.AddPicture(JPEGName);
        itable.columns(1).cells(5).Range.Text="Requirement : ";
        itable.columns(3).cells(5).Range.ParagraphFormat.Alignment =0;
        itable.columns(3).cells(5).Range.Text="Survey Reqd  : ";
        itable.columns(1).cells(6).Range.Text="Visiting Date    : ";
        itable.columns(2).cells(6).Range.ParagraphFormat.Alignment =1;
        itable.columns(2).cells(6).Range.Text="Visit Time : ";
        itable.columns(1).cells(5).Range.Text="Whom To Meet : ";
        itable.columns(3).cells(5).Range.ParagraphFormat.Alignment =0;
        itable.columns(3).cells(5).Range.Text="Underground Cable    : ";
        itable.columns(1).cells(6).Range.Text="Visiting Date    : ";
        itable.columns(2).cells(6).Range.ParagraphFormat.Alignment =1;
        itable.columns(2).cells(6).Range.Text="Overhead Work : ";
        itable.columns(3).cells(6).Range.Text="Underground Civil  : ";
        itable.columns(1).cells(7).Range.Text="Location:   ";
        itable.columns(1).cells(8).Range.Text="Map Ref:    ";
        itable.columns(1).cells(10).Range.ParagraphFormat.Alignment =1;
        itable.columns(1).cells(10).Range.Text="Title:  EXPOSE BURIED JOINT";
        itable.columns(3).cells(10).Range.ParagraphFormat.Alignment =1;
        itable.columns(3).cells(10).Range.Text="Job Summary";
        itable.columns(1).cells(12).Range.ParagraphFormat.Alignment =1;
        itable.columns(1).cells(12).Range.Text="Sig. of Security Supervisior";
        itable.columns(3).cells(12).Range.ParagraphFormat.Alignment =1;
        itable.columns(3).cells(12).Range.Text="Sig. of Meeting Person";
        itable.Cell(3, 1).Merge(itable.Cell(3, 2));
        itable.Cell(4, 2).Split(1, 2);
        itable.Cell(4, 1).Merge(itable.Cell(4, 2));
        itable.Cell(4, 2).Merge(itable.Cell(4, 3));
        itable.Cell(5, 2).Split(1, 2);
        itable.Cell(5, 1).Merge(itable.Cell(5, 2));
        itable.Cell(5, 2).Merge(itable.Cell(5, 3));
        itable.Cell(7, 1).Merge(itable.Cell(7, 3));
        itable.Cell(8, 1).Merge(itable.Cell(8, 3));
        itable.Cell(9, 1).Merge(itable.Cell(9, 3));
        itable.Cell(11, 1).Merge(itable.Cell(11, 3));
        itable.Cell(13, 1).Merge(itable.Cell(13, 3));
        itable.Cell(14, 1).Merge(itable.Cell(14, 3));
        itable.Cell(15, 1).Merge(itable.Cell(15, 3));
        itable.Cell(16, 1).Merge(itable.Cell(16, 3));
        //word.Application.PrintOut(true);
        //setTimeout("appexit()",10000);
        word.Selection.TypeParagraph();
        word.Selection.InlineShapes.AddPicture(JPEGName);
        }
        </script> 
        </head>
        <body>  
        <table id="myTable" style="border:1px solid black">
        <tr>
        <td>First Name</td>
        <td>Last Name</td><td>SSN</td>
        </tr>

        </tr>
        </table><br><br><br>
        Your Social security number is <script type="text/javascript">
        document.write(getFieldValue("ssn"))
        </script>.<br>
        You entered &quot;<script type="text/javascript">
        document.write(getFieldValue("FirstName"))
        </script>&quot; as your First Name.<br>
        You entered &quot;<script type="text/javascript">
        document.write(getFieldValue("lastName"))
        </script>&quot; as your Last Name.<br>
        <button id="foo" onclick="myFunction()">Try it</button>
        <input type="button" value="print slip" onclick="saveAsWord()"/> 

        </body>
        </html>

我是新编写脚本来生成MS文档的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-01 07:56:50

我发现了我做错了的代码。与其使用"word.Selection“,不如使用”itable.cell(行,列).range“进行插入。

代码语言:javascript
复制
      itable.cell(5,1).range.InlineShapes.AddPicture(JPEGName );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25030547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档