什么是高效&将数据从Excel表单绑定到中继器或GridView的最简单方法。将Excel表格中的数据绑定到中继器或GridView
回答
我认为很容易创建OleDbDataAdapter
并创建一个DataSet
将完成这项工作。
您可以轻松地绑定DataSet
到gridview
如
var conn = ("Provider=Microsoft.Jet.OLEDB.4.0;" +
("Data Source=add file path here;" +
"Extended Properties=\"Excel 8.0;\""));
var query = "SELECT table from [sheet1$]";
var adpterObj = new OleDbDataAdapter(SSQL, conn);
var ds = new DataSet();
adpterObj.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
你应该使用任何一个库(OLEDB Connection, COM Object
或任何其他)从Excel中读取数据,然后根据您的要求将结果赋予任何.Net对象(DataSet, DataTable
)。然后将DataSet
绑定到您的Repeater
。
也许这个链接将解决您的问题
public static DataSet ImportExcelXLS(string FileName, bool hasHeaders) {
string HDR = hasHeaders ? "Yes" : "No";
string strConn;
if (FileName.Substring(FileName.LastIndexOf('.')).ToLower() == ".xlsx")
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
else
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\"";
DataSet output = new DataSet();
using (OleDbConnection conn = new OleDbConnection(strConn)) {
conn.Open();
DataTable schemaTable = conn.GetOleDbSchemaTable(
OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
foreach (DataRow schemaRow in schemaTable.Rows) {
string sheet = schemaRow["TABLE_NAME"].ToString();
if (!sheet.EndsWith("_")) {
try {
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheet + "]", conn);
cmd.CommandType = CommandType.Text;
DataTable outputTable = new DataTable(sheet);
output.Tables.Add(outputTable);
new OleDbDataAdapter(cmd).Fill(outputTable);
} catch (Exception ex) {
throw new Exception(ex.Message + string.Format("Sheet:{0}.File:F{1}", sheet, FileName), ex);
}
}
}
}
return output;
}
只需添加一些链接作为答案时要小心!该帖子发生了什么被删除?你的回答将被重定向到不存在的页面。一些代码与参考将是很好的!请看** http://meta.stackexchange。com/questions/8231/are-answers-that-just-contain-links-others-really-good-answers ** –
首先我们要浏览
void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Filter = "Excel files (*.xls)|*.xls";
fileDialog.InitialDirectory = "C:";
fileDialog.Title = "Select a Excel file";
if (fileDialog.ShowDialog() == DialogResult.OK)
txtMsg.Text = fileDialog.FileName;
if (string.IsNullOrEmpty(txtMsg.Text))
return;
}
注意:txtMsg.Text = fileDialog.FileName; //此处文件名保存在一个文本框中
然后我们就可以上传Excel工作表到gridview的...
private void btnUpload_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtMsg.Text))
{
InsertBuyBackExceldata(txtMsg.Text);
}
}
在这里,我们可以调用InsertBuyBackExceldata方法
void InsertBuyBackExceldata(string filePath)
{
if (buyBackServiceProxyController == null)
buyBackServiceProxyController = new ProxyController();
buyBackServiceServiceProxy = buyBackServiceProxyController.GetProxy();
ListBuyBackrequest = new List();
String[] a= GetExcelSheetNames(filePath); // This method for get sheet names
var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", filePath);
//string a=filePath.
String sheetName = a[0];
var adapter = new OleDbDataAdapter("SELECT * FROM [" + sheetName + "]", connectionString);
var ds = new DataSet();
adapter.Fill(ds, sheetName);
DataTable data = ds.Tables[sheetName ];
BindGrid(data);
}
这里我打电话一方法来获取图纸名称。
private String[] GetExcelSheetNames(string excelFile)
{
OleDbConnection objConn = null;
System.Data.DataTable dt = null;
try
{
String connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + excelFile + ";Extended Properties=Excel 8.0;";
objConn = new OleDbConnection(connString);
objConn.Open();
dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return null;
}
String[] excelSheets = new String[dt.Rows.Count];
int i = 0;
foreach (DataRow row in dt.Rows)
{
excelSheets[i] = row["TABLE_NAME"].ToString();
i++;
}
return excelSheets;
}
catch (Exception ex)
{
return null;
}
finally
{
if (objConn != null)
{
objConn.Close();
objConn.Dispose();
}
if (dt != null)
{
dt.Dispose();
}
}
}
// Excel中上传到GridView控件
void BindGrid(DataTable Data)
{
try
{
// Adding one check box
DataGridViewCheckBoxColumn chkSelection = new DataGridViewCheckBoxColumn();
chkSelection.Name = "Selection";
dgItemsForBuyBackGrid.Columns.Add(chkSelection);
int intCount = Data.Rows.Count;
if (i==true)
{
if (intCount > 0)
{
ExcelGrid.DataSource = Data;
ExcelGrid.BindPage();
}
else
{
ExcelGrid.DataSource = null;
ExcelGrid.BindPage();
return;
}
// Here I am setting Grid colomns properties this name should equal to Excel //column names.
ExcelGrid.Columns["BI"].ReadOnly = true;
ExcelGrid.Columns["AAA"].ReadOnly = true;
ExcelGrid.Columns["AAB"].ReadOnly = true;
ExcelGrid.Columns["AAC"].ReadOnly = true;
ExcelGrid.Columns["AAD"].ReadOnly = true;
ExcelGrid.Columns["AAE"].ReadOnly = true;
ExcelGrid.Columns["AAF"].ReadOnly = true;
ExcelGrid.Columns["AAG"].ReadOnly = false;
}
else
{
// Some Code
}
}
catch (Exception ex)
{
}
}
}
}
Excel 2007和其他版本的.Net上传来的GridView。
步骤: 1)AccessDatabaseEngine.exe - >(安装) 而一些代码更改从上面的代码。
2)浏览单击
private void btnBrowse_Click(object sender, EventArgs e)
{
-------
fileDialog.Filter ="Excel files (*.xls;*xlsx;*xlsb)|*.xls;*xlsx;*xlsb";
// Show those extentional files.
--------------
}
3)过程点击
string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=Excel 12.0;";
谢谢:)
问题是关于* ASP.NET *。你应该不得不更新它。 – adatapost
- 1. 将数据绑定到中继器
- 2. 绑定列表到数据中继器
- 3. 将gridview格式化的数据绑定到新的gridview中
- 4. 将GridView绑定到动态数据表
- 5. 将两个sql表数据绑定到一个中继器
- 6. 使用jquery将excel表格中的数据导出到gridview
- 7. 如何绑定中继器内的gridview?
- 8. 将数据绑定到数据中继器 - VB.NET?
- 9. 中继器中的数据绑定到下拉列表C#
- 10. 将中继器绑定到文件和/或文件夹列表
- 11. 将数据网格绑定到表格
- 12. 将数据表值绑定到GridView中的复选框
- 13. GridView中的数据绑定
- 14. 的GridView内的中继器ondataitembound网格视图重新绑定
- 15. 将序列化的JSON数据绑定到中继器
- 16. 使用#eval()将多个表中的数据绑定到中继器
- 17. 将数据库数据绑定到ASP.Net中的GridView
- 18. 绑定中继器的数据源
- 19. 将数据绑定到Access(或SQL)数据源的Excel字段
- 20. 将excel文件保存到磁盘后从中继器绑定
- 21. 将GridView绑定到列表
- 22. 使用Ajax将数据绑定到中继器
- 23. Asp.Net将SQL数据绑定到中继器?
- 24. 将中继器绑定到记录?
- 25. 将DataReader绑定到ASP.NET中继器
- 26. 绑定中继器到堆栈列表
- 27. 将数据表绑定到jTable网格
- 28. 将数据绑定到gridview时出错
- 29. 将IEnumerable数据绑定到gridview
- 30. 将gridview数据绑定到数据表时出现错误
你是如何阅读Excel文件?使用OLEDB连接,Excel库或COM对象? – Icarus
@Icarus我正在使用OLEDB连接来读取excel。 – Abhijeet
@lcarus:如何在使用COM对象时绑定..? – Kulkarni