using System.Linq; using System.Threading.Tasks; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Spreadsheet; using Microsoft.AspNetCore.Mvc; using MyWebsite.Filters;
namespaceMyWebsite.Controllers { [Route("api/[controller]")] publicclassImportController : Controller { [HttpPost] [DisableFormValueModelBinding] publicasync Task<IActionResult> Post() { await Request.StreamFile(file => { using (var document = SpreadsheetDocument.Open(file.FileStream, false)) { var workbookPart = document.WorkbookPart; var sheet = workbookPart.Workbook.Sheets.GetFirstChild<Sheet>(); var worksheet = ((WorksheetPart)workbookPart.GetPartById(sheet.Id)).Worksheet; var sheetData = worksheet.GetFirstChild<SheetData>();
foreach (var row in sheetData.ChildElements.Select(x=>x as Row)) { // 取得每一行 foreach(var cell in row.ChildElements.Select(x => x as Cell)) { // 取得每一欄 var innerText = cell.CellValue.Text; // Do somethings } } } return file.FileStream; }); return Ok(); } } }