[英]Excel to Grid view issues using OleDbDataAdapter
I am creating a Windows Forms Application using Visual Studio 2010. 我正在使用Visual Studio 2010创建Windows窗体应用程序。
I populate data to a DataGridView from an excel file using OleDbDataAdapter method. 我使用OleDbDataAdapter方法从excel文件中将数据填充到DataGridView。
Here is my code 这是我的代码
dataGridView1.DataSource = null;
dataGridView1.Update();
var connectionString =
string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=No;IMEX=1\";", fileName);
var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);
var ds = new DataSet();
DataTable t = new DataTable();
adapter.Fill(t);
dataGridView1.DataSource = t;
Now Problem is if some cells are merged in the excel file output get bit different. 现在问题是如果某些单元格在excel文件输出中合并得有点不同。 Here is the image for better understanding. 这是更好理解的图像。
So how can I fix this problem ? 那么我该如何解决这个问题呢?
I think if I can identify the merge cells then I can fix this. 我想如果我可以识别合并单元格,那么我可以解决这个问题。 But I don't have a clear idea at the moment. 但目前我还没有明确的想法。
Is there a better way to represent excel data in grid view as it is in the excel file ? 有没有更好的方法在网格视图中表示excel数据,就像在excel文件中一样?
Any answer would be help. 任何答案都会有所帮助。 Please share any suggestions. 请分享任何建议。
Thanks 谢谢
Yohan 勒芒
My solution: 我的解决方案
protected void Page_Load(object sender, EventArgs e)
{
string path = @"C:\samples\firstexcel.xlsx";
GetExcelSheetNames(path);
}
private void GetExcelSheetNames(string excelFile)
{
OleDbConnection objConn = null;
System.Data.DataTable dt = null;
try
{
DataSet ds = new DataSet();
// Connection String.
String connString = @"Data Source=" + excelFile + ";
Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=Excel 12.0;";
// Create connection.
objConn = new OleDbConnection(connString);
// Opens connection with the database.
objConn.Open();
// Get the data table containing the schema guid, and also sheet names.
dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return;
}
String[] excelSheets = new String[dt.Rows.Count];
int i = 0;
// Add the sheet name to the string array.
// And respective data will be put into dataset table
foreach (DataRow row in dt.Rows)
{
excelSheets[i] = row["TABLE_name"].ToString();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + excelSheets[i] + "]", objConn);
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;
oleda.Fill(ds, "TABLE");
i++;
}
// Bind the data to the GridView
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
Session["Table"] = ds.Tables[0];
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
// Clean up.
if (objConn != null)
{
objConn.Close();
objConn.Dispose();
}
if (dt != null)
{
dt.Dispose();
}
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = ((DataTable)Session["Table"]).DefaultView;
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{;}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.