如何从excel将文件导入到listview c#
2个回答
展开全部
如果是客户端的话,建议先将excel上传到服务器,后台将这个excel文件作为数据源进行查询,将结果集绑定到listview上
/// <summary>执行查询语句返回SqlDataReader</summary>
/// <param name="storedProcName">执行的存储过程的名字,为字符串类型string</param>
/// <param name="sqlParam">存储过程参数的数组</param>
/// <returns>返回一个SqlDataReader</returns>
public DataTable ExecuteSqlFromFileReturnDataTable(string filenamewithpath)
{
string oleDBConnString = String.Empty;
oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;";
oleDBConnString += "Data Source=";
oleDBConnString += filenamewithpath;
oleDBConnString += ";Extended Properties=Excel 8.0;";
System.Data.OleDb.OleDbConnection oleDBConn = null;
System.Data.OleDb.OleDbDataAdapter oleAdMaster = null;
DataTable m_tableName = new DataTable();
DataTable dtResult = new DataTable();
oleDBConn = new System.Data.OleDb.OleDbConnection(oleDBConnString);
oleDBConn.Open();
m_tableName = oleDBConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
if (m_tableName != null && m_tableName.Rows.Count > 0)
{
m_tableName.TableName = m_tableName.Rows[0]["TABLE_NAME"].ToString();
}
string strCmd = " SELECT * FROM [" + m_tableName.TableName + "]";
oleAdMaster = new System.Data.OleDb.OleDbDataAdapter(strCmd, oleDBConn);
//oleAdMaster.Fill(ds, "m_tableName");
oleAdMaster.Fill(dtResult);
oleAdMaster.Dispose();
oleDBConn.Close();
oleDBConn.Dispose();
return dtResult;
}
/// <summary>执行查询语句返回SqlDataReader</summary>
/// <param name="storedProcName">执行的存储过程的名字,为字符串类型string</param>
/// <param name="sqlParam">存储过程参数的数组</param>
/// <returns>返回一个SqlDataReader</returns>
public DataTable ExecuteSqlFromFileReturnDataTable(string filenamewithpath)
{
string oleDBConnString = String.Empty;
oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;";
oleDBConnString += "Data Source=";
oleDBConnString += filenamewithpath;
oleDBConnString += ";Extended Properties=Excel 8.0;";
System.Data.OleDb.OleDbConnection oleDBConn = null;
System.Data.OleDb.OleDbDataAdapter oleAdMaster = null;
DataTable m_tableName = new DataTable();
DataTable dtResult = new DataTable();
oleDBConn = new System.Data.OleDb.OleDbConnection(oleDBConnString);
oleDBConn.Open();
m_tableName = oleDBConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
if (m_tableName != null && m_tableName.Rows.Count > 0)
{
m_tableName.TableName = m_tableName.Rows[0]["TABLE_NAME"].ToString();
}
string strCmd = " SELECT * FROM [" + m_tableName.TableName + "]";
oleAdMaster = new System.Data.OleDb.OleDbDataAdapter(strCmd, oleDBConn);
//oleAdMaster.Fill(ds, "m_tableName");
oleAdMaster.Fill(dtResult);
oleAdMaster.Dispose();
oleDBConn.Close();
oleDBConn.Dispose();
return dtResult;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-08-27
展开全部
不知道你将ListView的数据源绑定问Excel能不能行。你可以试试。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询