2011-09-13 2 views
1

私はCSV用のマクロを作成して、65キロ以上の行をインポートするようにしました。私はボタンをクリックしてC#からマクロを呼び出します。マクロでは、行単位でテキストファイルを読み込み、coloumnに追加するコードを書きました。最後に、coloumn全体を選択して、 "text to coloumns"をカンマ区切りで有効にしてExcelにエクスポートします。マクロは最初の行のみをエクスポートします

問題は、最初の行のみをエクスポートすることです。

親切に私を助けてください。あなたが期待するよう

Sub Read(FileName As String) 
'Dimension Variables 
     Dim ResultStr As String 
     'Dim FileName As String 
     Dim FileNum As Integer 
     Dim Counter As Double 
     'Ask User for File's Name 
     'FileName = InputBox("Please enter the Text File's name, e.g. test.txt") 
     'Check for no entry 
     If FileName = "" Then End 
     'Get Next Available File Handle Number 
     FileNum = FreeFile() 
     'Open Text File For Input 
     Open FileName For Input As #FileNum 
     'Turn Screen Updating Off 
     Application.ScreenUpdating = False 
     'Create A New WorkBook With One Worksheet In It 
     Workbooks.Add template:=xlWorksheet 
     'Set The Counter to 1 
     Counter = 1 
     'Loop Until the End Of File Is Reached 
     Do While Seek(FileNum) <= LOF(FileNum) 
     'Display Importing Row Number On Status Bar 
      Application.StatusBar = "Importing Row " & _ 
      Counter & " of text file " & FileName 
      'Store One Line Of Text From File To Variable 
      Line Input #FileNum, ResultStr 
      'Store Variable Data Into Active Cell 
      If Left(ResultStr, 1) = "=" Then 
      ActiveCell.Value = "'" & ResultStr 
      Else 
      ActiveCell.Value = ResultStr 
      End If 

      'For xl97 and later change 16384 to 65536 
      If ActiveCell.Row = 65536 Then 
       ActiveWorkbook.ActiveSheet.Columns(1).Select 
       Selection.TextToColumns DataType:=xlDelimited, _ 
       ConsecutiveDelimiter:=True, Space:=False, Comma:=True 
       'If On The Last Row Then Add A New Sheet 
       ActiveWorkbook.Sheets.Add 
      Else 
      'If Not The Last Row Then Go One Cell Down 
      ActiveCell.Offset(1, 0).Select 
      End If 
      'Increment the Counter By 1 
      Counter = Counter + 1 
     'Start Again At Top Of 'Do While' Statement 
     Loop 
     'Close The Open Text File 
     Close 
     ActiveWorkbook.ActiveSheet.Columns(1).Select 
     Selection.TextToColumns DataType:=xlDelimited, _ 
     ConsecutiveDelimiter:=True, Space:=False, Comma:=True 
     'Remove Message From Status Bar 
     Application.StatusBar = False 
End Sub 

Cシャープボタンのクリックイベント、

private void button1_Click(object sender, EventArgs e) 
     { 
      file_Writer = new StreamWriter(@"c:\tstf.csv",true); 
      //file_Writer.Close(); 
      //first row 
      for (int i = 0; i < 8; i++) 
      { 
       file_Writer.Write("A,"); 
      } 
      file_Writer.Write("\n"); 


      // second row 

      for (int i = 0; i < 8; i++) 
      { 
       file_Writer.Write("B,"); 
      } 

      file_Writer.Close(); 

      Thread.Sleep(1000); 


      object oMissing = System.Reflection.Missing.Value; 

      Excel.ApplicationClass oExcel = new Excel.ApplicationClass(); 
      oExcel.Visible = true; 
      Excel.Workbooks oBooks = oExcel.Workbooks; 
      Excel._Workbook oBook = null; 
      oBook = oBooks.Open("F:\\read.xls", oMissing, oMissing, 
       oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, 
       oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); 

      // Run the macros. 
      //RunMacro(oExcel, new Object[] { "DoKbTest" }); 
      RunMacro(oExcel, new Object[] { "siva1", @"c:\tstf.csv" }); 


      // Quit Excel and clean up. 
      oBook.Close(false, oMissing, oMissing); 
      System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook); 
      oBook = null; 
      System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks); 
      oBooks = null; 
      oExcel.Quit(); 
      System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel); 
      oExcel = null; 


      GC.Collect(); 
     } 
+0

Excelから呼び出すと機能しますか? –

+0

いいえ、Excelでも起動しません – Siva

+0

それはC#の問題ではありません。私はそのタグを削除しました。 –

答えて

1

問題の原因が見つかりました。これはキャリッジリターン "\ n"によるものです。 "\ n"の代わりに "\ r \ n"を行末に追加します。

file_Writer.Write("\r\n"); 
1

はどちらかあなたのDo While条件が機能していません。試してみてください:

Do until Eof(FileNum) 
    .... 
Loop 

および/または問題は出力ファイルを正しく閉じないためです。試してください:
Close Filenum

関連する問題