Excel数据导入到网格 [英] Excel data import to Grid
本文介绍了Excel数据导入到网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将Excel文件数据显示到Grid视图
将网格数据保存到sql表中
请尽快帮我.
谢谢.
解决方案
尝试一下:
受保护的 无效 Page_Load(对象发件人,EventArgs e) { 字符串 connString = ConfigurationManager.ConnectionStrings [" span>].ConnectionString; // 创建连接对象 OleDbConnection oledbConn = 新 OleDbConnection(connString); 尝试 { // 打开连接 oledbConn.Open(); // 创建OleDbCommand对象并从工作表Sheet1中选择数据 OleDbCommand cmd = 新 OleDbCommand(" ,oledbConn); // 创建新的OleDbDataAdapter OleDbDataAdapter oleda = 新 OleDbDataAdapter(); oleda.SelectCommand = cmd; // 创建一个数据集,该数据集将保存从工作表中提取的数据. DataSet ds = 新 DataSet(); // 从工作表中提取的数据填充数据集. oleda.Fill(ds," ); // 将数据绑定到GridView GridView1.DataSource = ds.Tables [ 0 ].DefaultView; GridView1.DataBind(); } 捕获 { } 最终 { // 关闭连接 oledbConn.Close(); } }
参考号:从ASP.NET中的Excel文件(.xls)中读取数据 [ ^ ]
请参考:
从excel导入到gridview [ public DataSet GetDataSetForGrid() { 字符串 cnstr = " + 扩展属性= Excel 9.0"; OleDbConnection oledbConn = 新 OleDbConnection(cnstr); 字符串 strSQL = " 解决方案
Try this:
protected void Page_Load(object sender, EventArgs e) { string connString = ConfigurationManager.ConnectionStrings["xls"].ConnectionString; // Create the connection object OleDbConnection oledbConn = new OleDbConnection(connString); try { // Open connection oledbConn.Open(); // Create OleDbCommand object and select data from worksheet Sheet1 OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1
", oledbConn); // Create new OleDbDataAdapter OleDbDataAdapter oleda = new OleDbDataAdapter(); oleda.SelectCommand = cmd; // Create a DataSet which will hold the data extracted from the worksheet. DataSet ds = new DataSet(); // Fill the DataSet from the data extracted from the worksheet. oleda.Fill(ds, "Employees"); // Bind the data to the GridView GridView1.DataSource = ds.Tables[0].DefaultView; GridView1.DataBind(); } catch { } finally { // Close connection oledbConn.Close(); } }
Ref.:Read Data From an Excel File (.xls) in ASP.NET[^]
Please refer:
import from excel to gridview[^]
Go step wise. Start from showing excel data in gridview, like:
public DataSet GetDataSetForGrid() { string cnstr = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\\MyExcelSheet.xls;" + "Extended Properties=Excel 9.0"; OleDbConnection oledbConn = new OleDbConnection(cnstr); string strSQL = "SELECT * FROM [Sheet1
这篇关于Excel数据导入到网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文