Skip to content

DevExpress-Examples/asp-net-web-forms-grid-upload-and-display-excel-file

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Grid View for ASP.NET Web Forms - How to display data from an uploaded Excel file

This example demonstrates how to use the ASPxUploadControl to let a user upload a Microsoft Excel file to the server and view the uploaded file's data in a Grid View.

Note: The example application uses the DevExpress.Docs assembly. The Document Server subscription license is required to use the demonstrated technique.

Upload an Excel File and Display it in a Grid View

Implementation Details

  1. Add an ASPxGridView and ASPxUploadControl to a page.

  2. Handle the upload control's ASPxUploadControl.FileUploadComplete event on the server and ASPxClientUploadControl.FileUploadComplete event on the client.

  3. Use the ASPxUploadControl.FileUploadComplete handler to save the uploaded files to the "~/XlsTables/" directory. Save the file path to the session.

    string FilePath {
        get { return Session["FilePath"] == null ? String.Empty : Session["FilePath"].ToString(); }
        set { Session["FilePath"] = value; }
    }
    protected void Upload_FileUploadComplete(object sender, DevExpress.Web.FileUploadCompleteEventArgs e) {
        FilePath = Page.MapPath("~/XlsTables/") + e.UploadedFile.FileName;
        e.UploadedFile.SaveAs(FilePath);
    } 
  4. Call the ASPxClientGridView.PerformCallback method from the ASPxClientUploadControl.FileUploadComplete event handler to refresh the Grid View after a file is uploaded.

    function OnFileUploadComplete(s, e) {
        Grid.PerformCallback();
    }
  5. Handle the ASPxGridView.Init event. Check the Session["FileName"] object's value in the event handler. If the value is not null, use the Spreadsheet Document API to export the saved Excel file to a DataTable object and bind the Grid View to this object.

    protected void Grid_Init(object sender, EventArgs e) {
        if (!String.IsNullOrEmpty(FilePath)) {
            Grid.DataSource = GetTableFromExcel();
            Grid.DataBind();
        }
    }
    private DataTable GetTableFromExcel() {
        Workbook book = new Workbook();
        book.InvalidFormatException += book_InvalidFormatException;
        book.LoadDocument(FilePath);
        Worksheet sheet = book.Worksheets.ActiveWorksheet;
        CellRange range = sheet.GetUsedRange();
        DataTable table = sheet.CreateDataTable(range, false);
        DataTableExporter exporter = sheet.CreateDataTableExporter(range, table, false);
        exporter.CellValueConversionError += exporter_CellValueConversionError;
        exporter.Export();
        return table;
    }

Files to Look At

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

About

Upload an Excel file and display its data in an ASPxGridView.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •