回 帖 发 新 帖 刷新版面

主题:C#导出到Excel的问题

怎么样将DataGridView 中的数据导入到Excel? 我看到了一些代码但是怎么写都写不对。不知道什么意思我把它发出来希望高手能帮忙解决谢谢:   try
   {
    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;";
    OleDbConnection conn = new OleDbConnection(strConn);
    conn.Open();  
    System.Data.OleDb.OleDbCommand cmd=new OleDbCommand ();
    cmd.Connection =conn;
    //cmd.CommandText ="UPDATE [sheet1$] SET 姓名='2005-01-01' WHERE 工号='日期'";
    //cmd.ExecuteNonQuery ();
    for(int i=0;i<fp2.Sheets [0].RowCount -1;i++)
    {
     if(fp2.Sheets [0].Cells[i,0].Text!="")
     {
      cmd.CommandText ="INSERT INTO [sheet1$] (工号,姓名,部门,职务,日期,时间) VALUES('"+fp2.Sheets [0].Cells[i,0].Text+ "','"+
       fp2.Sheets [0].Cells[i,1].Text+"','"+fp2.Sheets [0].Cells[i,2].Text+"','"+fp2.Sheets [0].Cells[i,3].Text+
       "','"+fp2.Sheets [0].Cells[i,4].Text+"','"+fp2.Sheets [0].Cells[i,5].Text+"')";
      cmd.ExecuteNonQuery ();
     }
    }
    conn.Close ();
    return true;
   }
   catch(System.Data.OleDb.OleDbException ex)
   {
    System.Diagnostics.Debug.WriteLine ("写入Excel发生错误:"+ex.Message );
   }
   return false;



// 这段代码好象是直接将某个数据库中的数据导入Excel,我想得到的是只把当前DataGridView 中的数据导出到Excel。乞求高手赐教!

回复列表 (共3个回复)

沙发

你的这个DOME只能对于数据库操作
如果是WEB
将DATAGRID中的数据下载到本地存为EXCEL格式 private static void WriteToExcelFromDataGrid(DataGrid dg, string fileName)
{ //StringWriter tw = new StringWriter(new CultureInfo( "zh-CHS", false)); StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
dg.RenderControl(hw);
System.Web.HttpResponse response = ystem.Web.HttpContext.Current.Response; response.Clear();
response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8"); response.ContentType ="application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
response.Charset = "gb2312";
response.Write(tw.ToString());
response.End(); }

板凳


        protected override bool Export(Infragistics.Win.UltraWinGrid.UltraGrid guid)
        {
            try
            {
                if(this.CurrentGrid==null)
                {
                    MessageBox.Show("请点击要导出的区域名!");
                }
                if(this.CurrentGrid!=null)
                {
                    string filename = @"C:\Documents and Settings\Administrator\My Documents\"+this.CurrentGrid.Text + this.CurrentGrid.DisplayLayout.Key +@".xls";
                    this.ultraGridExcelExporter.Export(this.CurrentGrid,filename);
                    System.Diagnostics.ProcessStartInfo st = new System.Diagnostics.ProcessStartInfo(filename);
                    st.WindowStyle =System.Diagnostics.ProcessWindowStyle.Maximized ; 
                    System.Diagnostics.Process.Start(st);

                }
                return true;
            }
                
            catch(System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }

3 楼

这是一个倒出当前Grid的的数据到EXCEL的代码

我来回复

您尚未登录,请登录后再回复。点此登录或注册