Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
540
igc-spreadsheet passing rows to server api
posted

I am loading an xslx into a spreadsheet. I am looking to either convert it to a data table or iterate through the row to pass them to an api.

If I iterate, how do I pass the columns properly?

Also, if it can be converted to a json file that might work, since I can iterate through the rows that way.

Thanks

Abbott

  • 34730
    Offline posted

    Hello Abbott,

    I see that this post was made in the Ignite UI for Blazor forums, but also for the igc-spreadsheet, and so I am unsure if this was supposed to be Ignite UI for Web Components? I will continue in a Blazor context on this though, as the APIs are very similar.

    Assuming that your Workbook / Worksheet has a rectangular, table-like structure, you can get the dimensions of the table to be generated by using the following code, where “wb” is the Workbook:

                Worksheet sheet = wb.Worksheets["Sheet1"];
    
                int rowCount = sheet.Rows.Count();
    
                int colCount = 0;
    
                foreach(WorksheetRow row in sheet.Rows)
                {
                    int cellCount = row.Cells.Count();
                    if(cellCount > colCount)
                    {
                        colCount = cellCount;
                    }
                }

    At first glance, this probably looks like it will return a huge amount of rows and columns/cells, since .xlsx files are capable of having that, but in the Count() method, we only allocate the rows and cells that have values in them. So, by the end of the foreach loop in the code snippet, you would know the dimensions of your table as rowCount and colCount.

    From there, you simply have to use the following code-snippet to get the value of each cell in each row, and you can construct a DataTable from it:

                for(int i=0; i<rowCount; i++)
                {
                    WorksheetRow row = sheet.Rows[i];
    
                    for(int j=0; j<colCount; i++)
                    {
                        WorksheetCell cell = row.Cells[j];
                        var value = cell.Value;
                    }
                }

    I hope this helps. Please let me know if you have any other questions or concerns on this matter.