【问题标题】:How to send details of excel data in Detailview如何在Detailview中发送excel数据的详细信息
【发布时间】:2014-11-19 05:08:08
【问题描述】:

我正在开发一个应在 Gridview 中提取 excel 数据并在 Detailsview 中显示单个数据的详细信息的应用程序。这里没有使用 SQL Server 和 sqlDataSource。不过,我进退两难了。

protected void btnViewDetail_Click(object sender, EventArgs e) { { // What to write in here? } }

我在 GridView 中有一个名为“查看详细信息”的按钮,它应该将我重定向到包含所选数据详细信息的 Detailsview。

谢谢。

【问题讨论】:

    标签: c# asp.net excel gridview detailsview


    【解决方案1】:

    使用此方法导出datagridview到excel

    参数 -> excel 路径和网格名称

    public void exportExcel(string path, DataGridView dgv)
                {
                   try
                    {
                         Microsoft.Office.Interop.Excel._Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
                         ExcelApp.Application.Workbooks.Add(Type.Missing);
                        ExcelApp.Columns.ColumnWidth = 10;
    
    
                         for (int j = 0; j < dgv.ColumnCount; j++)
                         {
                             ExcelApp.Cells[1, j + 1] = dgv.Columns[j].HeaderText;
                         }
                         Microsoft.Office.Interop.Excel.Range headerColumnRange = ExcelApp.get_Range("A1", "Z1");
                         headerColumnRange.Font.Bold = true;
                         headerColumnRange.Font.Color = 0xFF0000;
    
                         for (int i = 1; i < dgv.Rows.Count; i++)
                         {
                             //DataGridViewRow row = dataGridView1.Rows[i];
                             for (int j = 0; j < dgv.ColumnCount; j++)
                             {
                                 ExcelApp.Cells[i + 1, j + 1] = dgv.Rows[i - 1].Cells[j].Value.ToString();
                             }
                         }
                         ExcelApp.ActiveWorkbook.SaveCopyAs(path);
                         ExcelApp.ActiveWorkbook.Saved = true;
                         ExcelApp.Quit();
                     }
                     catch { }
                 }
    

    【讨论】:

      【解决方案2】:

      您可以使用EasyXLS Excel library 从 Excel 文件中提取数据。 Excel 可以导入 DataSet,不需要 SQL Server。

      // Create an instance of the class that imports Excel files
      ExcelDocument xls = new ExcelDocument();
      
      // Import Excel file to DataTable
      DataSet ds = xls.easy_ReadXLSXActiveSheet_AsDataSet("Excel.xlsx");
      DataTable dataTable = ds.Tables[0];  
      
      // Set the GridView data
      gridView.DataSource = dataTable;
      

      如果您的 Excel 文件中有多个工作表,您可以在 import Excel to Dataset in C# 上查看更多详细信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-06
        • 1970-01-01
        • 2019-05-15
        • 2017-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多