怎样把repeater所有数据导出到excel中?
怎样把repeater所有数据导出到excel中?一下方法只能导出当前页的数据,而不是所有页的数据?
public override void VerifyRenderingInServerForm(System .Web .UI .Control control) { } protected void btnPrint_Click(object sender, EventArgs e) { System.IO.StringWriter sw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw); this.Productlists.RenderControl(hw); Response.Clear(); Response.ContentType = "application/vnd.ms-excel"; Response.Charset = ""; Productlists.Page.EnableViewState = true; Response.AppendHeader("Content-Disposition", "attachment;filename=Teacher.xls"); Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=GB2312\"><title> Copyright by SDU</title></head><body><center>"); Response.Write(sw.ToString()); Response.Write("</center></body></html>"); Response.End(); }
Response.ContentType = "application/ms-excel";
Response.AddHeader("content-disposition", "attachment; filename=" + filename);
System.IO.MemoryStream memStream = workbook.SaveToStream();
Response.BinaryWrite(memStream.ToArray());
Response.End();
[解决办法]
通过excel模板,导出数据源如dataset,datataable导出到 excel
public void INSERT_Excel(DataView dvs,string strPath)
{
string s="";
Excel.Application app=new Application();
Excel._Workbook book;
Excel._Worksheet sheet;
book=(Excel._Workbook)app.Workbooks.Open(strPath,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,
Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
sheet=(Excel._Worksheet)book.Sheets[1];
int j=dvs.Count;
for(int i=0;i<dvs.Count;i++)
{
try
{
s=Convert.ToString(i);
sheet.Cells[i+3,"A"]="";sheet.Cells[i+3,"B"]="";
}
catch(Exception ex)
{
HttpContext.Current.Response.Write("<script language='javascript'>alert('"+ex.Message+"')</script>");
book.Close(null,null,null);
app.Workbooks.Close();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
sheet=null;
book=null;
app=null;
GC.Collect();
HttpContext.Current.Response.Write("<script language='javascript'>alert('导出失败!')</script>");
return;
}
}
book.Save();
book.Close(null,null,null);
app.Workbooks.Close();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
sheet=null;
book=null;
app=null;
GC.Collect();
GC.Collect();
GC.Collect();
HttpContext.Current.Response.Write("<script language='javascript'>alert('导出成功!')</script>");
HttpContext.Current.Response.Write("<script language='javascript'>window.open('../Template_temp/A.xls','_bank')</script>");
}
http://www.cnblogs.com/zpylh/articles/1299238.html
[解决办法]
要到处所有页的数据,你就绑定所有的数据再导出不就行了?
[解决办法]
public void INSERT_Excel(DataView dvs,string strPath)
{
string s="";
Excel.Application app=new Application();
Excel._Workbook book;
Excel._Worksheet sheet;
book=(Excel._Workbook)app.Workbooks.Open(strPath,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,
Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
sheet=(Excel._Worksheet)book.Sheets[1];
int j=dvs.Count;
for(int i=0;i <dvs.Count;i++)
{
try
{
s=Convert.ToString(i);
sheet.Cells[i+3,"A"]=""; sheet.Cells[i+3,"B"]="";
}
catch(Exception ex)
{
HttpContext.Current.Response.Write(" <script language='javascript'>alert('"+ex.Message+"') </script>");
book.Close(null,null,null);
app.Workbooks.Close();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
sheet=null;
book=null;
app=null;
GC.Collect();
HttpContext.Current.Response.Write(" <script language='javascript'>alert('导出失败!') </script>");
return;
}
}
book.Save();
book.Close(null,null,null);
app.Workbooks.Close();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
sheet=null;
book=null;
app=null;
GC.Collect();
GC.Collect();
GC.Collect();
HttpContext.Current.Response.Write(" <script language='javascript'>alert('导出成功!') </script>");
HttpContext.Current.Response.Write(" <script language='javascript'>window.open('../Template_temp/A.xls','_bank') </script>");
}