ASP.NET 显示 Excel 文件的分页方法
ASPX 代码
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string xlsConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Book1.xls") + ";Extended Properties='Excel 8.0;HDR=No;'";
OleDbConnection cn = new OleDbConnection(xlsConnStr);
cn.Open();
String sql = "select Count(*) From [Sheet1$]";
OleDbCommand cmd = new OleDbCommand(sql, cn);
// 总的记录数
int TotalCount = Convert.ToInt32(cmd.ExecuteScalar());
//当前页的序号
int PageIndex = 1;
String page = Request.QueryString["Page"];
if (page == null) page = "1";
Int32.TryParse(page, out PageIndex);
if (PageIndex < 1) PageIndex = 1;
//每页显示的数量
int PageItem = 5;
int startRecord = (PageIndex - 1) * PageItem;
sql = "Select * From [Sheet1$]";
OleDbDataAdapter da = new OleDbDataAdapter(sql, cn);
DataSet ds = new DataSet();
da.Fill(ds, startRecord, PageItem, "Sheet");
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
BuildPagers(TotalCount, PageIndex, PageItem);
}
private void BuildPagers(int TotalCountRecord, int CurrentPage, int PageItem)
{
int Step = 6;
int LeftNum = 0;
int RightNum = 0;
String PageUrl = "?";
int PageCount = (int)Math.Ceiling((double)(TotalCountRecord) / PageItem);
if (CurrentPage - Step < 1)
{
LeftNum = 1;
}
else
{
LeftNum = CurrentPage - Step;
}
if (CurrentPage + Step > PageCount)
{
RightNum = PageCount;
}
else
{
RightNum = CurrentPage + Step;
}
相关新闻>>
- 发表评论
-
- 最新评论 更多>>