通过mysql导出Gridview数据获取 [英] Export Gridview data fetch by mysql

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

问题描述

我将gridview数据导出到excel中,但是不幸的是,导出文件中的数据不同,应该是数据表.

hi i exported gridview data into excel but unfortunately the data inside the exported file was different,supposed to be a datatable.

下面是我的脚本在导出"按钮中,您能告诉我我的脚本有什么问题吗?我是ASP.net的新用户,谢谢

Below is my script in export button, can you tell me what is wrong in my script. I am new in ASP.net thanks

try
{
    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
    excel.Visible = true;
    Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Add(System.Reflection.Missing.Value);
    Microsoft.Office.Interop.Excel.Worksheet sheet1 = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
    int StartCol = 1;
    int StartRow = 1;
    int j = 0, i = 0;

    //Write Headers
    for (j = 0; j < GridView1.Columns.Count; j++)
    {
        Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)sheet1.Cells[StartRow, StartCol + j];
        myRange.Value = GridView1.Columns[j].HeaderText;
    }

    StartRow++;

     //Write datagridview content
     for (i = 0; i < GridView1.Rows.Count; i++)
     {
         for (j = 0; j < GridView1.Columns.Count; j++)
         {
             try
             {
                 Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)sheet1.Cells[StartRow + i, StartCol + j];
                 myRange.Value2 = GridView1.Rows[i].Cells[j].Text + ";" == null ? "" : GridView1.Rows[i].Cells[j].Text + ";";

             }
             catch
             {
                 GridView1.DataBind();
             }
         }
     }
 }
 catch (Exception ex)
 {
     MessageBox.Show(ex.ToString());

     // ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
     //  "alertMessage",
     //   "alert(ex.ToString());", true);

}

推荐答案

这是我经过漫长的旅程后才想到的,我在Excel中使用Aspose方法导出Gridview,它很简单,但功能如此强大!希望这会有所帮助,

Hi this is were i came up after long journey, i used the Aspose method on Exporting Gridview in Excel, it's simple yet so powerfull! hope this will help,

Export_button后面的代码:

Code behind Export_button:

  //Instantiate a new workbook
            Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
            //Get the first worksheet in the workbook
            Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0];
            //Import data from GridView control to fill the worksheet
            worksheet.Cells.ImportGridView(GridView1, 0, 0, new Aspose.Cells.ImportTableOptions() { IsFieldNameShown = true });
            worksheet.AutoFitColumns();
            //Send result to client in XLS format
            workbook.Save(this.Response, "export.xls", ContentDisposition.Attachment, new Aspose.Cells.XlsSaveOptions());

这篇关于通过mysql导出Gridview数据获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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