Asp.net 导入Excel(服务器不带Office)

#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;

posted @ 2013-11-06 19:07  永不言败,自信相伴  阅读(445)  评论(0编辑  收藏  举报