欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

js导出html到word

程序员文章站 2024-03-20 15:29:34
...

1.下载两个js

FileSaver.js    jquery.wordexport.js 网上有,我就不发了,哈哈

2.在html页面引入js

注意FileSaver.js要放在jquery.wordexport.js的上面,像这样

<script src="/js/FileSaver.js"></script>
<script src="/js/jquery.wordexport.js"></script>

3.添加一个按钮的点击事件


<input type="button" id="export" value="导出word"/>
<div id="export_word">
//要导出的部分
</div>

$("#export").click(function () {
   $("#export_word").wordExport("导出word的名字");
});

现在就可以导出word啦,不过现在word中只可以导出css内联样式,外联样式导出不了

4.导出css样式

以下是jquery.wordexport.js中修改的部分,多传了一个rule参数,在下面把styles赋值为rule,注意修改的地方一共有两处。

$.fn.wordExport = function(fileName,rule) {
            fileName = typeof fileName !== 'undefined' ? fileName : "jQuery-Word-Export";
            var static = {
                mhtml: {
                    top: "Mime-Version: 1.0\nContent-Base: " + location.href + "\nContent-Type: Multipart/related; boundary=\"NEXT.ITEM-BOUNDARY\";type=\"text/html\"\n\n--NEXT.ITEM-BOUNDARY\nContent-Type: text/html; charset=\"utf-8\"\nContent-Location: " + location.href + "\n\n<!DOCTYPE html>\n<html>\n_html_</html>",
                    head: "<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n<style>\n_styles_\n</style>\n</head>\n",
                    body: "<body>_body_</body>"
                }
            };
            var options = {
                maxWidth: 624
            };
            // Clone selected element before manipulating it
            var markup = $(this).clone();

            // Remove hidden elements from the output
            markup.each(function() {
                var self = $(this);
                if (self.is(':hidden'))
                    self.remove();
            });

            // Embed all images using Data URLs
            var images = Array();
            var img = markup.find('img');
            for (var i = 0; i < img.length; i++) {
                // Calculate dimensions of output image
                var w = Math.min(img[i].width, options.maxWidth);
                var h = img[i].height * (w / img[i].width);
                // Create canvas for converting image to data URL
                var canvas = document.createElement("CANVAS");
                canvas.width = w;
                canvas.height = h;
                // Draw image to canvas
                var context = canvas.getContext('2d');
                context.drawImage(img[i], 0, 0, w, h);
                // Get data URL encoding of image
                var uri = canvas.toDataURL("image/png");
                $(img[i]).attr("src", img[i].src);
                img[i].width = w;
                img[i].height = h;
                // Save encoded image to array
                images[i] = {
                    type: uri.substring(uri.indexOf(":") + 1, uri.indexOf(";")),
                    encoding: uri.substring(uri.indexOf(";") + 1, uri.indexOf(",")),
                    location: $(img[i]).attr("src"),
                    data: uri.substring(uri.indexOf(",") + 1)
                };
            }

            // Prepare bottom of mhtml file with image data
            var mhtmlBottom = "\n";
            for (var i = 0; i < images.length; i++) {
                mhtmlBottom += "--NEXT.ITEM-BOUNDARY\n";
                mhtmlBottom += "Content-Location: " + images[i].location + "\n";
                mhtmlBottom += "Content-Type: " + images[i].type + "\n";
                mhtmlBottom += "Content-Transfer-Encoding: " + images[i].encoding + "\n\n";
                mhtmlBottom += images[i].data + "\n\n";
            }
            mhtmlBottom += "--NEXT.ITEM-BOUNDARY--";

            //TODO: load css from included stylesheet
            var styles = rule || "";

然后html页面中的js修改为

        //传入css样式
        var rules = "",
            ss = document.styleSheets;
        for (var i = 0; i < ss.length; ++i) {
            for (var x = 0; x < ss[i].cssRules.length; ++x) {
                rules += ss[i].cssRules[x].cssText;
            }
        }
        $("#export").click(function () {
            $("#export_word").wordExport("导出word的名字",rules);
        }

这样就可以引入css了!