您的位置:首页 > 编程语言 > ASP

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

2013-11-06 19:07 309 查看
#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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: