首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

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

2012-09-09 
怎样把repeater所有数据导出到excel中?怎样把repeater所有数据导出到excel中?一下方法只能导出当前页的数

怎样把repeater所有数据导出到excel中?
怎样把repeater所有数据导出到excel中?一下方法只能导出当前页的数据,而不是所有页的数据?

C# code
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();        }



[解决办法]
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();
[解决办法]
通过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>"); 
}

热点排行