📄 loadexceldata.cs
字号:
using System;
using System.Data;
using System.Data.OleDb;
namespace HrSalary.util
{
/// <summary>
/// LoadExcelData 的摘要说明。
/// </summary>
public class LoadExcelData
{
private static OleDbConnection odconn = null;
private static OleDbDataAdapter odapt = null;
private static DataSet ds = null;
private LoadExcelData()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static DataSet getExcelData(string fileurl,string sheetname)
{
try
{
//创建一个数据链接
string strCon = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = "+fileurl+";Extended Properties=Excel 8.0";
string strSql = "SELECT * FROM ["+sheetname+"$]";
odconn = new OleDbConnection(strCon);
odconn.Open();
//打开数据链接,得到一个数据集
odapt = new OleDbDataAdapter(strSql,odconn);
//创建一个 DataSet对象
ds = new DataSet();
//得到自己的DataSet对象
odapt.Fill(ds,"datatable");
//关闭此数据链接
odapt.Dispose();
odconn.Dispose();
return ds;
}
catch
{
try
{
if(ds != null)
{
ds.Clear();
}
}
catch
{
return null;
}
try
{
if(odapt != null)
{
odapt.Dispose();
}
}
catch
{
return null;
}
try
{
if(odconn != null)
{
odconn.Close();
odconn.Dispose();
}
}
catch
{
return null;
}
return null;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -