Hello,
I'm having difficulties with this scenario. I need in ASP. CORE Razor Pages upload a excel file with the Upload File control, read that file with XLSIO engine and display the data in a DataGrid in Model BindProperty.
Regards,
@{
var asyncSettings = new Syncfusion.EJ2.Inputs.UploaderAsyncSettings { SaveUrl = "https://ej2.syncfusion.com/services/api/uploadbox/Save", RemoveUrl = "https://ej2.syncfusion.com/services/api/uploadbox/Remove" };
}
<ejs-uploader id="uploadFiles" asyncSettings="@asyncSettings" allowedExtensions=" .xls, .xlsx" autoUpload="false"></ejs-uploader> |
Do you have an example of how to bind JSON data of the converted excel file in the Grid ?
Also, in the upload control, this would call a post handler in the page ?
Regards,
<ejs-grid id="Grid" dataBound="dataBound" allowPaging="true" allowFiltering="true" allowGrouping="true" allowsorting="true"
toolbar="@(new List<string>(){"Add", "Edit", "Delete", "Update", "Cancel" })">
</ejs-grid>
<ejs-uploader id="uploader" autoUpload="false" asyncSettings="@asyncSettings" success="success" ></ejs-uploader>
<script>
function success(args) {
var grid = document.getElementsByClassName('e-grid')[0].ej2_instances[0];
var griddata = JSON.parse(args.e.target.responseText);
grid.dataSource = griddata;
}
</script>
|
using Syncfusion.XlsIO;
using System.Data;
. . .
{
public async Task<IActionResult> Save()
{
string filePath = "App_Data/TempData/";
string directoryPath = Path.Combine(new FileInfo(filePath).Directory.FullName);
if (!Directory.Exists(directoryPath))
Directory.CreateDirectory(directoryPath);
try
{
if (HttpContext.Request.Form.Files.Count > 0)
{
for (int i = 0; i < HttpContext.Request.Form.Files.Count; ++i)
{
IFormFile httpPostedFile = HttpContext.Request.Form.Files[i];
if (httpPostedFile != null)
{
filePath = Path.Combine(directoryPath, httpPostedFile.FileName);
if (!System.IO.File.Exists(filePath))
{
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await httpPostedFile.CopyToAsync(fileStream);
ExcelEngine excelEngine = new ExcelEngine();
//Loads or open an existing workbook through Open method of IWorkbooks
fileStream.Position = 0;
IWorkbook workbook = excelEngine.Excel.Workbooks.Open(httpPostedFile.OpenReadStream());
IWorksheet worksheet = workbook.Worksheets[0];
// Read data from the worksheet and Export to the DataTable.
DataTable table = worksheet.ExportDataTable(2,1,worksheet.Rows.Length, worksheet.Columns.Length, ExcelExportDataTableOptions.ColumnNames);
string JSONString = string.Empty;
JSONString = JsonConvert.SerializeObject(table);
ViewBag.data = JsonConvert.SerializeObject(table, Formatting.Indented, new JsonSerializerSettings { Converters = new[] { new Newtonsoft.Json.Converters.DataTableConverter() } });
//return View();
}
return Ok(ViewBag.data);
}
else
{
return BadRequest("File already exists");
}}}}
return BadRequest("No file in request"); ;
}
catch (Exception e)
{
return BadRequest(e.Message);
}
} |