2013-09-11 131 views

回答

1

在VB.NET試試這個代碼:

Protected Sub btnExport(ByVal sender As Object, ByVal e As System.EventArgs) 
    Dim TheFile As FileInfo = New FileInfo(MapPath(".") & "\" & "filename.xls") 

    ' Connection String to Excel Workbook 2010 (xlsx) 
    ' Dim excelConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~\directory\filename.xlsx") + ";Extended Properties=""Excel 12.0 Xml;HDR=YES;""" 

    ' Connection String to Excel Workbook 2003 (xls) 
    Dim excelConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~\directory\filename.xls") + ";Extended Properties=""Excel 8.0;HDR=YES;""" 

    ' Create Connection to Excel Workbook 
    Using connection As New OleDbConnection(excelConnectionString) 
     Dim command As New OleDbCommand("Select * FROM [sheetname$] ", connection) 

     connection.Open() 

     ' Create DbDataReader to Data Worksheet 
     Using dr As DbDataReader = command.ExecuteReader() 

      ' SQL Server Connection String 
      Const sqlConnectionString As String = "Data Source=server; Initial Catalog=database; Persist Security Info=True;User ID=userid;Password=password" 

      ' Bulk Copy to SQL Server 
      Using bulkCopy As New SqlBulkCopy(sqlConnectionString) 
       bulkCopy.DestinationTableName = "SqlServerTableName" 
       bulkCopy.WriteToServer(dr) 
      End Using 
     End Using 
    End Using 
End Sub 

附加提示:嘗試設置IIS運行32位應用程序。

+0

此代碼適合我,非常感謝! – tollamie

0

是的,這是可能的。以下是可能的基本步驟鳥瞰:

  1. 上傳完成後 - 保存Excel下暫時唯一的文件名稱
  2. 打開OLEDB連接上傳文件
  3. 讀它(通過OleDbDataReader - 如果它是一個巨大的文件,所以您不必如果是小的將其加載到內存中一次,或到數據表)
  4. 運行查詢,存儲過程來更新的SqlServer數據庫與採集的數據
  5. 刪除已上傳的文件
+0

你可以給一個代碼示例嗎? – tollamie

+0

這是一個很常見的代碼,你可以在網上找到很多例子。例如Google「Path.GetRandomFileName」或「VB.NET讀取Excel文件OLEDB」或「DataTable TVP」 –

0

嘗試此

public void export(string excelfilepath) 
{ 

    string ssqltable = "tdatamigrationtable"; 

    string myexceldataquery = "select student,rollno,course from [sheet1$]"; 
    try 
    { 
     //create our connection strings 
     string sexcelconnectionstring = @"provider=microsoft.jet.oledb.4.0;data source=" + excelfilepath + 
     ";extended properties=" + "\"excel 8.0;hdr=yes;\""; 
     string ssqlconnectionstring = "server=mydatabaseservername;user 
     id=dbuserid;password=dbuserpassword;database=databasename;connection reset=false"; 

     //series of commands to bulk copy data from the excel file into our sql table 
     oledbconnection oledbconn = new oledbconnection(sexcelconnectionstring); 
     oledbcommand oledbcmd = new oledbcommand(myexceldataquery, oledbconn); 
     oledbconn.open(); 
      oledbdatareader dr = oledbcmd.executereader(); 
      sqlbulkcopy bulkcopy = new sqlbulkcopy(ssqlconnectionstring); 
      bulkcopy.destinationtablename = ssqltable; 
      while (dr.read()) 
      { 
       bulkcopy.writetoserver(dr); 
      } 

      oledbconn.close(); 
     } 
     catch (exception ex) 
     { 
      //handle exception 
     } 
    } 
0
Imports System.Data.Common 
Imports System.Data.SqlClient 

Public Class Form1 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     Dim fname As String 
     Using ofd As New OpenFileDialog 
      If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then 
       fname = ofd.FileName 

      End If 
     End Using 

     Dim olecon As String = "Provider=Microsoft.ACE.OLEDB.12.0 ;Data Source=" & fname & ";Extended Properties=""Excel 12.0;IMEX=1;HDR=YES;""" 

     Dim dBaseConnection As New System.Data.OleDb.OleDbConnection(olecon) 

     dBaseConnection.Open() 


     SSQL = "select [LOT],[IMAGE],[STYLENO],[VENDOR] from [Sheet1$]" 

     Dim cmd As New OleDbCommand(SSQL, dBaseConnection) 

     Dim da As New OleDbDataAdapter(cmd) 

     Dim ds As New DataSet 

     da.Fill(ds) 


     Using dr As DbDataReader = cmd.ExecuteReader 

      If SHCONNECTION.State = ConnectionState.Closed Then 
       Call SHconn(MCONNECTIONSTRING) 
      End If 

      Using bulkCopy As New SqlBulkCopy(MCONNECTIONSTRING) 
       bulkCopy.DestinationTableName = "DBimage" 
       bulkCopy.WriteToServer(ds) 
      End Using 
     End Using 
    End Sub 
End Class 
相關問題