关于MVC打印问题,打印指定的内容
首先你的内容一定要放在一个div中如下代码
<div id="divprint">
<table class="table table-striped dataTable table-bordered" id="DataTables_Table_0" aria-describedby="DataTables_Table_0_info">
<thead>
<tr role="row">
<th class="sorting_asc" role="columnheader" tabindex="" aria-controls="DataTables_Table_0" rowspan="" colspan="" aria-sort="ascending" aria-label="Rendering engine: activate to sort column descending" style="width: 140px;">用户名ID</th>
<th class="sorting" role="columnheader" tabindex="" aria-controls="DataTables_Table_0" rowspan="" colspan="" aria-label="Browser: activate to sort column ascending" style="width: 180px;">用户名</th>
<th class="sorting" role="columnheader" tabindex="" aria-controls="DataTables_Table_0" rowspan="" colspan="" aria-label="Platform(s): activate to sort column ascending" style="width: 160px;">电话</th>
<th class="sorting" role="columnheader" tabindex="" aria-controls="DataTables_Table_0" rowspan="" colspan="" aria-label="Engine version: activate to sort column ascending" style="width: 140px;">真实姓名</th>
<th class="sorting" role="columnheader" tabindex="" aria-controls="DataTables_Table_0" rowspan="" colspan="" aria-label="CSS grade: activate to sort column ascending" style="width: 140px;">工号</th>
<th class="sorting" role="columnheader" tabindex="" aria-controls="DataTables_Table_0" rowspan="" colspan="" aria-label="CSS grade: activate to sort column ascending" style="width: 167px;">拥有角色</th>
</tr>
</thead> <tbody role="alert" aria-live="polite" aria-relevant="all">
@foreach (var item in Model.EntityList)
{
<tr>
<td class=" sorting_1">@item.OneUser.Uid</td>
<td class=" ">@item.OneUser.UserName</td>
<td class=" ">@item.OneUser.Tel</td>
<td class=" ">@item.OneUser.TrueName</td>
<td class=" ">@item.OneUser.WorkNo</td>
<td class=" ">
@if (item.HasRole!=null&&item.HasRole.Count>)
{
foreach (var name in item.HasRole)
{
<p>@name</p>
}
}
</td>
</tr>
} <tr class="odd">
<td class=" sorting_1">Gecko</td>
<td class=" ">Firefox 1.0</td>
<td class=" ">Win + / OSX.+</td>
<td class=" ">1.7</td>
<td class=" ">A</td>
</tr>
<tr class="even">
<td class=" sorting_1">Gecko</td>
<td class=" ">Firefox 1.5</td>
<td class=" ">Win + / OSX.+</td>
<td class=" ">1.8</td>
<td class=" ">A</td>
</tr>
</tbody>
</table>
</div>
<input type="button" id="btndy" value="打印" />
然后我们看JS代码部分:(注意:因为这里部分打印损坏了HTML代码结构,所以不管你打不打印都要重新加载当前页面,这里我们用window.location.href="/Account/UserManagerMent";或window.reload();)
$(function () {
$("#btndy").click(function () {
var headstr = "<html><head><title></title></head><body>";
var footstr = "</body>";
var newstr = $("#divprint").html(); // 获取要打印的div内容
var oldstr = document.body.innerHTML; // 将过去的body内容存到oldstr
document.body.innerHTML = headstr + newstr + footstr; // 组合打印内容
window.print(); // 弹出打印窗体
document.body.innerHTML = oldstr; // 还原HTML内容
window.location.href="/Account/UserManagerMent"; // 刷新当前页面
});
});
至于后台以前是什么样,现在还是什么样,只是刷新一下而已
小Y子的代码和我的差不多
<button onclick="doPrint()">打印</button>
<div id="pri">
111111111
</div>
<script>
function doPrint(){
var head_str = "<html><head><title></title></head><body>"; //先生成头部
var foot_str = "</body></html>"; //生成尾部
var older = document.body.innerHTML;
var new_str = document.getElementById('pri').innerHTML; //获取指定打印区域
var old_str = document.body.innerHTML; //获得原本页面的代码
document.body.innerHTML = head_str + new_str + foot_str; //构建新网页
window.print(); //打印刚才新建的网页
document.body.innerHTML = older; //将网页还原
return false;
}
</script>
关于MVC打印问题,打印指定的内容的更多相关文章
- 在ASP.NET2.0里打印网页指定的内容(比如打印网页里的一个Table)
原文:在ASP.NET2.0里打印网页指定的内容(比如打印网页里的一个Table) 打印指定内容: <html> <head> <script type= " ...
- HOW TO:使用 Visual C# .NET 打印 RichTextBox 控件的内容
概要 本分步指南介绍了如何打印 RichTextBox 控件的内容.RichTextBox 控件没有提供任何打印 RichTextBox 内容的方法.您可以扩展 RichTextBox 类以使用 EM ...
- C#如何打印RichTextBox控件的内容
本任务的内容 摘要 创建 RichTextBoxPrintCtrl 控件 测试控件 参考 概要 本分步指南介绍了如何打印 RichTextBox 控件的内容.RichTextBox 控件没有提供任 ...
- python 打印 str 字符串的实际内容 repr(str)
python 打印 str 字符串的实际内容 repr(str) s = 'aa' print(repr(s))
- C#实现打印与打印预览功能
C#实现打印与打印预览功能的思路及代码. 在windows应用程序中文档的打印是一项非常重要的功能,在以前一直是一个非常复杂的工作,Microsoft .Net Framework的打印功能都以组件的 ...
- C#教程之打印和打印预览
最近研究一了一下关于PDF打印和打印预览的功能,在此小小的总结记录一下学习过程. 实现打印和打印预览的方法,一般要实现如下的菜单项:打印.打印预览.页面设置. PrintDocument类 Print ...
- C#实现打印与打印预览功能(转)
在windows应用程序中文档的打印是一项非常重要的功能,在以前一直是一个非常复杂的工作,Microsoft .Net Framework的打印功能都以组件的方式提供,为程序员提供了很大的方便,但是这 ...
- python3获取指定目录内容的详细信息
不同平台获取指定目录内容的详细信息命令各不相同: Linux中可以通过ls -al获取获取 windows中可以通过dir命令获取 下面是我写的一个通用获取目录内容详细信息的python3脚本: #! ...
- 获取txt文件指定行内容
#!/usr/bin/python num=0; ni=open("C:\Python34\ceshi.txt") for line in ni: num=num+1; #表示行 ...
- android网页打印,安卓网页打印,h5页面打印,浏览器打印,js打印工具
Android设备打印比较麻烦,一般设备厂商都提供原生app开发的SDK,我们web开发者为难了,不会原生开发啊 给大家提供一个思路,实现web加壳,利用打印浏览器实现 简单来说就是把我们的web页面 ...
随机推荐
- poj_2479 动态规划
题目大意 给定一列数,从中选择两个不相交的连续子段,求这两个连续子段和的最大值. 题目分析 典型的M子段和的问题,使用动态规划的方法来解决. f[i][j] 表示将A[1...i] 划分为j个不相交连 ...
- 后Hadoop时代的大数据架构
提到大数据分析平台,不得不说Hadoop系统,Hadoop到现在也超过10年的历史了,很多东西发生了变化,版本也从0.x进化到目前的2.6版本.我把2012年后定义成后Hadoop平台时代,这不是说不 ...
- 构建API
前言 过程,如图: 第一步创建一个帮助类,类里面提供了加密.组装Url等方法,代码如下: using System; using System.Collections.Generic; using S ...
- 160613、MyBatis insert操作返回主键
在使用MyBatis做持久层时,insert语句默认是不返回记录的主键值,而是返回插入的记录条数:如果业务层需要得到记录的主键时,可以通过配置的方式来完成这个功能,针对Sequence主键而言,在执行 ...
- SignalR循序渐进(三)简易的集群通讯组件
上一篇演示了泛型Hub的实现,微软于6月17日更新了SignalR 2.1.0,然后自带了泛型Hub,于是就不需要自己去实现了…(微软你为啥不早一个月自带啊…).不过没关系,SignalR出彩之处不在 ...
- 20165330 2017-2018-2 《Java程序设计》第6周学习总结
课本知识总结 第八章 常用实用类 String类 String对象 构造方法: 使用String类声明对象并创建对象 String s = new String("we are studen ...
- Python 之 UUID
UUID是根据MAC以及当前时间等创建的不重复的随机字符串 import uuid # Generate a UUID from a host ID, sequence number, and the ...
- dict文档
文档 class dict(object): """ dict() -> new empty dictionary 创建字典的方式有两种: 1.dic = {} 2 ...
- java获取src下文件
方式一: InputStream in = Test.class .getResourceAsStream("/env.properties"); URL url = Test.c ...
- 《Python核心编程》第五章:数字
PS:[笔记+代码+图片]在GitHub上持续更新,欢迎star:https://github.com/gdouchufu/Core-Python-Programming 本章大纲 介绍Python支 ...