Excel 导入数据到GridControl中的方法
来源:互联网 发布:九搜网络登录 编辑:程序博客网 时间:2024/09/21 09:23
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Excel文件";
ofd.FileName = "";
ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
ofd.Filter = "所有文件(*.*)|*.*|Excel2003文件(*.xls)|*.xls|Excel2007文件(*.xlsx)|*.xlsx";
ofd.ValidateNames = true;
ofd.CheckFileExists = true;
ofd.CheckPathExists = true;
string strName = string.Empty;
if (ofd.ShowDialog() == DialogResult.OK)
{
strName = ofd.FileName;
}
if (strName == "")
{
XtraMessageBox.Show("没有选择Excel文件!无法进行数据导入");
return;
}
else
{
try
{
DataSet myDs = new DataSet();
string text = string.Format("Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '{0}';Extended Properties=Excel 8.0", strName);
string excelFirstTableName = GetExcelFirstTableName(text);
myDs.Tables.Clear();
myDs.Clear();
this.gridControl1.DataSource = null;
OleDbConnection selectConnection = new OleDbConnection(text);
OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(string.Format("select * from [{0}]", excelFirstTableName), selectConnection);
oleDbDataAdapter.Fill(myDs);
this.gridControl1.DataSource = myDs.Tables[0];
this.gridView1.PopulateColumns();
}
catch (Exception ex)
{
XtraMessageBox.Show("从电子表格文件中装载数据异常!", ex.Message);
}
}
}
/// <summary>
/// 返回Excel第一个工作表表名
/// </summary>
/// <param name="connectstring">excel连接字符串</param>
/// <returns></returns>
public static string GetExcelFirstTableName(string connectstring)
{
using (OleDbConnection connection = new OleDbConnection(connectstring))
{
string tableName = string.Empty;
if (connection.State == ConnectionState.Closed)
connection.Open();
DataTable dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt != null && dt.Rows.Count > 0)
{
tableName = ConvertTo<string>(dt.Rows[0][2]);
}
return tableName;
}
}
/// <summary>
/// 将数据转换为指定类型
/// </summary>
/// <typeparam name="T">转换的目标类型</typeparam>
/// <param name="data">转换的数据</param>
public static T ConvertTo<T>(object data)
{
if (data == null || Convert.IsDBNull(data))
return default(T);
object obj = ConvertTo(data, typeof(T));
if (obj == null)
{
return default(T);
}
return (T)obj;
}
/// <summary>
/// 将数据转换为指定类型
/// </summary>
/// <param name="data">转换的数据</param>
/// <param name="targetType">转换的目标类型</param>
public static object ConvertTo(object data, Type targetType)
{
if (data == null || Convert.IsDBNull(data))
{
return null;
}
Type type2 = data.GetType();
if (targetType == type2)
{
return data;
}
if (((targetType == typeof(Guid)) || (targetType == typeof(Guid?))) && (type2 == typeof(string)))
{
if (string.IsNullOrEmpty(data.ToString()))
{
return null;
}
return new Guid(data.ToString());
}
if (targetType.IsEnum)
{
try
{
return Enum.Parse(targetType, data.ToString(), true);
}
catch
{
return Enum.ToObject(targetType, data);
}
}
if (targetType.IsGenericType)
{
targetType = targetType.GetGenericArguments()[0];
}
return Convert.ChangeType(data, targetType);
}
- Excel 导入数据到GridControl中的方法
- excel导入数据到db2中的各种方法
- 实现Excel数据导入到SQL2005中的方法
- 实现Excel数据导入到SQL2005中的方法
- excel导入数据到db2中的各种方法
- excel中数据导入到Orcale数据库表中的方法
- 实现Excel数据导入到SQL2005中的方法
- 从EXCEL导入数据到SQL已有表中的方法
- 快速地将Excel数据导入到SQL2005中的方法
- Excel数据导入到sql库中的方法和步骤
- datagridview中的数据导入到excel中
- 将EXCEL中的数据导入到Mysql
- excel中的数据导入到R中
- 将Excel中的数据导入到Sqlserver
- Excel数据导入到Oracle有几种方法
- Excel数据导入到Oracle有几种方法
- c# 数据导入到EXCEL的方法
- js将网页中的table中的数据导入到excel中方法
- 微信公众平台开发(51)会员卡
- cocos2d-x 横屏、竖屏、读写sd卡
- 一键生成HTML4和WAP站
- 腾讯推出微信企业服务平台风铃
- WAP网页输入框的默认键盘类型控制
- Excel 导入数据到GridControl中的方法
- 淘宝微信互相屏蔽影响了谁
- 微信公众平台2013.08.05更新说明
- 微信5.0安卓内测版下载
- 微信公众平台高级功能
- Unity3D优化之合并网格
- 微信公众平台开发(52)大转盘
- 微信公众平台开发(53)砸金蛋
- 微信公众平台开发(54)翻板抽奖