private int Inum = 1; //行号,规定其索引初始值为1
int pagesize = 2;//每页的数据条数
int pagecount = 0;//共有几页
string file = "";
int allCount = 0;//共有几条
DataTable dw = new DataTable();
private void view_bt_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Multiselect = true;
fileDialog.Title = "请选择文件";
fileDialog.Filter = "所有文件(*.*)|*.*";//判断文件类型
if (fileDialog.ShowDialog() == DialogResult.OK)
{
file = fileDialog.FileName;
//MessageBox.Show("已选择文件:" + file, "选择文件提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
if (MessageBoxButtons.OK.ToString().Equals("OK"))
{
filePath_text.Text = file;
}
ReadFromExcelFileService readFromExcelFileService = new ReadFromExcelFileService();
dw = readFromExcelFileService.GetData(file);//调用GetData方发写上Excel文件所在的路径,这样就能获取到Excel表里面的数据了然后我们用个集合把读取到数据添加进去
show(1, pagesize);
}
private void UploadDelivery_Load(object sender, EventArgs e)
{
}
public DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize)//PageIndex表示第几页,PageSize表示每页的记录数
{
if (PageIndex == 0)
return dt;//0页代表每页数据,直接返回
DataTable newdt = dt.Copy();
newdt.Clear();//copy dt的框架
int rowbegin = (PageIndex - 1) * PageSize;
int rowend = PageIndex * PageSize;
if (rowbegin >= dt.Rows.Count)
return newdt;//源数据记录数小于等于要显示的记录,直接返回dt
if (rowend > dt.Rows.Count)
rowend = dt.Rows.Count;
for (int i = rowbegin; i <= rowend - 1; i++)
{
DataRow newdr = newdt.NewRow();
DataRow dr = dt.Rows[i];
foreach (DataColumn column in dt.Columns)
{
newdr[column.ColumnName] = dr[column.ColumnName];
}
newdt.Rows.Add(newdr);
}
return newdt;
}
private void show(int start,int end)
{
allCount = dw.Rows.Count; //获取数据表中记录的个数
pagecount = allCount % pagesize; //页数 = 总数/显示的行数
int pageNo = 0;
if (allCount % pagesize == 0)
{
pageNo = allCount / pagesize;
}
else
{
pageNo = allCount / pagesize + 1;
}
LB_showPage.Text = "共 " + pageNo + " 页" + " 当前第 " + start + " 页";
//只能显示一页,判断是否是整除
if (pagecount == 0)
{
pagecount = allCount / pagesize;
}
else
{
pagecount = allCount / pagesize + 1;
}
DataTable dt = GetPagedTable(dw, start, end);
List<ExpressBill> list = new List<ExpressBill>();
for (int i = 0; i < dt.Rows.Count; i++)
{
ExpressBill bill = new ExpressBill();
bill.ExpressNum = dt.Rows[i][0].ToString();
bill.SenderCity = dt.Rows[i][1].ToString();
bill.Sender = dt.Rows[i][2].ToString();
bill.SenderPhone = dt.Rows[i][3].ToString();
bill.SenderAddress = dt.Rows[i][4].ToString();
bill.RecipientCity = dt.Rows[i][5].ToString();
bill.Recipient = dt.Rows[i][6].ToString();
bill.RecipientPhone = dt.Rows[i][7].ToString();
list.Add(bill); //这里就把读取的数据添加到集合了
}
dataGridView.DataSource = list;
// 隐藏数据列 以及重命名 列名
for (int n = 0; n < dataGridView.ColumnCount; n++)
{
if (dataGridView.Columns[n].DataPropertyName.Equals("BillId"))
{
dataGridView.Columns[n].Visible = false;
dataGridView.Columns[n].HeaderText = "面单号主键";
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("ExpressNum"))
{
dataGridView.Columns[n].HeaderText = "单号";
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("SenderCity"))
{
dataGridView.Columns[n].HeaderText = "寄件城市";
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("Sender"))
{
dataGridView.Columns[n].HeaderText = "寄件人";
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("SenderPhone"))
{
dataGridView.Columns[n].HeaderText = "寄件手机";
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("SenderAddress"))
{
dataGridView.Columns[n].HeaderText = "寄件地址";
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("RecipientCity"))
{
dataGridView.Columns[n].HeaderText = "收件城市";
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("Recipient"))
{
dataGridView.Columns[n].HeaderText = "收件人";
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("RecipientAddress"))
{
dataGridView.Columns[n].HeaderText = "收件人地址";
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("RecipientPhone"))
{
dataGridView.Columns[n].HeaderText = "收件电话";
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("TemplateId"))
{
dataGridView.Columns[n].HeaderText = "模板主键";
dataGridView.Columns[n].Visible = false;
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("CollectPoint"))
{
dataGridView.Columns[n].HeaderText = "揽件点";
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("Collecter"))
{
dataGridView.Columns[n].HeaderText = "揽件人";
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("CollecterPhone"))
{
dataGridView.Columns[n].HeaderText = "揽件人手机";
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("CollectTime"))
{
dataGridView.Columns[n].HeaderText = "揽件时间";
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("PackageName"))
{
dataGridView.Columns[n].HeaderText = "包裹品名";
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("PackageWeight"))
{
dataGridView.Columns[n].HeaderText = "包裹重量";
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("FileName"))
{
dataGridView.Columns[n].HeaderText = "文件名";
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("UserId"))
{
dataGridView.Columns[n].HeaderText = "用户主键";
dataGridView.Columns[n].Visible = false;
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("ExpressPoint"))
{
dataGridView.Columns[n].HeaderText = "快递网点";
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("ExpressCompany"))
{
dataGridView.Columns[n].HeaderText = "快递公司";
}
else if (dataGridView.Columns[n].DataPropertyName.Equals("OperateTime"))
{
dataGridView.Columns[n].HeaderText = "操作时间";
}
}
}
private void LLB_First_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Inum = 1;
show(Inum, pagesize);
}
private void LLB_Last_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Inum = pagecount;
show(Inum, pagesize);
}
private void LLB_Next_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Inum++;
if (Inum <= pagecount) //如果没有超出记录行数
{
show(Inum, pagesize);
}
else
{
MessageBox.Show("现已是最后一页记录");
Inum = pagecount; //更改为 Inum = pagecount;
return;
}
}
private void LLB_Previous_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Inum--;
if (Inum > 0) //如果当前不是首记录
{
show(Inum, pagesize); //显示上一页记录
}
else
{
MessageBox.Show("现已是第一页记录");
Inum = 1;
return;
}
}
C#excel 文件上传并进行分页
最新推荐文章于 2021-06-03 05:48:24 发布