2011-09-13 3 views
1

CSV를위한 매크로를 작성하여 65k 줄 이상 가져 오기를 수행했습니다. 나는 버튼 클릭을 통해 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

엑셀에서도 작동하지 않습니다. – Siva

+0

그렇다면 문제가 아닙니다. 나는 그 태그를 제거했다. –

답변

1

문제점의 원인을 찾았습니다. 이것은 캐리지 리턴 "\ n"때문입니다. "\ r \ n"대신 줄 끝에 "\ r \ n"을 추가합니다.

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

은 어느 당신의 Do While 조건은 작동하지 않습니다. 시도해보십시오 :

Do until Eof(FileNum) 
    .... 
Loop 

그리고/또는 문제는 출력 파일을 올바르게 닫지 않았기 때문입니다. 시도 :
Close Filenum