日期:2014-05-18  浏览次数:20929 次

excel实现拖放导入datagridview
excel实现拖放导入datagridview。

通过打开对话框导入excel文件到datagridview,这个我实现了。
请教,我想直接把excel文件通过拖放方式导入datagridview如何实现?

------解决方案--------------------
http://www.cnblogs.com/lhxhappy/archive/2008/11/26/1341873.html
参考:WINFORM实现的用于拖放操作和文件复制小功能
private void ExBtnExecute_ClickEvent(object sender, EventArgs e)
{
ExBtnExecute.CtlEnabled = false;
for (int i = 0; i < lstFilePath.Items.Count; i++)
{
Application.DoEvents();
lstFilePath.SetSelected(i, true);
//如果当前字符串是一个目录则.
if (Directory.Exists(lstFilePath.Items[i].ToString()))
{
CopyDirectory(lstFilePath.Items[i].ToString(), txtSelDrive.Text +"\\"+ Path.GetFileName(lstFilePath.Items[i].ToString()));
lstFilePath.Items[i] = "复制完成";
}
else
{
if (!File.Exists(lstFilePath.Items[i].ToString())) continue; //如果文件不存在继续下一个循环
File.Copy(lstFilePath.Items[i].ToString(), txtSelDrive.Text + "\\" + Path.GetFileName(lstFilePath.Items[i].ToString())); 
lstFilePath.Items[i] = "复制完成";
}
}
lstFilePath.Items.Clear();
ExBtnExecute.CtlEnabled = true;
}
private void txtSelDrive_DoubleClick(object sender, EventArgs e)
{
FolderSelDialog.ShowDialog(this);
txtSelDrive.Text = FolderSelDialog.SelectedPath;
}
/**//// <summary>
/// 复制一个目录下的所有文件或目录到一个新的目录下
/// </summary>
/// <param name="sourcePath">源目录路径</param>
/// <param name="destPath">目标目录路径</param>
private void CopyDirectory(string sourcePath, string destPath)
{
try
{
//如果目标路径没有以\结尾则加之 
if (destPath[destPath.Length - 1] != Path.DirectorySeparatorChar)
{
destPath += Path.DirectorySeparatorChar;
}
if (!Directory.Exists(destPath))
{
Directory.CreateDirectory(destPath);
}
string[] fileList = Directory.GetFileSystemEntries(sourcePath);
foreach (string file in fileList)
{
//如果是一个目录则
if (Directory.Exists(file))
{
CopyDirectory(file, destPath + Path.GetFileName(file));
}
else
{
File.Copy(file, destPath + Path.GetFileName(file),true);
}
}
}
catch(IOException ioe)
{
MessageBox.Show(ioe.Message, "复制文件时出错", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
 }