从数据网格导出Excel [英] Export Excel from Data Grid

查看:91
本文介绍了从数据网格导出Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好大师的

我试图从DataGrid导出Excel,在Microsoft中找到此示例,然后单击按钮实现.

但不确定如何在此处调用我的数据集.

Hello Guru''s

i was trying to export Excel from my DataGrid,found this example in microsoft and implement in my button click.

But not sure how to call my data set here.

private void button1_Click(object sender, System.EventArgs e)
        {


            Excel.Application oXL;
            Excel._Workbook oWB;
            Excel._Worksheet oSheet;
            Excel.Range oRng;
            FillExcel();
            try
            {
                //Start Excel and get Application object.
                oXL = new Excel.Application();
                oXL.Visible = true;


                //Get a new workbook.
                oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
                oSheet = (Excel._Worksheet)oWB.ActiveSheet;

                //Add table headers going cell by cell.
                oSheet.Cells[1, 1] = "No";
                oSheet.Cells[1, 2] = "Message ID";
               

                //Format A1:F1 as bold, vertical alignment = center.
                oSheet.get_Range("A1", "C1").Font.Bold = true;
                oSheet.get_Range("A1", "C1").VerticalAlignment =
                    Excel.XlVAlign.xlVAlignCenter;

                // Create an array to multiple values at once.
                string[,] saNames = new string[5, 2];
                //this.dgtbcE_Name.MappingName = "MessageID";
                // saNames[0, 0] = dgtbcE_NamedgtbcE_Name.ToString;
                saNames[0, 1] = "Smith";
                saNames[1, 0] = "Tom";
               

                //Fill A2:B6 with an array of values (First and Last Names).
                oSheet.get_Range("A2", "B6").Value2 = saNames;

                //Fill C2:C6 with a relative formula (=A2 & " " & B2).
                oRng = oSheet.get_Range("C2", "C6");
                oRng.Formula = "=A2 & \" \" & B2";

                //Fill D2:D6 with a formula(=RAND()*100000) and apply format.
                oRng = oSheet.get_Range("D2", "D6");
                oRng.Formula = "=RAND()*100000";
                oRng.NumberFormat = "$0.00";

                //AutoFit columns A:D.
                oRng = oSheet.get_Range("A1", "C1");
                oRng.EntireColumn.AutoFit();

                 //Make sure Excel is visible and give the user control
                //of Microsoft Excel''s lifetime.
                oXL.Visible = true;
                oXL.UserControl = true;
            }





private void loadPage()
		{
            
            
			string strSql = "";
			int intSkip = 0;

			intSkip = (this.mintCurrentPage * this.mintPageSize);

			string ItemList = SearchList.SelectedItem.ToString();
            

            //Return All rows and columns in Message Trace Table
            if (ItemList == "ALL")
            {
                
                strSql = "SELECT Top " + this.mintPageSize +
                                  " * FROM tblTraceMessages WHERE MessageId NOT IN " +
                                  "(SELECT TOP " + intSkip + " ID FROM tblTraceMessages)";
            }
	SqlCommand cmd = this.mcnSample.CreateCommand();
			cmd.CommandText = strSql;
            
			SqlDataAdapter da = new SqlDataAdapter(cmd);
			
			DataSet ds = new DataSet();
			da.Fill(ds, "tblTraceMessages");

// Populate Data Grid
			this.dgEmp.DataSource = ds.Tables["tblTraceMessages"].DefaultView;

			// Show Status
			this.lblStatus.Text = (this.mintCurrentPage + 1).ToString() + " / " + this.mintPageCount.ToString();
 
			cmd.Dispose();
			da.Dispose();
			ds.Dispose();

推荐答案

0.00; //自动调整列A:D. oRng = oSheet.get_Range("A1","C1"); oRng.EntireColumn.AutoFit(); //确保Excel是可见的并授予用户控制权 //Microsoft Excel的生命周期. oXL.Visible = true; oXL.UserControl = true; }
0.00"; //AutoFit columns A:D. oRng = oSheet.get_Range("A1", "C1"); oRng.EntireColumn.AutoFit(); //Make sure Excel is visible and give the user control //of Microsoft Excel''s lifetime. oXL.Visible = true; oXL.UserControl = true; }





private void loadPage()
		{
            
            
			string strSql = "";
			int intSkip = 0;

			intSkip = (this.mintCurrentPage * this.mintPageSize);

			string ItemList = SearchList.SelectedItem.ToString();
            

            //Return All rows and columns in Message Trace Table
            if (ItemList == "ALL")
            {
                
                strSql = "SELECT Top " + this.mintPageSize +
                                  " * FROM tblTraceMessages WHERE MessageId NOT IN " +
                                  "(SELECT TOP " + intSkip + " ID FROM tblTraceMessages)";
            }
	SqlCommand cmd = this.mcnSample.CreateCommand();
			cmd.CommandText = strSql;
            
			SqlDataAdapter da = new SqlDataAdapter(cmd);
			
			DataSet ds = new DataSet();
			da.Fill(ds, "tblTraceMessages");

// Populate Data Grid
			this.dgEmp.DataSource = ds.Tables["tblTraceMessages"].DefaultView;

			// Show Status
			this.lblStatus.Text = (this.mintCurrentPage + 1).ToString() + " / " + this.mintPageCount.ToString();
 
			cmd.Dispose();
			da.Dispose();
			ds.Dispose();


将DataGrid导出到Excel 1 [ ^ ]
将DataGrid导出到Excel 2 [
Export DataGrid to Excel 1[^]
Export DataGrid to Excel 2[^]


这篇关于从数据网格导出Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆