在线查看word文档

 

页面代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>附件上传</title>
    <style type="text/css">
        *
        {
            color: #383838;
            font-size: 12px;
            margin:0;
            padding:0;
        }
        #container table
        {
        	margin:50px auto 0 auto;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div id="container">
        <table cellpadding="0" cellspacing="0">
            <tr>
                <td>
                    请选在要上传的文件(最大4M):
                </td>
                <td>
                    <asp:FileUpload ID="FileUpload1" Width="250px" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                   上传 Word文档将生成在线查看文档!
                </td>
                <td>
                    <asp:Button ID="btnSubmit" runat="server" Text="确定上传" OnClick="btnSubmit_Click" />
                    <asp:HyperLink ID="HyperLink1" runat="server" Visible="false">查看生成文档</asp:HyperLink>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

 

 

后台代码:


 

protected void btnSubmit_Click(object sender, EventArgs e)
        {
            HttpPostedFile upLoadFile = FileUpload1.PostedFile;
            if (upLoadFile.ContentLength >= 4194304)
            {
                //如果上传文件大于4M则提示用户上传附件失败
                ScriptManager.RegisterClientScriptBlock(btnSubmit, btnSubmit.GetType(), null, "<script>alert('上传失败,文件不能大于4M!')</script>", false);
            }
            else
            {
                //此处的处理方法是将doc文件直接转为html文件,存放在服务器的某个路径下,然后直接读取即可
                //用到的类库是Microsoft.Office.Interop.Word,此类库net自带,不必自行下载 modify by Vincnet.Q 10.11.27
                string fileName = upLoadFile.FileName.Substring(upLoadFile.FileName.LastIndexOf("\\"));
                string fileExtension =upLoadFile.FileName.Substring( upLoadFile.FileName.LastIndexOf(".")+1).ToLower();
                string path = Server.MapPath("UpLoadFiles") + "\\" +fileName ;
                upLoadFile.SaveAs(path);
                if (fileExtension == "doc" || fileExtension == "docx")
                {
                    #region 定义打开word文档发方法的参数
                    object filePath = Server.MapPath("UpLoadFiles\\"+fileName);
                    object readOnly = true;
                    object addToRecentFiles = false;
                    object confirmConversion = false;
                    object revert = true;
                    object visible = false;
                    object formate = WdSaveFormat.wdFormatFilteredHTML;
                    object missing = System.Reflection.Missing.Value;
                    object savePath = Server.MapPath("GenerationFiles\\"+fileName.Replace(fileExtension,"html"));
                    object saveChanges = false;
                    #endregion
                    //word引用程序
                    Application word = new Application();
                    //打开一个文档
                    Document doc = word.Documents.Open(ref filePath,
            ref  confirmConversion, ref readOnly, ref addToRecentFiles, ref missing, ref missing, ref revert, ref missing, ref missing, ref missing, ref missing,
            ref visible, ref missing, ref missing, ref missing, ref missing);
                    //另存为网页格式
                    doc.SaveAs(ref savePath, ref formate, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                    word.Quit(ref saveChanges, ref missing, ref missing);
                    HyperLink1.Visible = true;
                    HyperLink1.NavigateUrl = "GenerationFiles" + fileName.Replace(fileExtension, "html");
                    
                    //显示出路径
                    HyperLink1.Text = HyperLink1.NavigateUrl;

                }
                ScriptManager.RegisterClientScriptBlock(btnSubmit, btnSubmit.GetType(), null, "<script>alert('上传附件成功')</script>", false);
            }
        }


 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要实现上传Word文档在线查看,需要以下几个步骤: 1. 创建一个表单,用于上传文件。可以使用HTML的form标签来实现。 ``` <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload" name="submit"> </form> ``` 2. 创建一个PHP脚本,用于处理上传文件。在PHP脚本中,可以使用PHPWord库来读取Word文档,并将其转换为HTML格式。 ``` <?php require_once 'vendor/autoload.php'; use PhpOffice\PhpWord\IOFactory; if(isset($_POST["submit"])) { $target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $fileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); if($fileType != "docx") { echo "Sorry, only DOCX files are allowed."; $uploadOk = 0; } if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; } else { if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { $phpWord = IOFactory::load($target_file); echo $phpWord->saveHTML(); } else { echo "Sorry, there was an error uploading your file."; } } } ?> ``` 3. 在PHP脚本中,使用PHPWord库来读取上传的Word文档,并将其转换为HTML格式。最后,将解析后的HTML输出到浏览器中,以便用户在线查看。 上面的代码使用了PHPWord库来读取Word文档,并将其转换为HTML格式。如果需要使用其他编程语言实现,可以考虑使用相关的库或工具来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值