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了!
推荐阅读
-
js导出html到word
-
java word操作 通过字符串生成word文件,读取word内容,word转pdf、html,pdf流输出展示到页面,file转 MultipartFile
-
Ireport+JasperReport制作报表导出PDF,Word,Html,Excel
-
DIV转成图片并导出html2canvas.js
-
asp.net中如何批量导出access某表内容到word文档
-
java将后台数据库查询到的数据导出word文档当中
-
jQuery+Ajax+js请求json格式数据并渲染到html页面
-
请教大神,使用js在文本段落后插入div图片容器,结果图片却显示到段落前了,代码如下。非常感谢!_html/css_WEB-ITnose
-
简单实现兼容各大浏览器的js复制内容到剪切板_html/css_WEB-ITnose
-
Js 导出table内容到Excel的简单实例_javascript技巧