将datagrid数据导入到excel(转载)
2006-05-22 13:55
483 查看
前几天要做一个将datagrid数据倒出到excel的程序,从网上考了一点代码,解决了这个问题
private void SaveResult_Click(object sender, EventArgs e)
{
try
{
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls");
Response.ContentEncoding = Encoding.GetEncoding("GB2312");
//设置输出流为简体中文
Response.ContentType = "application/ms-excel";
//设置输出文件类型为excel文件。
this.EnableViewState = false;
CultureInfo myCItrad = new CultureInfo("ZH-CN",true);
StringWriter oStringWriter = new StringWriter(myCItrad);
HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
dgMqcLog.PagerStyle.Visible = false;
this.ClearControls(dgMqcLog);
dgMqcLog.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
后来发现,如果datagrid加了分页和排序功能,就没法正常倒出了,没办法,又去网上搜索,有为大哥说加上下面的代码可以,我也没完全弄清楚,不过还是很奏效的.相信对大家有点用
private void ClearControls(Control control)
{
for(int i=control.Controls.Count -1; i>=0; i--)
{
ClearControls(control.Controls[i]);
}
if(!(control is TableCell))
{
if(control.GetType().GetProperty("SelectedItem") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
try
{
literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control,null);
}
catch
{
}
control.Parent.Controls.Remove(control);
}
else if(control.GetType().GetProperty("Text") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control,null);
control.Parent.Controls.Remove(control);
}
}
return;
}
private void SaveResult_Click(object sender, EventArgs e)
{
try
{
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls");
Response.ContentEncoding = Encoding.GetEncoding("GB2312");
//设置输出流为简体中文
Response.ContentType = "application/ms-excel";
//设置输出文件类型为excel文件。
this.EnableViewState = false;
CultureInfo myCItrad = new CultureInfo("ZH-CN",true);
StringWriter oStringWriter = new StringWriter(myCItrad);
HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
dgMqcLog.PagerStyle.Visible = false;
this.ClearControls(dgMqcLog);
dgMqcLog.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
后来发现,如果datagrid加了分页和排序功能,就没法正常倒出了,没办法,又去网上搜索,有为大哥说加上下面的代码可以,我也没完全弄清楚,不过还是很奏效的.相信对大家有点用
private void ClearControls(Control control)
{
for(int i=control.Controls.Count -1; i>=0; i--)
{
ClearControls(control.Controls[i]);
}
if(!(control is TableCell))
{
if(control.GetType().GetProperty("SelectedItem") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
try
{
literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control,null);
}
catch
{
}
control.Parent.Controls.Remove(control);
}
else if(control.GetType().GetProperty("Text") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control,null);
control.Parent.Controls.Remove(control);
}
}
return;
}
相关文章推荐
- excel数据导入到datagrid
- 将DataGrid中的数据完全导入到EXCEL中
- Java实现Excel导入数据库,数据库中的数据导入到Excel。。转载 自学资料总结 实现的功能: Java实现Excel导入数据库,如果存在就更新 数据库中的数据导入到Excel 1、
- c#将Excel数据导入到数据库的实现代码(转载)
- DataGrid数据导入Excel
- DataGrid中的数据导入到Word和Excel_C#
- [转载]利用SQL Server的DTS操作EXCEL、Access等数据表的导入导出
- 转载Java导入excel数据到数据库。
- DataGrid的数据导入到Excel或CSV
- 怎样将DataGrid中的数据导入Excel中
- 转载:如何将Excel数据批量导入SQL数据库
- excel数据导入到datagrid
- 利用phpExcel实现Excel数据的导入导出(全步骤详细解析)(转载粘贴的)
- DataGrid数据导入excel或word
- Excel数据导入DataGrid中
- Excel导入easyui dataGrid数据批量保存新思路
- [VB.NET]救助:将datagrid中的数据导入到excel中时遇到错误??
- DataGrid的数据导入到Excel或CSV
- 将EXCEL 数据导入到 DataGrid中
- EXTJS学习系列提高篇:第二篇(转载)作者殷良胜,结合EXT2.2+C#.net实现将数据导入Excel的功能