How to send details of excel data in Detailview

262 Views Asked by At

I am developing an application that should extract excel data in a Gridview and show the details of individual data in a Detailsview. There is no use of SQL Server and sqlDataSource here. I am in dilemma though.

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

I have a button named 'View Detail' in GridView that should redirect me to Detailsview with the details of selected data.

Thanks.

2

There are 2 best solutions below

0
On

Use this method for exporting datagridview to excel

parameters -> excel path, and grid name

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 { }
             }
0
On

You can use EasyXLS Excel library to extract data from the Excel file. The Excel can be imported into a DataSet and no SQL Server is required.

// 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;

If you have more than one sheet into your Excel file, you may check for more details on import Excel to Dataset in C#.