.net webForm 中gridview分页如何打印?

syh661 2009-12-30 12:46:33
.net webForm 中gridview分页如何打印?最好给个DEMO
...全文
154 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
JhonnyHui 2009-12-30
  • 打赏
  • 举报
回复
方法二:传给另一个页面
protected void btnExcel_Click(object sender, EventArgs e)
{
#region 导入Excel方法2
Session["grid"] = this.gvManualSign;
//Server.Transfer("ExportExcel.aspx");

Response.Redirect("ExportExcel.aspx");


#endregion

}


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExportExcel.aspx.cs" Inherits="ManualSign_ExportExcel" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width: 253px" id="table" runat="server">
</table>
</div>
</form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ManualSign_ExportExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
#region 导入Excel方法2
TranslateGridToTable(Session["grid"] as GridView, table,"员工考勤统计");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");//解决汉字乱码问题
Response.ContentType = "application/vnd.ms-excel"; //指定导出文件格式
//注意:这里能导出数据的元素不能是表单元素并且标签数据内容不能为空:如table为<table> 并且有数据

HtmlTextWriter hw = new HtmlTextWriter(Response.Output);

this.table.RenderControl(hw); //指定需要导出的页面元素的内容到响应流

Response.End();//导出指定页面元素的内容
// Response.Output.Flush();//导出整个页面内容




#endregion
}
#region 建Excel表
private void TranslateGridToTable(GridView grid, HtmlTable table,string tableHead)
{
table.Rows.Clear();
HtmlTableRow title = new HtmlTableRow();
HtmlTableCell caption = new HtmlTableCell();
caption.ColSpan = grid.HeaderRow.Cells.Count;
caption.Align = "center";
caption.InnerHtml = tableHead;
caption.BgColor = "#ddffdd";
title.Cells.Add(caption);
table.Rows.Add(title);


//设置列名
HtmlTableRow head = new HtmlTableRow();
foreach (TableCell cell in grid.HeaderRow.Cells)
{
HtmlTableCell td = new HtmlTableCell();
td.InnerText = cell.Text;
head.Cells.Add(td);

}
table.Rows.Add(head);
//添加行内容
foreach (GridViewRow row in grid.Rows)
{
HtmlTableRow tr = new HtmlTableRow();
foreach (TableCell cell in row.Cells)
{
HtmlTableCell td = new HtmlTableCell();
td.InnerText = cell.Text;
tr.Cells.Add(td);

}
table.Rows.Add(tr);
}

}
#endregion
}
JhonnyHui 2009-12-30
  • 打赏
  • 举报
回复
//string path = Server.MapPath("~/DownLoad");
//File.Copy(path + "\\Temp.xls", path + "\\ManualSign.xls",true);
//string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + "\\ManualSign.xls;Extended Properties=Excel 8.0";
//OleDbConnection con = new OleDbConnection(strConn);
//con.Open();
//OleDbCommand command = new OleDbCommand();
//OleDbTransaction trans = con.BeginTransaction();
//command.Connection = con;
//List<string> list = new List<string>();
//IList<UserInfo> listU = Display();
//foreach(UserInfo row in listU)
//{
// DateTime startTime = Convert.ToDateTime(txtBeginTime.Text + " 0:00:00");
// DateTime endTime = Convert.ToDateTime(txtEndTime.Text + " 23:59:59");
// string userId = row.UserId;
// UserInfo user = userInfoManager.GetUserById(userId);
// string userName = user.UserName;
// string attendances = manualSignManager.GetUserAttendances(user.UserId, startTime, endTime);
// string subAttendances=attendances.Substring(0,attendances.Length -1);
// string arriveLates = manualSignManager.GetUserArriveLates(user.UserId, startTime, endTime).ToString();
// string leaveEarlys = manualSignManager.GetUserLeaveEarlys(user.UserId, startTime, endTime).ToString();
// string absences = manualSignManager.GetUserAbsences(user.UserId, startTime, endTime).ToString();
// string departName = user.DepartInfo.DepartName;
// string branchName = user.DepartInfo.BranchInfo.BranchName;

// string sql = "insert into [出勤表$] (姓名,出勤,迟到次数,早退次数,矿工次数,所属部门,所属机构)";
// sql += " values('" + userName + "'," + subAttendances + ",'" + arriveLates + "','" + leaveEarlys + "','" + absences + "','" + departName + "','" + branchName + "')";

// list.Add(sql);
//}
//try
//{
// command.Transaction = trans;
// foreach (string sql in list)
// {
// command.CommandText = sql;
// command.ExecuteNonQuery();
// }
// trans.Commit();
// con.Close();
// // ScriptManager.RegisterClientScriptBlock(this,this.GetType(),"a","alert(导入xls成功)",true);
//}
//catch (System.Data.OleDb.OleDbException ex)
//{
// trans.Rollback();
// con.Close();
// throw ex;
//}
lirenniao 2009-12-30
  • 打赏
  • 举报
回复
结贴太少

62,254

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧