2012-09-20 52 views

回答

2

我认为很容易创建OleDbDataAdapter并创建一个DataSet将完成这项工作。

您可以轻松地绑定DataSetgridview

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(); 
1

你应该使用任何一个库(OLEDB Connection, COM Object或任何其他)从Excel中读取数据,然后根据您的要求将结果赋予任何.Net对象(DataSet, DataTable)。然后将DataSet绑定到您的Repeater

1

也许这个链接将解决您的问题

click me

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; 
} 
+0

只需添加一些链接作为答案时要小心!该帖子发生了什么被删除?你的回答将被重定向到不存在的页面。一些代码与参考将是很好的!请看** http://meta.stackexchange。com/questions/8231/are-answers-that-just-contain-links-others-really-good-answers ** –

1

首先我们要浏览

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;"; 

谢谢:)

+0

问题是关于* ASP.NET *。你应该不得不更新它。 – adatapost