#region 把excel文件转换为DataSet.
/// <summary>  
/// 把excel文件转换为DataSet.  
/// </summary>  
/// <param name="filepath">文件路径</param>  
/// <param name="firstRow">第一行初始值</param>
/// <param name="firstColumn">第一列初始值</param>
/// <param name="moreSheet">是否取多余1个Sheet</param>
/// <returns></returns>  
public static DataSet ExcelToDataSet(string filepath, int firstRow, int firstColumn, bool moreSheet = false)
{
    DataSet ds = new DataSet();
    try
    {
        Workbook workbook = new Workbook(filepath);
        foreach (Worksheet worksheet in workbook.Worksheets)
        {
            if (worksheet.Cells.Rows.Count > 0)
            {
                ds.Tables.Add(worksheet.Cells.ExportDataTable(firstRow, firstColumn, worksheet.Cells.MaxDataRow + 1, worksheet.Cells.MaxDataColumn + 1, true));
                if (!moreSheet)
                    break;
            }
        }
    }
    catch (Exception ex)
    {
        Logging.Error(string.Format("把excel文件转换为DataSet时,读取Excel文件异常,描述:{0}", ex.Message));
    }
    return ds;
}
#endregion

用这个方法是要注意,需要下载一个Aspose.Cells.dll文件,引用到项目中

并且引用明明空间 using Aspose.Cells;

相关文章:

  • 2021-08-29
  • 2021-08-20
  • 2022-12-23
  • 2022-01-17
  • 2022-12-23
  • 2021-05-21
  • 2021-11-20
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-11
  • 2021-07-11
  • 2022-12-23
  • 2021-06-03
相关资源
相似解决方案