vs2005 使用openFileDialog控件,在打开文件对话框中却看不到任何文件是怎么回事?我用这个导入Excel的代码如下:
private void tstripInExcel_Click(object sender, EventArgs e)//导入Excel文档
{
this.openFileDialog1.Multiselect = true;
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
string MySql = "insert into Course select * from OPENROWSET('MICROSOFT.JET.OLEDB.4.0','Excel 8.0;HDR=YES;DATABASE=" + this.openFileDialog1.FileName.ToString() + "',sheet1$) ";
try
{
this.sqlConnection1.Open();
SqlCommand MyCommand = new SqlCommand(MySql, this.sqlConnection1);
MyCommand.ExecuteNonQuery();
this.sqlConnection1.Close();
this.LoadDataSet();
MessageBox.Show("导入成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception E)
{
this.ErrorHandle(E);
}
}
}之前还好好的,可是不知道为什么,打开打开文件对话框后就根本看到不到任何文件,只能看到文件夹。
private void tstripInExcel_Click(object sender, EventArgs e)//导入Excel文档
{
this.openFileDialog1.Multiselect = true;
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
string MySql = "insert into Course select * from OPENROWSET('MICROSOFT.JET.OLEDB.4.0','Excel 8.0;HDR=YES;DATABASE=" + this.openFileDialog1.FileName.ToString() + "',sheet1$) ";
try
{
this.sqlConnection1.Open();
SqlCommand MyCommand = new SqlCommand(MySql, this.sqlConnection1);
MyCommand.ExecuteNonQuery();
this.sqlConnection1.Close();
this.LoadDataSet();
MessageBox.Show("导入成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception E)
{
this.ErrorHandle(E);
}
}
}之前还好好的,可是不知道为什么,打开打开文件对话框后就根本看到不到任何文件,只能看到文件夹。
openFileDialog1.Filter = "excel文件|*.xls";
这个方法试了,可就是不管用
在打开文件对话框中,文件类型下拉出现"excel文件("也出现的)只有filter属性为空的时候才显示文件
char szFile[260]; // buffer for file name
HWND hwnd; // owner window
HANDLE hf; // file handle// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
//
// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
//
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;// Display the Open dialog box. if (GetOpenFileName(&ofn)==TRUE)
hf = CreateFile(ofn.lpstrFile, GENERIC_READ,
0, (LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
用兼容模式就能认出来了不好意思耽误大家时间了 - -#