怎样把repeater所有数据导出到excel中?

qq948641731 2009-03-16 12:46:49
怎样把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();
}

...全文
1106 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lijun2000 2012-08-04
  • 打赏
  • 举报
回复
有用吗?怎么调用?
weir55 2009-03-16
  • 打赏
  • 举报
回复
Workbook workbook = new Workbook();


workbook.Open(Server.MapPath("~/temp/ReportSaler.xls"));

Worksheet sheet = workbook.Worksheets[0];
Cells cells = sheet.Cells;

int RowNo = 2;
DataTable dt =
oProductItem.SalerList(Convert.ToInt32(ViewState["SupplierId"]), ViewState["OrderField"].ToString(),
ViewState["IsAsc"].ToString(), dtpStart.Value, dtpEnd.Value);
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = oProductItem.ShoppingCartProductInfo(Convert.ToInt32(dt.Rows[i]["ProductItemId"]));
string ProductNo = "";
string ProductName = "";
string CreateTime = "";
string Type = "";
string Inventory = "";
string ListPrice = "";
string DiscountPrice = "";
if (dr != null)
{
ProductNo = dr["ProductNo"].ToString();
ProductName = dr["Name"].ToString();
CreateTime = string.Format("{0:yyyy-MM-dd}", Convert.ToDateTime(dr["StartTime"].ToString()));
Type = showType(dr["ProductTypeId"].ToString().Trim());
Inventory = dr["Inventory"].ToString();
ListPrice = dr["ListPrice"].ToString();
DiscountPrice = dr["DiscountPrice"].ToString();
}

cells["A" + RowNo].PutValue(i+1);
cells["B" + RowNo].PutValue(dt.Rows[i]["Nums"].ToString());
cells["C" + RowNo].PutValue(ProductNo);
cells["D" + RowNo].PutValue(ProductName);
cells["E" + RowNo].PutValue(CreateTime);
cells["F" + RowNo].PutValue(Type);
cells["G" + RowNo].PutValue(Inventory);
cells["H" + RowNo].PutValue(ListPrice);
cells["I" + RowNo].PutValue(DiscountPrice);
RowNo++;

}

string filename =
string.Format("{0}{1}.xls", "saleranklist", Convert.ToDateTime(DateTime.Now).ToString("yyyyMMdd"));

Response.ContentType = "application/ms-excel";


Response.AddHeader("content-disposition", "attachment; filename=" + filename);


System.IO.MemoryStream memStream = workbook.SaveToStream();

Response.BinaryWrite(memStream.ToArray());

Response.End();
灵雨飘零 2009-03-16
  • 打赏
  • 举报
回复
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>");
}
嘴哥臭鼬 2009-03-16
  • 打赏
  • 举报
回复
传入所有数据再导呗。
周公 2009-03-16
  • 打赏
  • 举报
回复
要到处所有页的数据,你就绑定所有的数据再导出不就行了?
wuyq11 2009-03-16
  • 打赏
  • 举报
回复
通过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

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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