一 前言
二 准备
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script src="~/Scripts/html2canvas.min.js"></script>
<script src="~/Scripts/bluebird.js"></script>
三 Demo/js
@using PlatForm.ERP.Models.Reports
@using PlatForm.ERP.Models.Daily
@model WeekReportSummaryAndPlanModel
@{
var webSiteName = ViewBag.WebSiteName;
var weekName = ViewBag.WeekName;
var operateDayOrWeekReport = ViewBag.OperateDayOrWeekReport as OperateDayOrWeekReportModel;
var storeVisitorFlowRateDayOrWeekReports = ViewBag.StoreVisitorFlowRateDayOrWeekReports as IList<StoreVisitorFlowRateDayOrWeekReportModel>;
var singleProductDayOrWeekReports = ViewBag.SingleProductDayOrWeekReports as IList<SingleProductDayOrWeekReportModel>;
var weekReportSummaryAndPlan = ViewBag.WeekReportSummaryAndPlan as WeekReportSummaryAndPlanModel;
}
<style type="text/css">
td {
border: 1px solid #ccc;
}
</style>
<div style="float:right;"><input type="button" id="btn" class="k-button" value="保存为图片" /></div>
<div id="imgDiv" style="padding-top:30px;">
<div>
<table class="zTable" cellpadding="" cellspacing="" style="width:80%;text-align:center;background-color:white;">
<tr>
<td colspan="" style="font-weight:bolder;text-align:center;background-color:#87CEFA;">店铺运营基础数据</td>
</tr>
<tr>
<td style="font-weight:bolder;">日期</td>
<td style="font-weight:bolder;">成交金额</td>
<td style="font-weight:bolder;">访客数</td>
<td style="font-weight:bolder;">去年访客数</td>
<td style="font-weight:bolder;">访客数同比</td>
<td style="font-weight:bolder;">浏览量</td>
<td style="font-weight:bolder;">去年浏览量</td>
<td style="font-weight:bolder;">浏览量同比</td>
</tr>
@if (operateDayOrWeekReport != null)
{
<tr>
<td style="font-weight:bolder;" rowspan="">@operateDayOrWeekReport.WeekName</td>
<td>@operateDayOrWeekReport.DealAmount</td>
<td>@operateDayOrWeekReport.VisitorCount</td>
<td>@operateDayOrWeekReport.BeforeYearVisitorCount</td>
<td>@operateDayOrWeekReport.VisitorCountYearOnYear</td>
<td>@operateDayOrWeekReport.BrowseVolume</td>
<td>@operateDayOrWeekReport.BeforeYearBrowseVolume</td>
<td>@operateDayOrWeekReport.BrowseVolumeYearOnYear</td>
</tr>
}
<tr>
<td colspan="" style="font-weight:bolder;text-align:center;background-color:#87CEFA;">下单</td>
</tr>
<tr>
<td style="font-weight:bolder;">下单客户数</td>
<td style="font-weight:bolder;">下单单量</td>
<td style="font-weight:bolder;">下单商品件数</td>
<td style="font-weight:bolder;">下单金额</td>
</tr>
@if (operateDayOrWeekReport != null)
{
<tr>
<td>@operateDayOrWeekReport.OrderCustomerCount</td>
<td>@operateDayOrWeekReport.OrderQuantity</td>
<td>@operateDayOrWeekReport.OrderProductCount</td>
<td>@operateDayOrWeekReport.OrderAmount</td>
</tr>
}
<tr>
<td colspan="" style="font-weight:bolder;text-align:center;background-color:#87CEFA;">成交</td>
</tr>
<tr>
<td style="font-weight:bolder;">成交客户数</td>
<td style="font-weight:bolder;">成交单量</td>
<td style="font-weight:bolder;">成交商品件数</td>
<td style="font-weight:bolder;">客单价</td>
<td style="font-weight:bolder;">实际销售件数</td>
<td style="font-weight:bolder;">实际销售金额</td>
<td style="font-weight:bolder;">去年实际销售</td>
<td style="font-weight:bolder;">退货件数</td>
<td style="font-weight:bolder;">退货金额</td>
<td style="font-weight:bolder;">去年退货金额</td>
<td style="font-weight:bolder;">下单转化率</td>
<td style="font-weight:bolder;">成交转化率</td>
<td style="font-weight:bolder;">下单成交转化率</td>
</tr>
@if (operateDayOrWeekReport != null)
{
<tr>
<td>@operateDayOrWeekReport.DealCustomerCount</td>
<td>@operateDayOrWeekReport.DealQuantity</td>
<td>@operateDayOrWeekReport.DealProductCount</td>
<td>@operateDayOrWeekReport.CustomerPrice</td>
<td>@operateDayOrWeekReport.RealSaleCount</td>
<td>@operateDayOrWeekReport.RealSaleAmount</td>
<td>@operateDayOrWeekReport.BeforeYearRealSale</td>
<td>@operateDayOrWeekReport.ReturnCount</td>
<td>@operateDayOrWeekReport.ReturnAmount</td>
<td>@operateDayOrWeekReport.BeforeYearReturn</td>
<td>@operateDayOrWeekReport.OrderConversionRate</td>
<td>@operateDayOrWeekReport.ConversionRate</td>
<td>@operateDayOrWeekReport.OrderDealConversionRate</td>
</tr>
}
<tr>
<td colspan="" style="font-weight:bolder;text-align:center;background-color:#87CEFA;">费用</td>
</tr>
<tr>
<td style="font-weight:bolder;">补单佣金</td>
<td style="font-weight:bolder;">活动</td>
<td style="font-weight:bolder;">快车</td>
<td style="font-weight:bolder;">其他</td>
</tr>
@if (operateDayOrWeekReport != null)
{
<tr>
<td>@operateDayOrWeekReport.SupplementOrderCommission</td>
<td>@operateDayOrWeekReport.Activitie</td>
<td>@operateDayOrWeekReport.KuaiChe</td>
<td>@operateDayOrWeekReport.OtherFee</td>
</tr>
}
</table>
</div>
<div style="padding-top:50px;">
<table class="zTable" cellpadding="" cellspacing="" style="width:80%;text-align:center; background-color:white;">
<tr>
<td colspan="" style="font-weight:bolder;text-align:center;background-color:#87CEFA;">店铺流量</td>
</tr>
<tr>
<td style="font-weight:bolder;">日期</td>
<td style="font-weight:bolder;">来源</td>
<td style="font-weight:bolder;">访客数</td>
<td style="font-weight:bolder;">访客数占比</td>
<td style="font-weight:bolder;">上周访客数</td>
<td style="font-weight:bolder;">上周访客数占比</td>
<td style="font-weight:bolder;">去年同时期访客数</td>
<td style="font-weight:bolder;">去年同时期访客数占比</td>
<td style="font-weight:bolder;">环比(访客数)</td>
<td style="font-weight:bolder;">同比(访客数)</td>
</tr> @if (storeVisitorFlowRateDayOrWeekReports != null)
{
foreach (var item in storeVisitorFlowRateDayOrWeekReports)
{
<tr>
<td style="font-weight:bolder;">@item.WeekName</td>
<td>@item.Source</td>
<td>@item.VisitorCount</td>
<td>@item.VisitorCountProportion</td>
<td>@item.BeforeDayOrWeekVisitorCount</td>
<td>@item.BeforeDayOrWeekVisitorCountProportion</td>
<td>@item.BeforeYearVisitorCount</td>
<td>@item.BeforeYearVisitorCountProportion</td>
<td>@item.Ratio</td>
<td>@item.YearRatio</td>
</tr>
}
} </table>
</div>
<div style="padding-top:50px;">
<table class="zTable" cellpadding="" cellspacing="" style="width:80%;text-align:center; background-color:white;">
<tr>
<td colspan="" style="font-weight:bolder;text-align:center;background-color:#87CEFA;">单品</td>
</tr>
<tr>
<td style="font-weight:bolder;">货号</td>
<td style="font-weight:bolder;">日期</td>
<td style="font-weight:bolder;">实际销售件数</td>
<td style="font-weight:bolder;">实际销售金额</td>
<td style="font-weight:bolder;">退货件数</td>
<td style="font-weight:bolder;">退货金额</td>
<td style="font-weight:bolder;">搜索排名</td>
<td style="font-weight:bolder;">浏览量</td>
<td style="font-weight:bolder;">访客数</td>
<td style="font-weight:bolder;">加购人数</td>
<td style="font-weight:bolder;">加购率</td>
<td style="font-weight:bolder;">下单客户数</td>
<td style="font-weight:bolder;">下单转化率</td>
<td style="font-weight:bolder;">成交客户数</td>
<td style="font-weight:bolder;">成交商品数</td>
<td style="font-weight:bolder;">成交转化率</td>
</tr>
<tr>
@if (singleProductDayOrWeekReports != null)
{
foreach (var item in singleProductDayOrWeekReports)
{
<tr>
<td style="font-weight:bolder;">@item.StyleNo</td>
<td style="font-weight:bolder;">@(item.Time.Value.ToString("yyyy-MM-dd"))</td>
<td>@item.RealSaleCount</td>
<td>@item.RealSaleAmount</td>
<td>@item.ReturnProductCount</td>
<td>@item.ReturnProductAmount</td>
<td>@item.SearchRanking</td>
<td>@item.BrowseVolume</td>
<td>@item.VisitorCount</td>
<td>@item.AddPurchasePeopleCount</td>
<td>@item.AddPurchaseRate</td>
<td>@item.OrderCustomerCount</td>
<td>@item.OrderConversionRate</td>
<td>@item.DealCustomerCount</td>
<td>@item.DealProductCount</td>
<td>@item.DealConversionRate</td>
</tr>
}
}
</table>
</div>
<div style="padding-top:50px;">
<form id="summaryAndPlan">
<table class="zTable" cellpadding="" cellspacing="" style="width:80%;">
<tr>
<td class="adminTitle" style="font-weight:bolder;">
总结:
</td>
<td class="adminData">
<textarea id="Summary" name="Summary" style="width:600px;height:100px;">@(weekReportSummaryAndPlan==null?"":weekReportSummaryAndPlan.Summary.Trim())</textarea>
</td>
</tr>
<tr>
<td class="adminTitle" style="font-weight:bolder;">
计划:
</td>
<td class="adminData">
<textarea id="UpPlan" name="UpPlan" style="width:600px;height:100px;">@(weekReportSummaryAndPlan == null ? "" : weekReportSummaryAndPlan.UpPlan.Trim())</textarea>
</td>
</tr>
<tr>
<td class="adminData" colspan="" style="text-align: center;">
<input type="button" id="confirmPlan" class="k-button" value="确定" />
</td>
</tr>
</table>
</form>
</div>
</div>
<script>
$("#confirmPlan").click(function () {
if (confirm("确定吗?")) {
var summary =$("#@Html.FieldIdFor(x=>x.Summary)").val();
var plan =$("#@Html.FieldIdFor(x=>x.UpPlan)").val();
$.ajax({
url: "@Url.Action("WeekReportSummaryAndPlanConfirm", "Daily")",
data: { "webSiteName":"@webSiteName","summary": summary,"plan":plan,"weekName":"@weekName"},
type: "POST",
success: function (result) {
if (result.split("|")[] == "true") {
alertBox(result.split("|")[], result.split("|")[]);
} else {
alertBox(result.split("|")[], result.split("|")[]);
}
}
});
}
});
</script>
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script src="~/Scripts/html2canvas.min.js"></script>
<script src="~/Scripts/bluebird.js"></script>
<script type="text/javascript">
$(document).ready(function () {
// canvas生成图片
$("#btn").on("click", function () {
var getPixelRatio = function (context) { // 获取设备的PixelRatio
var backingStore = context.backingStorePixelRatio ||
context.webkitBackingStorePixelRatio ||
context.mozBackingStorePixelRatio ||
context.msBackingStorePixelRatio ||
context.oBackingStorePixelRatio ||
context.backingStorePixelRatio || 0.5;
return (window.devicePixelRatio || 0.5) / backingStore;
};
//生成的图片名称
var imgName = "@(webSiteName)@(weekName).jpg";
var shareContent = document.getElementById("imgDiv");
var width = shareContent.offsetWidth;
var height = shareContent.offsetHeight;
var canvas = document.createElement("canvas");
var context = canvas.getContext('2d');
var scale = getPixelRatio(context); //将canvas的容器扩大PixelRatio倍,再将画布缩放,将图像放大PixelRatio倍。
canvas.width = width * scale;
canvas.height = height * scale;
canvas.style.width = width + 'px';
canvas.style.height = height + 'px';
context.scale(scale, scale); var opts = {
scale: ,
background: '#FFFFFF'
};
window.pageYoffset = ;
document.documentElement.scrollTop = ;
document.body.scrollTop = ;
html2canvas(shareContent, opts).then(function (canvas) {
context.imageSmoothingEnabled = false;
context.webkitImageSmoothingEnabled = false;
context.msImageSmoothingEnabled = false;
context.imageSmoothingEnabled = false;
var dataUrl = canvas.toDataURL('image/jpeg', 1.0);
dataURIToBlob(imgName, dataUrl, callback);
});
});
}) // edited from https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob#Polyfill
var dataURIToBlob = function (imgName, dataURI, callback) {
var binStr = atob(dataURI.split(',')[]),
len = binStr.length,
arr = new Uint8Array(len); for (var i = ; i < len; i++) {
arr[i] = binStr.charCodeAt(i);
} callback(imgName, new Blob([arr]));
} var callback = function (imgName, blob) {
var triggerDownload = $("<a>").attr("href", URL.createObjectURL(blob)).attr("download", imgName).appendTo("body").on("click", function () {
if (navigator.msSaveBlob) {
return navigator.msSaveBlob(blob, imgName);
}
});
triggerDownload[].click();
triggerDownload.remove();
};
</script>
四 效果图
五 最后
自己记录一下,有这方面的需求,大家可以借鉴一下,不喜勿喷!