在将excel导入datagridivew时,Alphabets不会显示在datagridview中 [英] While Importing the excel to datagridivew the Alphabets is not displaying in the datagridview
问题描述
Excel表格如下
Excel sheet as follows
StudentName ParentName MobileNo Eng Tam Mat Sci Social
Ram Suresh 9789502560 70 60 AB 60 65
Sam Sudhir 9940560520 60 55 75 AB 70
Vijay Ajay 9045078505 78 AB 60 50 AB
运行模式如下;
浏览(按钮)
当我点击浏览(按钮)时,上面的Excel表格将在datagridview中显示如下;
在Datagridview中如下;
Run mode as follows;
Browse (Button)
When i click the Browse (Button) the above Excel sheet will be displayed in datagridview as follows;
In Datagridview as follows;
StudentName ParentName MobileNo Eng Tam Mat Sci Social
Ram Suresh 9789502560 70 60 60 65
Sam Sudhir 9940560520 60 55 75 70
Vijay Ajay 9045078505 78 60 50
但是Gridview AB(缺席)未显示在Datagridview中。
浏览按钮代码如下;
But in the Gridview AB(Absent) is not displayed in the Datagridview.
Browse Button code as follows;
private void Browse _Click(object sender, EventArgs e)
{
try
{
DialogResult MsgDr = MessageBox.Show("Excel file format should be in Student Name,Parent Name,Mobile No,Marks. \nAre you sure want to continue?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (MsgDr == DialogResult.No)
return;
LblSheetName.Text = "";
OpenFileDialog fldg = new OpenFileDialog();
fldg.Title = "Select File";
fldg.FileName = txtFileName.Text;
fldg.Filter = "Excel Sheet(*.xls)|*.xls|Excel-2007 Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
fldg.FilterIndex = 1;
fldg.RestoreDirectory = true; //added
if (fldg.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = fldg.FileName;
FileInfo file = new FileInfo(txtFileName.Text);
if(IsFileLocked(file) == true)
{
MessageBox.Show("Selected file is opened already/not properly closed/using by some other person. Close it properly and try again", "File already opened", MessageBoxButtons.OK);
txtFileName.Text = string.Empty;
return;
}
Import();
System.Windows.Forms.Application.DoEvents();
}
}
catch (Exception ex1)
{
progressBar1.Visible = false;
MessageBox.Show("Error in open" + ex1.ToString(), "Error", MessageBoxButtons.OK);
return;
}
}
private void Import()
{
try
{
progressBar1.Visible = true;
progressBar1.Value = 700;
String Pathname = txtFileName.Text;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Pathname + ";Extended Properties=\"Excel 8.0;HDR=YES\";";
conn.Open();
GSheetName = "";
System.Data.DataTable dt = conn.GetSchema("Tables");
string sheetname = "";
foreach (DataRow row in dt.Rows)
{
sheetname = (string)row["TABLE_NAME"];
if (sheetname.EndsWith("$"))
{
GSheetName = sheetname.ToString().Trim();
break;
}
}
LblSheetName.Text = "Sheet Name :" + GSheetName.ToString().Trim();
if (GSheetName == "" || GSheetName == null)
{
progressBar1.Visible = false;
MessageBox.Show("Invalid Sheet Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
OleDbCommand command = new OleDbCommand("SELECT * From[" + GSheetName + "]", conn);
DataSet ds = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
progressBar1.Visible = false;
}
catch (Exception ex)
{
progressBar1.Visible = false;
MessageBox.Show("Error in Import" + ex.ToString(), "Error", MessageBoxButtons.OK);
return;
}
}
如何在将Excel工作表导入datagridview时显示字母表。
请帮帮我我上面的代码中的问题。
问候,
Narasiman P.
how to show the Alphabets while importing the excel sheet into datagridview.
please help me what si the problem in my above code.
Regards,
Narasiman P.
推荐答案
))
{
GSheetName = sheetname.ToString()。Trim();
break ;
}
}
LblSheetName.Text = 工作表名称: + GSheetName.ToString()。Trim();
if (GSheetName == || GSheetName == null )
{
progressBar1.Visible = false ;
MessageBox.Show( 无效的工作表名称, 错误,MessageBoxButtons.OK,MessageBoxIcon.Information);
return ;
}
OleDbCommand command = new OleDbCommand( SELECT * From [ + GSheetName + ],conn);
DataSet ds = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables [ 0 ];
progressBar1.Visible = false ;
}
catch (例外情况)
{
progressBar1.Visible = 假跨度>;
MessageBox.Show( 导入错误 + ex.ToString(), 错误,MessageBoxButtons.OK);
return ;
}
}
")) { GSheetName = sheetname.ToString().Trim(); break; } } LblSheetName.Text = "Sheet Name :" + GSheetName.ToString().Trim(); if (GSheetName == "" || GSheetName == null) { progressBar1.Visible = false; MessageBox.Show("Invalid Sheet Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } OleDbCommand command = new OleDbCommand("SELECT * From[" + GSheetName + "]", conn); DataSet ds = new DataSet(); OleDbDataAdapter adapter = new OleDbDataAdapter(command); adapter.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; progressBar1.Visible = false; } catch (Exception ex) { progressBar1.Visible = false; MessageBox.Show("Error in Import" + ex.ToString(), "Error", MessageBoxButtons.OK); return; } }
如何在将Excel工作表导入datagridview时显示字母表。
请帮帮我我上面的代码中的问题。
问候,
Narasiman P。
how to show the Alphabets while importing the excel sheet into datagridview.
please help me what si the problem in my above code.
Regards,
Narasiman P.
这篇关于在将excel导入datagridivew时,Alphabets不会显示在datagridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!