目的是做数据导出。我在页面上放了一个datagrid进行数据的分页显示,导出这个控件的数据能够做到
但是分页显示,只能导出当前页的数据,而且导出的格式也不好看,于是我动态创建一个datagrid,没有分页等设置,但报错 “未将对象引用设置到对象的实例”。我在页面的html中开始没有定义datagird,但我拖入一个datagrid后不进行设置还是不行。动态创建的代码:
DataGrid mDataGrid1 = new DataGrid();
mDataGrid1.DataSource = this.objDataSet.Tables[0];
mDataGrid1.DataBind();全部代码:
private void LinkButton1_Click(object sender, System.EventArgs e)
{
DataGrid mDataGrid1 = new DataGrid();
mDataGrid1.DataSource = this.objDataSet.Tables[0];
mDataGrid1.DataBind(); Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false; System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.ClearControls(mDataGrid1);
mDataGrid1.RenderControl(oHtmlTextWriter); Response.Write(oStringWriter.ToString()); Response.End();
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().GetPropert("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;
}请问如何解决?????
但是分页显示,只能导出当前页的数据,而且导出的格式也不好看,于是我动态创建一个datagrid,没有分页等设置,但报错 “未将对象引用设置到对象的实例”。我在页面的html中开始没有定义datagird,但我拖入一个datagrid后不进行设置还是不行。动态创建的代码:
DataGrid mDataGrid1 = new DataGrid();
mDataGrid1.DataSource = this.objDataSet.Tables[0];
mDataGrid1.DataBind();全部代码:
private void LinkButton1_Click(object sender, System.EventArgs e)
{
DataGrid mDataGrid1 = new DataGrid();
mDataGrid1.DataSource = this.objDataSet.Tables[0];
mDataGrid1.DataBind(); Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false; System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.ClearControls(mDataGrid1);
mDataGrid1.RenderControl(oHtmlTextWriter); Response.Write(oStringWriter.ToString()); Response.End();
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().GetPropert("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;
}请问如何解决?????
不要创建datagrid了
http://dotnet.aspx.cc/ShowDetail.aspx?id=EC5E84EC-68F9-4CD7-9E11-6F5C92027F0B
http://dotnet.aspx.cc/ShowDetail.aspx?id=8A4CBF47-B888-4832-3389-ED3A3A3C8AAB
我想导出全部数据,所以想把当前页的数据源dataset绑定到另外一个不分页
的datagrid,但不想显示出来。谢谢!
mDataGrid1.DataSource = this.objDataSet.Tables[0];
mDataGrid1.DataBind();
这里的dataset可能是空的,重新fill再绑定是可以导出的,我以前做过,不过导出数据的表头都是你数据库里的列名的,用户看不懂。
http://www.codeproject.com/aspnet/ExportClassLibrary.asp
你可以dataset 导入 excel参考
http://community.csdn.net/Expert/topic/3077/3077526.xml?temp=.8746912
http://www.dev-club.com/club/bbs/showEssence.asp?id=26350http://dev.csdn.net/Develop/article/18/18623.shtm
http://community.csdn.net/Expert/topic/3112/3112296.xml?temp=.926861
http://dotnet.aspx.cc/ShowDetail.aspx?id=BF0A54F9-C7C7-4200-BD9A-802AC1F5DE50
http://expert.csdn.net/Expert/TopicView1.asp?id=2928057www.foxhis.com/powermjtest/
原文代码:
private void Button1_Click(object sender, System.EventArgs e)
{
//写入Excel的方法:
//定义需要参数。
string SourceFile="Data.XLS"; //源文件名称。
string TemplatePath=Server.MapPath("ExcelTemplate"); //存放源文件的文件夹路径。
string DownloadPath=Server.MapPath("ExcelDownload"); //副本的文件夹路径。
//副本的文件名。
string TempFileName = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".XLS";
object missing = System.Reflection.Missing.Value;
Excel.Application myExcel=new Excel.Application();
//打开新文件
myExcel.Application.Workbooks.Open(TemplatePath+"\\"+SourceFile,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing, missing,missing);
Excel.Workbook myBook=myExcel.Workbooks[1];
Excel.Worksheet curSheet = (Excel.Worksheet)myBook.Sheets[2];
string DownloadFilePath=DownloadPath+"\\"+TempFileName;
int i=0;
while (i<=10)
{
myExcel.Cells[4+i,2]=i.ToString();
myExcel.Cells[4+i,3]=i.ToString();
myExcel.Cells[4+i,4]=i.ToString();
myExcel.Cells[4+i,5]=i.ToString();
myExcel.Cells[4+i,6]=i.ToString();
i++;
}
myBook.Saved=true;
//myBook.SaveAs(DownloadFilePath,missing,"","",false,false,Excel.XlSaveAsAccessMode.xlNoChange,1,false,missing,missing);
myBook.PrintPreview(0);
//myBook.PrintOut(missing,missing,missing,missing,missing,missing,missing,missing);
myBook.Close(false, null,null);
myExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
myBook = null;
myExcel = null;
GC.Collect();
//Response.Redirect("ExcelDownload//"+TempFileName); //下载文件
}