| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 706 人关注过本帖
标题:一段实现将数据导出到EXCEL中的代码,不完整,,求补充!
取消只看楼主 加入收藏
zsqbctw
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2008-5-8
收藏
 问题点数:0 回复次数:0 
一段实现将数据导出到EXCEL中的代码,不完整,,求补充!
以下这段是将GirdView的数据导出到Excel的代码,,在网上查到的。初学,不知道前后应如何补充使其完整,,求高手帮我补充一下。
 this.GridView1.DataBind();

    Excel.ApplicationClass oExcel = new Excel.ApplicationClass();
   object oMissing  = System.Reflection.Missing.Value;
            
   oExcel.Workbooks.Add(oMissing);
   Excel.Workbook oBook = oExcel.Workbooks[1];
   Excel.Worksheet oSheet = (Excel.Worksheet)oBook.Sheets[1];
   oSheet.Name = this.Title;
           
   Excel.Range rg;

   for (int j = 0; j < this.GridView1.HeaderRow.Cells.Count; j++)
  {
      rg = ((Excel.Range)oSheet.Cells[ 1, j + 1]);
      rg.FormulaR1C1 = this.GridView1.HeaderRow.Cells[j].Text;
    }

    for(int i = 0;i<this.GridView1.Rows.Count;i++)
  {
      for (int j = 0; j < this.GridView1.Rows[0].Cells.Count; j++)
    {
       rg = ((Excel.Range)oSheet.Cells[i + 2, j+1]);
       rg.FormulaR1C1 = this.GridView1.Rows[i].Cells[j].Text;
      }
    }
    rg = null;

    string VirFileName = Guid.NewGuid().ToString() + ".xls";
    oBook.SaveAs(Server.MapPath(VirFileName), Excel.XlFileFormat.xlExcel9795, oMissing, oMissing, oMissing, oMissing, Excel.XlSaveAsAccessMode.xlExclusive,
    oMissing, oMissing, oMissing, oMissing, oMissing);
    oExcel.Workbooks.Close();
    oExcel.Quit();
           
    oSheet = null;
    oBook = null;
    oExcel = null;

     GC.Collect();

     Response.Redirect(VirFileName);
搜索更多相关主题的帖子: EXCEL Excel 数据 Worksheet 代码 
2008-05-28 18:11
快速回复:一段实现将数据导出到EXCEL中的代码,不完整,,求补充!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.040765 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved