Я не могу преобразовать xlsx в JSON.
Я пытался таким образом:
var jsonString = string.Empty;
jsonString = JsonConvert.SerializeObject(dataList, Formatting.Indented);
public ActionResult UploadFile()
{
try
{
var file = Request.Form.Files[0];
string folderName = "Upload";
string webRootPath = _hostingEnvironment.WebRootPath;
string newPath = Path.Combine(webRootPath, folderName);
if (!Directory.Exists(newPath))
{
Directory.CreateDirectory(newPath);
}
if (file.Length > 0)
{
string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
string fullPath = Path.Combine(newPath, fileName);
using (var stream = new FileStream(fullPath, FileMode.Create))
{
file.CopyTo(stream);
}
var workbook = new XLWorkbook(fullPath);
//Read the first Sheet from Excel file.
var workSheet = workbook.Worksheet(1);
var firstRowUsed = workSheet.FirstRowUsed();
var firstPossibleAddress = workSheet.Row(firstRowUsed.RowNumber()).FirstCell().Address;
var lastPossibleAddress = workSheet.LastCellUsed().Address;
// Get a range with the remainder of the worksheet data (the range used)
var range = workSheet.Range(firstPossibleAddress, lastPossibleAddress).AsRange(); //.RangeUsed();
// Treat the range as a table (to be able to use the column names)
var table = range.AsTable();
//Specify what are all the Columns you need to get from Excel
var dataList = new List<string[]>
{
table.DataRange.Rows()
.Select(tableRow =>
tableRow.Field("Condition").GetString())
.ToArray(),
table.DataRange.Rows()
.Select(tableRow => tableRow.Field("rules__field").GetString())
.ToArray(),
table.DataRange.Rows()
.Select(tableRow => tableRow.Field("rules__operator").GetString())
.ToArray(),
table.DataRange.Rows()
.Select(tableRow => tableRow.Field("rules__value").GetString())
.ToArray(),
};
var jsonString = string.Empty;
jsonString = JsonConvert.SerializeObject(dataList, Formatting.Indented);
}
return Json("Upload Successful.");
}
catch (Exception ex)
{
return Json("Upload Failed: " + ex.Message);
}
}
}
Получено:
[
[
"and"
],
[
"Id"
],
[
"Contains"
],
[
"4000030000502"
]
]
Ожидаемый результат:
{
"condition": "and",
"rules": [
{
"field": "Id",
"operator": "Contains",
"value": "4000030000502"
}
]
}