2013-02-25 39 views
0

我有一個包含50個文本文件的文件夾,我需要一個可以讀取這些文本文件並將它們轉換爲表格的C#或.NET程序,我希望主鍵是文本文件本身的名稱。如何從文件夾中讀取文本文件並將其轉換爲MySQL中的表格

//sample contents of my 1.txt file is as follows 
atro 
astrology 
king 
moon 
monkey 
seven 
skin // 

所有文本文件都包含相同格式的信息。 我已經寫了一個宏,它可以讀取上述數據格式的文本文件,然後當我嘗試在Excel中運行宏時,我收到一個錯誤,指出內存不足。

enter code here 

子rameshc() ' ' 拉梅什宏 ' ' 快捷鍵:Ctrl + K ' 昏暗nxt_row只要

'Change Path 
Const strPath As String = "C:\Users\roo\Desktop\Volumes\eGo\tags\0\" 
Dim strExtension As String 

'Stop Screen Flickering 
Application.ScreenUpdating = False 

ChDir strPath 

'Change extension 
strExtension = Dir(strPath & "*.txt") 

Do While strExtension <> "" 

    'Adds File Name as title on next row 
    Range("A65536").End(xlUp).Offset(1, 0).Value = strExtension 

    'Sets Row Number for Data to Begin 
    nxt_row = Range("A65536").End(xlUp).Offset(1, 0).Row 

    'Below is from a recorded macro importing a text file 
    With ActiveSheet.QueryTables.Add(Connection:= _ 
     "TEXT;" & strPath & strExtension, Destination:=Range("$A$" & nxt_row)) 
     .Name = strExtension 
     .FieldNames = True 
     .RowNumbers = False 
     .FillAdjacentFormulas = False 
     .PreserveFormatting = True 
     .RefreshOnFileOpen = False 
     .RefreshStyle = xlInsertDeleteCells 
     .SavePassword = False 
     .SaveData = True 
     .AdjustColumnWidth = True 
     .RefreshPeriod = 0 
     .TextFilePromptOnRefresh = False 
     .TextFilePlatform = 850 
     .TextFileStartRow = 1 
     .TextFileParseType = xlDelimited 
     .TextFileTextQualifier = xlTextQualifierDoubleQuote 
     'Delimiter Settings: 
     .TextFileConsecutiveDelimiter = True 
     .TextFileTabDelimiter = True 
     .TextFileSemicolonDelimiter = True 
     .TextFileCommaDelimiter = True 
     .TextFileSpaceDelimiter = True 
     .TextFileOtherDelimiter = "=" 

     .TextFileTrailingMinusNumbers = True 
     .Refresh BackgroundQuery:=False 
    End With 

    strExtension = Dir 
Loop 

Application.ScreenUpdating = True 

結束子 子拉梅什() ' 'ramesh宏 ' '鍵盤快捷鍵:Ctrl + l ' Selection.Copy ActiveCell.Offset(0,1).Range(「A1」)。Select Selection.PasteSpecial Paste:= xlPasteAll,Operation:= xlNone,SkipBlanks:= _ False,Transpose:= True End Sub

+1

你能讀一個文件夾中的所有文件,並生成SQL代碼,將創建每個數據表的。所有你需要做的就是複製粘貼生成的SQL並在MySQL中執行它。 – 2013-02-25 10:59:45

+0

@rameshkumar我是新來的,所以我請你簡單介紹一下程序 – rameshkumar 2013-02-25 11:10:58

+0

你真正的問題是什麼?你卡在哪裏?我們不能爲你寫這個程序。 – mattmanser 2013-02-25 11:53:45

回答

1

最快的方法是:

  1. 創建控制檯應用程序。在您的文件夾內搜索您的文件。 http://msdn.microsoft.com/en-us/library/vstudio/ezwyzy7b.aspx
  2. 使用System.Data.SqlClient建立直接連接。 http://www.bigresource.com/MS_SQL--Problem-with-simple-sqlclient-database-acces--AVufrVBE.html
  3. 填寫您的插入聲明。迭代你的文件。併爲每一個插入。 http://www.w3schools.com/sql/sql_insert.asp

您將使用這樣的閱讀您的文件:

string[] lines = File.ReadAllLines("C:/YourFile.txt"); 

     foreach (var line in lines) 
     { 
      *Insert Statement* 
     } 
+0

Ramesh問MySQL,如果我沒有錯。 – 2013-02-25 13:05:15

+0

@IvanIvković你是對的。這裏有一個關於如何將C#連接到MySQL的好例子。 http://www.codeproject.com/Articles/43438/Connect-C-to-MySQL。代碼將按照相同的方式。 – 2013-02-25 13:10:05

+0

這是一個很好的教程,我已經嘗試過。 – 2013-02-25 13:10:52

相關問題