public static bool GetExcelDatatable(out DataTable dt, out string strMSG) { strMSG = null; dt = null; try { HttpPostedFile files = HttpContext.Current.Request.Files[0]; Workbook workbook = new Workbook(files.InputStream); //创建工作簿对象 WorksheetCollection worksheets = workbook.Worksheets; //创建工作表对象 string sheetName = worksheets[0].Name; Worksheet worksheet = workbook.Worksheets[0]; Cells cells = worksheet.Cells; dt = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true); return true; ; } catch (Exception ee) { strMSG = ee.Message; return false; } }
注意需要在NuGet包中添加Aspose组件!
此处我用的是webapi