在c#应用程序中实现从Excel文件导入数据,但是为什么每次导入都会多出三空白行的,以下是程序片段:
string FileName;
openFileDialog1.Filter = "Excle文件(*.xls)|*.xls"; if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
FileName = openFileDialog1.FileName; string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + FileName + "';Extended Properties=Excel 8.0"; DataSet myDataset = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", constr);
da.Fill(myDataset);
//显示在dataGridView1
try
{ dataGridView1.DataSource = myDataset.Tables[0]; }
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
string FileName;
openFileDialog1.Filter = "Excle文件(*.xls)|*.xls"; if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
FileName = openFileDialog1.FileName; string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + FileName + "';Extended Properties=Excel 8.0"; DataSet myDataset = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", constr);
da.Fill(myDataset);
//显示在dataGridView1
try
{ dataGridView1.DataSource = myDataset.Tables[0]; }
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
你换一个文件试试.
当然,在 Excel 中删除的时候,应该把整行都删除,而不仅仅是删除内容,这样就不会出现多余的空行了。