2010-03-09 45 views

Respuesta

7

Prueba el siguiente código C#:

Dim MyCommand As System.Data.OleDb.OleDbDataAdapter 
Dim MyConnection As System.Data.OleDb.OleDbConnection 
MyConnection = New System.Data.OleDb.OleDbConnection(_ 
"provider=Microsoft.Jet.OLEDB.4.0; " & _ 
"data source=" & ExcelFilePath & "; " & _ 
"Extended Properties=Excel 8.0") 

' Select the data from Sheet1 ([in-house$]) of the workbook. 
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [in-house$]", MyConnection) 

DS = New System.Data.DataSet 
MyCommand.Fill(DS) 
Dt = DS.Tables(0) 
DataGrid1.DataSource = Dt 

Para probar este particular, celular (D6 celular que va a leer). El punto a tener en cuenta es que no está utilizando la conexión OLEDB sino que está accediendo directamente.

Espacio de nombres requerido usando Microsoft.Office.Core;

agregarlo mediante la adición de referencia de COM para Microsoft Office 12.0 Object Library

Dim oApp As New Excel.Application 
Dim oWBa As Excel.Workbook = oApp.Workbooks.Open("c:\Test.XLS") 
Dim oWS As Excel.Worksheet = DirectCast(oWBa.Worksheets(1), 
Excel.Worksheet) 
oApp.Visible = False 

Dim oRng As Excel.Range 
oRng = oWS.Range("D6") 
MsgBox(oRng.Value) 
+0

Si se lee todos los archivos de Excel. ¿Cómo puedo leer una celda específica (es decir: me refiero a cómo puedo leer A11 en la hoja de Excel?)? Su conjunto de códigos funciona para leer todo el archivo. Gracias. – RedsDevils

+0

¿Qué necesito para importar para usar como Excel.Application? – RedsDevils

+0

Ok lo tengo para referencia COM. Gracias, probaré tu código. – RedsDevils

0

es un componente de hoja de cálculo compatible con Excel para .NET que se puede utilizar para obtener la fórmula, el valor, texto formateado, etc. de cualquier celda. Aquí está un ejemplo sencillo:

using System; 
using SpreadsheetGear; 

namespace Program 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // Load a workbook from disk and get the first worksheet. 
      IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook(@"C:\tmp\HelloWorld.xlsx"); 
      IWorksheet worksheet = workbook.Worksheets[0]; 
      // Get a reference to cell A1 and write the formatted value to the console. 
      IRange a1 = worksheet.Cells["A1"]; 
      Console.WriteLine("A1={0}", a1.Text); 
      // Get a reference to B2 and write the formula/value/text to the console. 
      IRange b2 = worksheet.Cells[1, 1]; 
      Console.WriteLine("B2 Formula={0}, Value={1}, Text={2}", b2.Formula, b2.Value, b2.Text); 
     } 
    } 
} 

Se puede ver muestras vivas here o descargar la versión de prueba here si desea hacerlo por uno mismo.

exención de responsabilidad: Tengo SpreadsheetGear LLC

0

prueba este código C#,

DimobjEXCELCon As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=EXCLE_FILE_PATH;Extended Properties=""Excel 12.0 Xml;HDR=Yes""") 
ExcelConnection.Open() 

Dim objQuery As String = "SELECT * FROM [Sheet1$]" 'get values from sheet1, here you can change your sheet name 

Dim objCMD As OleDbCommand = New OleDbCommand(objQuery,objEXCELCon) 
Dim objDR As OleDbDataReader 

Dim SQLconn As New SqlConnection() 
Dim szCON As String = "Connection string for database" 
SQLconn.ConnectionString = szCON 
SQLconn.Open() 


Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(SQLConn) 
bulkCopy.DestinationTableName = "TableToWriteToInSQLSERVER" 

Try 
    objDR = objCMD.ExecuteReader 
    bulCopy.WriteToServer(objDR) 
    objDR.Close() 
    SQLConn.Close() 

Catch ex As Exception 
    MsgBox(ex.ToString) 
End Try 
Cuestiones relacionadas