2014-09-02 2 views
0

interop assembly을 사용하여 excel sheetmulti dimensional C# list을 삽입하는 방법을 생각해 봅니다. 목록에 여러 행의 데이터가 8 열 이상 확산되어 있습니다. 내 쿼리를 실행하지만 그것은 나에게 COM Exception from HRESULT: 0x800A03EC을 던져 버리고 다음 코드Excel 시트에 다차원리스트를 삽입하는 중 예외가 발생했습니다

public void ExportStructureListToExcel(List<StructuresDS> listExport, string sheetName) 
{ 
    try 
    { 
     Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); 
     Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); 
     Microsoft.Office.Interop.Excel._Worksheet worksheet1 = null; 
     worksheet1 = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets["Sheet1"]; 
     worksheet1 = (Microsoft.Office.Interop.Excel._Worksheet)workbook.ActiveSheet; 

     for (int i = 1; i < listExport.Count + 1; i++) 
     { 
      for (int j = 1; j < 8; j++) 
      { 
        worksheet1.Cells[i, j] = listExport[i - 1]; 
      } 
     } 

     string fileDestination = @"S:\Parser Project\sde.xls"; 
     if (File.Exists(fileDestination)) 
     { 
      File.Delete(fileDestination); 
     } 

     workbook.SaveAs(fileDestination, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 
     workbook.Close(true, Type.Missing, Type.Missing); 
     Process.Start(fileDestination); 
     app.Quit(); 
    } 
    catch (Exception e) 
    { 
     MessageBox.Show(e.Message); 
    } 
} 

StructureDS 구조를 가지고

오류가 worksheet1.Cells[i, j] = listExport[i - 1]; 에서 발생되는
public class StructuresDS 
     { 
      public DateTime time; 
      public string CC; 
      public string term; 
      public string strike; 
      public string strategy; 
      public double? premium; 
      public int volume; 
      public double ratio; 
      public string over; 
     } 

**Inserting elements to the List** 



listStructures.Add(new StructuresDS 
       { 
        time = Convert.ToDateTime(AxiomSubSet[0].time.ToString("HH:mm:ss")), 
        CC = AxiomSubSet[0].CC, 
        term = listCodedTerms[0], 
        strike = (Convert.ToDouble(AxiomSubSet[0].strike) * 100).ToString(), 
        strategy = AxiomSubSet[0].strategy, 
        premium = Convert.ToDouble(AxiomSubSet[0].price), 
        volume = Convert.ToInt32(AxiomSubSet[0].quantity) 
       }); 

나는를 찾을 수 없습니다입니다 이것에 대한 해결책. 내가 틀린 곳을 알 수 있겠 니?

+0

오류를 발생시키는 행을 언급하지 않았습니다. – user845279

+0

worksheet1.Cells [i, j] = listExport [i - 1], 질문을 편집했습니다. – DoIt

+0

'worksheet1.Cells [i, j] .Value = listExport [i - 1];이 필요하다고 생각합니다. 또한, 나는 StructureDS가 무엇인지 알지 못하지만, 구조 자체보다는 그 속성이 필요할 수도 있습니다. –

답변

2

구조에 셀을 설정하려고합니다. 구조의 필드 중 하나에 설정해야합니다. 같은 뭔가 :

worksheet1.Cells[i, j].Value = listExport[i - 1].over; 

(.over가) 나는 당신이 실제로 셀에 넣어하려는 하나 모르는 beause 잘못된 필드가 될 수있다.

관련 문제