2014-12-18 2 views
-2

가 나는 CSV (공통 선택한 값 파일) XLS로 파일을 변환하는 프로그램 (Microsoft는 파일을 엑셀) 파일을 만드는 중이라서 객체 오류의 인스턴스로 설정되지해결하는 방법 개체 참조가

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Excel = Microsoft.Office.Interop.Excel; 


namespace ConversionToXLSFile 
{ 
    public partial class Converter : System.Web.UI.Page 
    { 
     //Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); 
     Excel.Application xlApp; 
     Excel.Workbook xlWorkBook; 
      Excel.Worksheet xlWorkSheet; 
      object misValue = System.Reflection.Missing.Value; 

      //xlWorkBook = xlApp.Workbooks.Add(misValue); 
      //xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      //List<string> row = new List<string>(); 

      string fullPath = @"D:\Work\Sep-14\ConversionToXLSFile\ConversionToXLSFile\File\diff_16122014095440.csv"; 
      string[] fileRows = File.ReadAllLines(fullPath, Encoding.UTF8); 

      foreach (string rows in fileRows) 
      { 
       var columns = rows.Split(';'); 

       for (int j = 0; j < fileRows.Length; j++) 
       { 
        for (int i = 0; i < columns.Length; i++) 
        { 
         List<string> elements = new List<string>(); 
         foreach (string col in columns) 
         { 
          elements.Add(col); 
          xlWorkSheet.Cells[j, i] = col; 
         } 
        } 
       } 
      } 




      xlWorkBook.SaveAs("d:\\csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); 
      xlWorkBook.Close(true, misValue, misValue); 
      xlApp.Quit(); 

      releaseObject(xlWorkSheet); 
      releaseObject(xlWorkBook); 
      releaseObject(xlApp); 
     } 

     private void releaseObject(object obj) 
     { 
      try 
      { 
       System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); 
       obj = null; 
      } 
      catch (Exception ex) 
      { 
       obj = null; 
       Console.WriteLine("Exception Occured while releasing object " + ex.ToString()); 
      } 
      finally 
      { 
       GC.Collect(); 
      } 
     } 
     private void Add(dynamic dynamic) 
     { 
      throw new NotImplementedException(); 
     } 

     } 
} 
+1

CSV = 쉼표로 구분 된 값 – Liath

답변

2

지금까지 내가 볼 수있는, 당신은 당신이 변수 xlApp, xlWorkBook 및 xlWorkSheet을 할당 코드 ...

//Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); 
//xlWorkBook = xlApp.Workbooks.Add(misValue); 
//xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 

이 가라을 주석 한 // 각 라인의 밖으로 당신은 null 참조 예외 경우를받지 않습니다 당신은 그들을 사용하려고합니다.

관련 문제