C#新手求解 增加一个按钮,实现打印GridView中的数据

huitian1621 2012-02-08 04:30:34



protected void Button4_Click(object sender, EventArgs e)
{
if (GridView1.Rows.Count == 0)
return;
Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
Excel.Application.Workbooks.Add(true);
Excel.Visible = true;

for (int i = 0; i > GridView1.Columns.Count; i++)
{
Excel.Cells[1, i + 1] = GridView1.Columns[i].HeaderText;
}
for (int i = 0; i > GridView1.Columns.Count; i++)
{
for (int j = 0; j < GridView1.Columns.Count; j++)
{
Excel.Cells[i + 2, j + 1] = GridView1.Rows[i].Cells[j].Text;
}
}
}






填充不进数据,出来的报表都是空的。。。求指导。。。
...全文
130 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
huitian1621 2012-02-08
  • 打赏
  • 举报
回复
刚发错了,代码是这样的,解决了。谢谢帮忙


protected void Button4_Click(object sender, EventArgs e)
{
if (GridView1.Rows.Count == 0)
return;
Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
Excel.Application.Workbooks.Add(true);
Excel.Visible = true;

for (int i = 0; i < GridView1.Columns.Count; i++)
{
Excel.Cells[1, i + 1] = GridView1.Columns[i].HeaderText;
}

for (int i = 0; i < GridView1.Rows.Count; i++)
{
for (int j = 0; j < GridView1.Columns.Count; j++)
{
Excel.Cells[i + 2, j + 1] = GridView1.Rows[i].Cells[j].Text;
}
}
}


huitian1621 2012-02-08
  • 打赏
  • 举报
回复
刚发错了,代码这样解决了:


protected void Button4_Click(object sender, EventArgs e)
{
if (GridView1.Rows.Count == 0)
return;
Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
Excel.Application.Workbooks.Add(true);
Excel.Visible = true;

for (int i = 0; i < GridView1.Columns.Count; i++)
{
Excel.Cells[1, i + 1] = GridView1.Columns[i].HeaderText;
}

for (int i = 0; i < GridView1.Rows.Count; i++)
{
for (int j = 0; j < GridView1.Columns.Count; j++)
{
Excel.Cells[i + 2, j + 1] = GridView1.Rows[i].Cells[j].Text;
}
}
}


huitian1621 2012-02-08
  • 打赏
  • 举报
回复
现在代码是这样:


protected void Button4_Click(object sender, EventArgs e)
{
if (GridView1.Rows.Count == 0)
return;
Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
Excel.Application.Workbooks.Add(true);
Excel.Visible = true;

for (int i = 0; i < GridView1.Columns.Count; i++)
{
Excel.Cells[1, i + 1] = GridView1.Columns[i].HeaderText;
}

for (int i = 0; i < GridView1.Rows.Count; i++)
{
for (int j = 0; j < GridView1.Columns.Count; j++)
{
Excel.Cells[i + 2, j + 1] = GridView1.Rows[i].Cells[j].Text;
}
}
}


EnForGrass 2012-02-08
  • 打赏
  • 举报
回复
for (int i = 0; i > GridView1.Rows.Count; i++)
{
for (int j = 0; j < GridView1.Columns.Count; j++)
{
Excel.Cells[i + 2, j + 1] = GridView1.Rows[i].Cells[j].Text;
}
}
huitian1621 2012-02-08
  • 打赏
  • 举报
回复
GridView中是有数据的,但是我调试的时候发现取不到数据,不知道哪里有问题
从这句for (int i = 0; i > GridView1.Columns.Count; i++)
就直接跳出去了。。。
bdmh 2012-02-08
  • 打赏
  • 举报
回复
调试,一步步确认是否存在数据

1,978

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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