| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 706 人关注过本帖
标题:一段实现将数据导出到EXCEL中的代码,不完整,,求补充!
只看楼主 加入收藏
zsqbctw
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2008-5-8
收藏
 问题点数:0 回复次数:1 
一段实现将数据导出到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
imbug
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-8-17
收藏
得分:0 
送楼主参考一下吧。

 public void DataExcelResult()
        {
            string str = null;
            string tempStr = null;
            try
            {
                if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
                    return;

                Stream myStream = saveFileDialog.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312")); //gb2312

                try
                {
                    //写标题
                    for (int i = 0; i < dataGridPlay.ColumnCount; i++)
                    {
                        str += dataGridPlay.Columns[i].HeaderText;
                        if (i >=0)  
                        {
                            str += "\t";
                        }           
                    }

                    sw.WriteLine(str);
                    //写内容

                    for (int j = 0; j < dataGridPlay.Rows.Count - 1; j++)
                    {
                        tempStr = "";

                        for (int k = 0; k < dataGridPlay.Columns.Count; k++)
                        {
                            tempStr += dataGridPlay.Rows[j].Cells[k].Value.ToString();

                            if (k >= 0)         
                            {
                                tempStr += "\t";
                            }
                        }
                        sw.WriteLine(tempStr);
                    }
                    sw.Close();
                    myStream.Close();
                    MessageBox.Show("恭喜你,导出成功","导出信息");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("写入出错" + ex.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("你的格式错误。。如。。*.xls或*.txt或*.doc" + ex.ToString());
            }
        }
2008-05-29 21:34
快速回复:一段实现将数据导出到EXCEL中的代码,不完整,,求补充!
数据加载中...
 
   



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

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