stringfilename="", filesize, filetype;get.filenames=filename;//string tablename = "导入发货信息格式";HttpFileCollection hfc=Request.Files;
HttpPostedFile hpf=hfc[0];
filename=Path.GetFileName(hpf.FileName);
filesize=hpf.ContentLength.ToString();
filetype=hpf.ContentType;//Response.Write(filetype);if(filename.IndexOf(".xls")>-1)
{
hpf.SaveAs(Server.MapPath("uplode/")+filename);//Response.Write(filename + "
" + filesize + "
" + filetype);//RegisterStartupScript("dd", "");//读取上传的数据表格在页面上显示出来stringstrCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("uplode"+"\\"+filename)+"; Extended Properties=Excel 8.0;";
OleDbConnection con=newOleDbConnection(strCon);
con.Open();//获好表格默认第一个表名DataTable dt=con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);stringtableNamess=dt.Rows[0][2].ToString().Trim();
OleDbDataAdapter sdr=newOleDbDataAdapter("select * from ["+tableNamess+"]", con);
DataSet ds=newDataSet();
sdr.Fill(ds);
GridView1.DataSource=ds.Tables[0];
GridView1.DataBind();
con.Close();
ds.Dispose();
sdr.Dispose();
用的GridView控件显示数据,如果数据太多的话,存在分页问题。
一下是别人介绍的方法:具体可行没试过
方法1:
你可以自定义分页
采用这个方法
myAdapter.Fill(ds, startRecord, PageItem, "Paging");
方法二:
方法三:代码
/p>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
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;
}
string OutPut = "";
for (int i = LeftNum; i <= RightNum; i++)
{
if (i == CurrentPage)
{
OutPut += " " + i.ToString() + " ";
}
else
{
OutPut += " " + i.ToString() + " ";
}
}
if (CurrentPage > 1)
{
OutPut = " 首页 上一页 " + OutPut;
}
if (CurrentPage < PageCount)
{
OutPut += " 下一页 末页 ";
}
Pager.InnerHtml = OutPut ;
}
孟宪会的Excel分页测试