/// <summary> /// 转DataTable /// </summary> /// <param name="ExcelPath">Excel文件地址</param> /// <param name="SheetName">Sheet名称</param> /// <param name="StartRow">开始行数</param> /// <param name="StartColumn">开始列数</param> /// <returns></returns> public static DataTable GetDataTable(string ExcelPath, string SheetName, int StartRowIndex, int StartColumnIndex) { DataTable dt = new DataTable(); try { Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(ExcelPath); Aspose.Cells.Worksheet sheet = workbook.Worksheets[SheetName]; dt = sheet.Cells.ExportDataTable(StartRowIndex, StartColumnIndex, sheet.Cells.MaxDataRow + 1, sheet.Cells.MaxDataColumn + 1, true); } catch (Exception ex) { dt = null; } return dt; }
删除行方法:
/// <summary> /// 删除Excel多行 /// </summary> /// <param name="ExcelPath">Excel路径</param> /// <param name="SheetName">Sheet表名</param> /// <param name="RowNumber">删除多行,注意删除的顺序必须为倒序(RowNumber="4,3,2,1,0")</param> /// <returns></returns> public static bool DeleteRow(string ExcelPath, string SheetName, string RowNumber) { bool IsDelete = false; try { Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(ExcelPath); Aspose.Cells.Worksheet sheet = workbook.Worksheets[SheetName]; string[] InputStr = RowNumber.Split(','); foreach (string item in InputStr) { sheet.Cells.DeleteRow(int.Parse(item)); } workbook.Save(ExcelPath); IsDelete = true; } catch (Exception) { IsDelete = false; } return IsDelete; }