ASP.NETCore --- Excel 파일 가져오기

18761 단어
전단에 excel 파일을 업로드하고 npoi 읽기 데이터를 데이터테이블로 변환(netcore 갱부야, vs2017을 시각화할 수 없다니)
프런트엔드 인터페이스
@{
    Layout = null;
}



    "viewport" content="width=device-width" />
    Xlsx


    
"multipart/form-data"method="post"asp-action="ImportToDataTable">
"file"name="excelfile"/>
"submit"value = "업로드"/>
 
백그라운드 /// /// EXCEL /// /// /// public void ImportToDataTable(IFormFile excelfile) { DataTable dt = new DataTable(); var filePath = excelfile.FileName.Split('.'); if (filePath[1].ToLower()==".xls") {//.xls #region .xls :HSSFWorkbook HSSFWorkbook hssfworkbook; try { using (MemoryStream ms = new MemoryStream()) { excelfile.CopyTo(ms); ms.Seek(0, SeekOrigin.Begin); hssfworkbook = new HSSFWorkbook(ms); } ISheet sheet = hssfworkbook.GetSheetAt(0); System.Collections.IEnumerator rows = sheet.GetRowEnumerator(); var maxRowIndex = 0; while (rows.MoveNext()) { IRow row = (HSSFRow)rows.Current; var r = row.Cells.Select(p => p.StringCellValue).ToList(); var _result = row.Cells.Any(t => !string.IsNullOrEmpty(t.StringCellValue)); if (_result) { maxRowIndex = row.RowNum; break; } } HSSFRow headerRow = (HSSFRow)sheet.GetRow(maxRowIndex);// var _ColumnIndex = new List<int>(); // for (int j = 0; j < (headerRow.LastCellNum); j++) { //SET EVERY COLUMN NAME HSSFCell cell = (HSSFCell)headerRow.GetCell(j); if (!string.IsNullOrEmpty(cell.StringCellValue)) { dt.Columns.Add(cell.ToString()); _ColumnIndex.Add(cell.ColumnIndex); } } while (rows.MoveNext()) { IRow row = (HSSFRow)rows.Current; DataRow dr = dt.NewRow(); if (row.RowNum <= headerRow.RowNum) continue; for (int i = 0; i < _ColumnIndex.Count; i++) { if (i >= dt.Columns.Count) { break; } ICell cell = row.GetCell(_ColumnIndex[i]); if ((i == 0) && cell == null)// cell ,break { break; } if (cell == null) { dr[i] = null; } else { switch (cell.CellType) { case CellType.String: dr[i] = cell.StringCellValue; break; case CellType.Numeric: if (DateUtil.IsCellDateFormatted(cell)) { dr[i] = cell.DateCellValue; } else { dr[i] = cell.NumericCellValue; } break; default: dr[i] = null; break; } } } dt.Rows.Add(dr); } } catch (Exception e) { throw new Exception(e.Message, e); } #endregion } else {//.xlsx #region .xlsx :XSSFWorkbook XSSFWorkbook hssfworkbook; try { using (MemoryStream ms = new MemoryStream()) { excelfile.CopyTo(ms); ms.Seek(0, SeekOrigin.Begin); hssfworkbook = new XSSFWorkbook(ms); } ISheet sheet = hssfworkbook.GetSheetAt(0); // ISheet sheet = hssfworkbook.GetSheet(sheetName); System.Collections.IEnumerator rows = sheet.GetRowEnumerator(); var maxRowIndex = 0; while (rows.MoveNext()) { IRow row = (XSSFRow)rows.Current; var _result = row.Cells.Any(t => !string.IsNullOrEmpty(t.StringCellValue)); if (_result) { maxRowIndex = row.RowNum; break; } } XSSFRow headerRow = (XSSFRow)sheet.GetRow(maxRowIndex);// var _ColumnIndex = new List<int>(); // for (int j = 0; j < (headerRow.LastCellNum); j++) { //SET EVERY COLUMN NAME XSSFCell cell = (XSSFCell)headerRow.GetCell(j); if (!string.IsNullOrEmpty(cell.StringCellValue)) { dt.Columns.Add(cell.ToString()); _ColumnIndex.Add(cell.ColumnIndex); } } while (rows.MoveNext()) { IRow row = (XSSFRow)rows.Current; DataRow dr = dt.NewRow(); if (row.RowNum == 0) continue; for (int i = 0; i < _ColumnIndex.Count; i++) { if (i >= dt.Columns.Count) { break; } ICell cell = row.GetCell(_ColumnIndex[i]); if ((i == 0) && (cell == null))// cell ,break { break; } if (cell == null) { dr[i] = null; } else { switch (cell.CellType) { case CellType.String: dr[i] = cell.StringCellValue; break; case CellType.Numeric: if (DateUtil.IsCellDateFormatted(cell)) { dr[i] = cell.DateCellValue; } else { dr[i] = cell.NumericCellValue; } break; default: dr[i] = null; break; } } } dt.Rows.Add(dr); } } catch (Exception e) { throw new Exception(e.Message, e); } #endregion } var a = dt.Rows.Count;// }
 
다음으로 전송:https://www.cnblogs.com/macT/p/11613663.html

좋은 웹페이지 즐겨찾기