<SfGrid DataSource="@Orders" AllowFiltering="true" AllowPaging="true">
<GridColumns>
. . . .. . .
</GridColumns>
</SfGrid>
@code{
public List<Order> Orders = new List<Order>();
public DataTable table = new DataTable();
protected override void OnInitialized()
{
//convert datatable to list
ConvertDataTable<Order>(table);
}
protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
{
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
//Set the default application version
application.DefaultVersion = ExcelVersion.Excel2016;
//Load the existing Excel workbook into IWorkbook
FileStream inputStream = new FileStream("Sample.xlsx", FileMode.Open);
IWorkbook workbook = application.Workbooks.Open(inputStream);
IWorksheet worksheet = workbook.Worksheets[0];
// Read data from the worksheet and Export to the DataTable.
table = worksheet.ExportDataTable(worksheet.UsedRange, ExcelExportDataTableOptions.ColumnNames);
}
}
private void ConvertDataTable<T>(DataTable dt)
{
foreach (DataRow row in dt.Rows)
{
Orders.Add(new Order
{
OrderID = Convert.ToInt32(row[0]),
. . . . .
});
}
}
public class Order
{
public int OrderID { get; set; }
. . . . . .
}
}
|
I did not find the sample useful.
Is there funtionality to import excel into a datagrid?
protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
{
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
//Set the default application version
application.DefaultVersion = ExcelVersion.Excel2016;
//Load the existing Excel workbook into IWorkbook
FileStream inputStream = new FileStream("Sample.xlsx", FileMode.Open);
IWorkbook workbook = application.Workbooks.Open(inputStream);
IWorksheet worksheet = workbook.Worksheets[0];
// Read data from the worksheet and Export to the DataTable.
table = worksheet.ExportDataTable(worksheet.UsedRange, ExcelExportDataTableOptions.ColumnNames);
ConvertDataTable<Order>(table);
}
}
|
Hi
Vignesh Natarajan,
I found your above sample useful.
But i'd like to ask two things.
1 Since it fetchng the excel file from the project directly, How can i change that and use the
File Upload component so i can choose any excel file and upload it to filestream for it to be read into the datagrid ?
2. I believe after i have the data uploaded in the data grid, with a stored procedure i can save them into my database table, corr
<SfGrid @ref="Grid" DataSource="@GridData" Toolbar="@(new List<string> { "Add", "Edit", "Delete", "Cancel", "Update" })" AllowFiltering="true" AllowSorting="true" AllowPaging="true">
<GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true"></GridEditSettings>
<GridEvents OnActionBegin="OnBegin" OnActionComplete="OnComplete" TValue="Order"></GridEvents>
<GridColumns>
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsIdentity="true" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn>
</GridColumns>
</SfGrid>
@code{
SfGrid<Order> Grid { get; set; }
public IEnumerable<Order> GridData { get; set;}
protected override void OnInitialized()
{
GridData = OrderData.GetAllOrders().ToList();
}
public void OnComplete(ActionEventArgs<Order> Args)
{
if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Save || Args.RequestType == Syncfusion.Blazor.Grids.Action.Refresh)
{
GridData = OrderData.GetAllOrders().ToList(); // fetch updated data from service and bind to grid datasource property
}
}
public void OnBegin(ActionEventArgs<Order> Args)
{
if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Save) // update the changes in Actionbegine event
{
if (Args.Action == "Add")
{
//Args.Data contain the inserted record details
//insert the data into your database
OrderData.AddOrder(Args.Data);
}
else
{
//Args.Data contain the updated record details
//update the data into your database
OrderData.UpdateOrder(Args.Data);
}
} else if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Delete)
{
OrderData.DeleteOrder(Args.Data.OrderID); // delete the record from your database
}
}
} |
May I please have a demo for this
Hi,
Can you please continue with Boot's question.
Hi Christopher,
Based on your query, We prepared sample like fetched data from excel file and convert them in to data. Please refer the attached sample for your reference.
Please let us know if you have any concerns.
Regards,
Naveen Palanivel