Excel到c#.net中的Datagrid [英] Excel to Datagrid in c#.net

查看:75
本文介绍了Excel到c#.net中的Datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在没有数据库的情况下将所选的Excel工作表数据显示到数据网格中。如果有

Is this possible to display selcted excel sheet data into datagrid without database. send me snippet if there

推荐答案

发送给我片段你好,

看看这个



string connString =;

string strFileType =。xlsx;

string path = @C:\\\ nsr \ Book1.xlsx;

// Excel工作簿的连接字符串

if(strFileType.Trim()==。xls)

{

connString =Provider = Microsoft.Jet.OLEDB.4.0; Data Source =+ path +; Extended Properties = \Excel 8.0; HDR = Yes; IMEX = 2 \;

}

else if(strFileType.Trim()==。xlsx)

{

connString =Provider = Microsoft .ACE.OLEDB.12.0;数据源=+路径+;扩展属性= \Excel 12.0; HDR =是; IMEX = 2 \;

}



OleDbConnection con = new OleDbConnection(connString);

OleDbComm和cmdnew = new OleDbCommand();

cmdnew.CommandType = System.Data.CommandType.Text;

cmdnew.Connection = con;

OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmdnew);

DataTable dtExcelRecords = new DataTable();

con.Open();

DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);

string getExcelSheetName = dtExcelSheetName.Rows [0] [Table_Name]。ToString();

cmdnew.CommandText = SELECT * FROM [+ getExcelSheetName +];

dAdapter.SelectCommand = cmdnew;

dAdapter.Fill(dtExcelRecords);

Grd.DataSource = dtExcelRecords;

Grd.DataBind();

con.Close();

con.Dispose();
Hi,
Check this out

string connString = "";
string strFileType = ".xlsx";
string path = @"C:\Usr\Book1.xlsx";
//Connection String to Excel Workbook
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}

OleDbConnection con = new OleDbConnection(connString);
OleDbCommand cmdnew = new OleDbCommand();
cmdnew.CommandType = System.Data.CommandType.Text;
cmdnew.Connection = con;
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmdnew);
DataTable dtExcelRecords = new DataTable();
con.Open();
DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
cmdnew.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
dAdapter.SelectCommand = cmdnew;
dAdapter.Fill(dtExcelRecords);
Grd.DataSource = dtExcelRecords;
Grd.DataBind();
con.Close();
con.Dispose();


我想这个链接可以帮到你



http://stackoverflow.com/questions/11518344/excel-to-datagridview -without-using-hard-code [
i think so this link will help you

http://stackoverflow.com/questions/11518344/excel-to-datagridview-without-using-hard-code[


查看此解决方案,将选定的Excel工作表数据显示到gridview中。我认为它有助于你



string sourceFile =,worksheet =SheetName;



sourceFile = Server.MapPath(〜// Files // demo09.xls);



string strcnn =Provider = Microsoft.Jet.OLEDB.4.0; Data Source =+ sourceFile +;扩展属性= Excel 8.0;



OleDbConnection cnn = null;

OleDbCommand cmd = null;

OleDbDataAdapter da = null;





cnn = new OleDbConnection(strcnn);

cnn.Open();



cmd = new OleDbCommand(Select * from [+ workheet +
check out this solution to display selected excel sheet data into gridview. I think its helping to you

string sourceFile = "", worksheet = "SheetName";

sourceFile = Server.MapPath("~//Files//demo09.xls");

string strcnn = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " + sourceFile + ";Extended Properties=Excel 8.0";

OleDbConnection cnn = null;
OleDbCommand cmd = null;
OleDbDataAdapter da = null;


cnn = new OleDbConnection(strcnn);
cnn.Open();

cmd = new OleDbCommand("Select * from [" + worksheet + "


这篇关于Excel到c#.net中的Datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆