我用的VS2005 我做了一个导出Excel的程序。我想把一个DatagrideView里所有的数据都导到Excel里应该怎么做。以下是我的代码,请师哥们指正。
       private void btOut_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog=new SaveFileDialog();
            saveFileDialog.Filter = "Excel|*.xls"; 
            saveFileDialog.FilterIndex=2; 
            saveFileDialog.RestoreDirectory=true;            string fName = "";
            if(saveFileDialog.ShowDialog()==DialogResult.OK) 
            { 
                if(saveFileDialog.ShowDialog()==DialogResult.OK) 
                { 
                    fName=saveFileDialog.FileName;
                    SaveFile(fName);
                  
                } 
            }
        }        private void SaveFile(string fileName)
        {            try
            {                Stream stream = File.OpenWrite(fileName);                using (StreamWriter writer = new StreamWriter(stream))
                {
                    //this.dataGridView1   这里应该怎么写
                    writer.Write("aaa");                }            }            catch (IOException ex)
            {                MessageBox.Show(ex.Message, "Simple Editor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);            }        }

解决方案 »

  1.   

    http://www.cnblogs.com/perfect/archive/2008/05/05/1183205.html
      

  2.   

       Response.Clear();
                Response.Buffer = true;
                //使用中文   
                Response.Charset = "utf-8";//"ISO-8859-13";// "utf-8";//"gb2312";
                //Response.AppendHeader("Content-Disposition", "attachment;filename=统计查询报表.xls");
                Response.AppendHeader("Content-Disposition", "attachment;   filename=" + System.Web.HttpUtility.UrlEncode("统计查询报表", System.Text.Encoding.UTF8) + ".xls");//            Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
                //设置格式为Excel   00
                Response.ContentType = "application/ms-excel";
                EnableViewState = false;
                System.IO.StringWriter sw = new System.IO.StringWriter();
                HtmlTextWriter tw = new HtmlTextWriter(sw);
                da.RenderControl(tw);
                Response.Write(tw);
                Response.End();
      

  3.   

    我是要导出!要从DataGrideView取数据然后导出!
      

  4.   

    N久以前写的,不过是asp的,参考一下吧 呵呵try
            {
                GridView1.Columns[0].Visible = false;
                GridView1.Columns[8].Visible = false;
                GridView1.Columns[9].Visible = false;
                GridView1.Columns[10].Visible = false;            Response.ClearContent();
                Response.Buffer = false;
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//防止乱码
                string style = @"<style> .text { mso-number-format:\@; } </style> ";//控制文本格式,防止0丢失
                Response.AddHeader("content-disposition", "attachment; filename=MyAddressList.xls");
                Response.ContentType = "application/excel";            StringWriter sw = new StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(sw);            GridView1.AllowPaging = false;
                GridView1.AllowSorting = false;
                Bind();            GridView1.RenderControl(htw);
                Response.Write(style);            Response.Write(sw.ToString());
                Response.End();
                GridView1.AllowPaging = true;
                GridView1.AllowSorting = true;
            }
            catch (Exception err)
            {
                string str = Server.HtmlEncode(err.Message);
                Response.Write("<script>alert('导出错误:" + str + "')</script>");
            }