将数据从网格视图导出到Windows应用程序中的Excel [英] exporting data from grid view to excel in windows application

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

问题描述

大家好。

im将数据从网格视图导出到excel。

虽然它工作得很好但是花了太多时间导出。

有没有其他更快的出口方式。?

如果有的话请帮助我。



给出代码。



hello all.
i m exporting data from grid view to excel.
though its working perfectly but its taking too much time to export.
is there any other faster way to export.?
if so please help me.

giving codes.

private void btnExport_Click(object sender, EventArgs e)
        {
            
            Cursor.Current = Cursors.WaitCursor;
            lblWait.Visible = true;

            try
            {
                if (rbAll.Checked == true)
                {
                    filename = "All Record_"+divisionname;
                }
                else
                {
                    filename = divisionname +"-" + dtSearchDate.Value.ToString("d-MM-yyy");
                }
                if (dt.Rows.Count > 0)
                {
                    oXL = new Excel.Application();

                    // Set some properties
                    oXL.Visible = false;
                    oXL.DisplayAlerts = false;

                    // Get a new workbook.
                    oWB = oXL.Workbooks.Add(Missing.Value);

                    // Get the active sheet
                    oSheet = (Excel.Worksheet)oWB.ActiveSheet;
                    oSheet.Name = "Record";

                    int rowCount = 1;
                    foreach (DataRow dr in dt.Rows)
                    {
                        rowCount += 1;
                        for (int i = 1; i < dt.Columns.Count + 1; i++)
                        {
                            // Add the header the first time through
                            if (rowCount == 2)
                            {
                                oSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
                            }
                            oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
                        }
                    }

                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.FileName = filename.ToString();
                    sfd.Filter = "Excel Documents (*.xls)|*.xls";
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        ToCsV(gvDetail, sfd.FileName); // Here gvDetail is your grid view name
                        oXL.Visible = false;
                        MessageBox.Show("Export Complete..!");
                        oXL.Application.Quit();
                        lblWait.Visible = false;

//----close previous process of excel if opened----//
                        foreach (System.Diagnostics.Process process in System.Diagnostics.Process.GetProcessesByName("EXCEL"))
                        {
                            if (process.MainModule.ModuleName.ToUpper().Equals("EXCEL.EXE"))
                            {
                                process.Kill();
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("File Not Saved..!");
                        lblWait.Visible = false;
                    }
                }
                else
                {
                    MessageBox.Show("No Record Found To Export..!");
                    lblWait.Visible = false;
                }
            }
            catch
            {
                MessageBox.Show("Export Failed..!");
                clearcontrols();
                lblWait.Visible = false;
            }
        }







提前感谢




thanks in advance

推荐答案

为您找到SomeThing:

Found SomeThing For You:
string filename = "DownloadMobileNoExcel.xls";
     System.IO.StringWriter tw = new System.IO.StringWriter();
     System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
     DataGrid dgGrid = new DataGrid();
     dgGrid.DataSource = dt;
     dgGrid.DataBind();

       //Get the HTML for the control.
       dgGrid.RenderControl(hw);
       //Write the HTML back to the browser.
       //Response.ContentType = application/vnd.ms-excel;
       Response.ContentType = "application/vnd.ms-excel";
   Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
       this.EnableViewState = false;
       Response.Write(tw.ToString());
       Response.End();


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

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