2013-08-14 1 views
0

C#을 사용하여 기존 csv 파일에서 기존 Excel 매크로 (VB로 작성, Excel에서 매크로 편집기로 복사 가능)를 수행해야합니다. 지정된 파일은 csv 파일의 경우C#에서 csv 파일에 Excel 매크로 수행

using System; 
using Excel = Microsoft.Office.Interop.Excel; 

namespace MacroBuddy 
{ 
    public class test 
    { 
     public static void go_Macro() 
     { 
      object oMissing = System.Reflection.Missing.Value; 

      //create new Excel application instance 
      Excel.Application oExcel = new Excel.Application(); 

      oExcel.Visible = true; 

      Excel.Workbooks oBooks = oExcel.Workbooks; 
      Excel._Workbook oBook = null; 

      string path = @"C:\Users\user\Desktop\test.csv"; 

      //open file located at path 
      oBook = oBooks.Open(path, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); 

      //run Macro by referencing file and the name of the Macro 
      RunMacro(oExcel, new Object[] { "test.xlsm!TestMacro" }); 

      //save and close workbook 
      oBook.Save(); 
      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(); 
     } 

     private static void RunMacro(object oApp, object[] oRunArgs) 
     { oApp.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs); } 

     static void Main() 
     { go_Macro(); } 
    } 
} 

그러나, 그것은 작동하지 않습니다 : 나는 이미 내가 같이 보이는, XLSM 파일에 매크로를 수행하는 데 사용할 수있는 작업 코드가 있습니다. 그래서 csv 파일에서 유사한 코드 작업을하거나 Csv 파일을 xlsm 파일에서 C#으로 변환하는 자동화 된 프로세스를 만드는 데 도움이 필요합니다.

또한 VB 매크로 코드를 문자열로 사용하고 문자열을 인수 또는 유사한 프로세스로 사용하는 방법을 사용하여 매크로를 실행할 수있는 것이 도움이됩니다.

답변

0

, 나는 이런 식으로 뭔가를 사용하여 여러 시트가있는 .XLSX 파일에 쓰기로했다 :

public class test 
{ 
    object missing = Type.Missing; 
    public test() 
    { 
     Excel.Application XL = new Excel.Application(); 
     oXL.Visible = false; 
     Excel.Workbook WB = XL.Workbooks.Add(missing); 
     Excel.Worksheet Sheet = WB.ActiveSheet as Excel.Worksheet; 
     oSheet.Name = "First sheet"; 
     oSheet.Cells[1, 1] = "Written on first sheet"; 
     Excel.Worksheet Sheet2 = WB.Sheets.Add(missing, missing, 1, missing) 
        as Excel.Worksheet; 
     Sheet2.Name = "Second sheet"; 
     Sheet2.Cells[1, 1] = "Written on second sheet"; 
     string fileName = @"C:\temp\SoSample.xlsx"; 
     oWB.SaveAs(fileName, Excel.XlFileFormat.xlOpenXMLWorkbook, 
      missing, missing, missing, missing, 
      Excel.XlSaveAsAccessMode.xlNoChange, 
      missing, missing, missing, missing, missing); 
     oWB.Close(missing, missing, missing); 
     oXL.UserControl = true; 
     oXL.Quit(); 
    } 
} 

그리고 난 다음에 통합 문서로 만들어 코드 :

private void button1_Click(object sender, System.EventArgs e) 
{ 
     Excel.Application oExcel; 
     Excel.Workbook oBook; 
     VBIDE.VBComponent oModule; 
     Office.CommandBar oCommandBar; 
     Office.CommandBarButton oCommandBarButton; 
     String sCode; 
     Object oMissing = System.Reflection.Missing.Value; 
     // Create an instance of Excel. 
     oExcel = new Excel.Application(); 

     // Add a workbook. 
     oBook = oExcel.Workbooks.Add(@"C:\Users\user\Downloads\test.xlsm"); 

     // Create a new VBA code module. 
     oModule = oBook.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule); 

     sCode = 
     //paste in your macro here, with each line followed by a new line 
     "Sub TestMacro()\r\n" + 
     "Columns(\"D:D\").Select\r\n" + 
     "Selection.Copy\r\n" + 
     "Columns(\"F:F\").Select\r\n" + 
     "ActiveSheet.Paste\r\n" + 
     "Application.CutCopyMode = False\r\n" + 
     "ActiveSheet.Range(\"$F$1:$F$542\").RemoveDuplicates Columns:=1, Header:=xlNo\r\n" + 
     "Range(\"F1\").Select\r\n" + 
     "ActiveCell.FormulaR1C1 = \"Unique Query\"\r\n" + 
     "Range(\"F2\").Select\r\n" + 
     "End Sub"; 
     // Add the VBA macro to the new code module. 
     oModule.CodeModule.AddFromString(sCode); 



     // Make Excel visible to the user. 
     oExcel.Visible = true; 
     // Set the UserControl property so Excel won't shut down. 
     oExcel.UserControl = true; 

     // Release the variables. 
     oModule = null; 
     oBook = null; 
     oExcel = null;    
     // Collect garbage. 
     GC.Collect(); 
} 

는 그때 그냥 내가 원래 게시 된 코드를 사용하여 만든 매크로를 실행 할 수 있었다.