2017-11-02 2 views
0

enter image description hereC#の輸出DataGridViewの値いずれかがこの問題で私を助けて

をExcelに、私は本当に私はこのプログラムのために、このリンクのアプリケーションを使用していて、この問題が常に表示され、理解することはできません。

Hhereは、私がここに http://www.aspdotnet-pools.com/2015/03/export-gridview-data-to-excel-sheet.html

をやろうとしているリンクは絵

+0

あなたがこの記事を見たことがあるです:https://stackoverflow.com/questions/28066719/unable -to-cast-com-object-of-type-microsoft-office-interop-excel-applicationcla? –

+0

はい、私はその1つを見た、私はすでに同じことをして、私は私のコードでこのエラーを見て "この操作はIIDのインターフェイスのCOMコンポーネントのQueryInterfaceコール{000208D5-0000-0000 -C000-000000000046} 'が見つかりませんでした:ライブラリが登録されていません(HRESULTの例外:0x8002801D(TYPE_E_LIBNOTREGISTERED))。 – Kimie

+0

しかし、000208D5-0000-0000-C000-000000000046を私のregeditで見つけることができません。なぜなら、それは私が削除する必要があるかもしれないと思うからです。 – Kimie

答えて

0
//Try some thing like this... 
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Windows.Forms; 
    using System.Data.Sql; 
    using System.Data.SqlClient; 
    using System.Configuration; 


      void ExportTOExcel(DataGridView gridviewID) 
      { 


       Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); 

       Microsoft.Office.Interop.Excel.Workbook xlWorkBook; 
       Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; 
       object misValue = System.Reflection.Missing.Value; 

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

       //add data 
       int StartCol = 1;`enter code here` 
       int StartRow = 1; 
       int j = 0, i = 0; 

       //Write Headers 
       for (j = 0; j < gridviewID.Columns.Count; j++) 
       { 
        Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[StartRow, StartCol + j]; 
        myRange.Value2 = gridviewID.Columns[j].HeaderText; 
       } 

       StartRow++; 

       //Write datagridview content 
       for (i = 0; i < gridviewID.Rows.Count; i++) 
       { 
        for (j = 0; j < gridviewID.Columns.Count; j++) 
        { 
         try 
         { 
          Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[StartRow + i, StartCol + j]; 
          myRange.Value2 = gridviewID[j, i].Value == null ? "" : gridviewID[j, i].Value; 
         } 
         catch 
         { 
          ; 
         } 
        } 
       } 

       Microsoft.Office.Interop.Excel.Range chartRange; 

       Microsoft.Office.Interop.Excel.ChartObjects xlCharts = (Microsoft.Office.Interop.Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing); 
       Microsoft.Office.Interop.Excel.ChartObject myChart = (Microsoft.Office.Interop.Excel.ChartObject)xlCharts.Add(10, 80, 300, 250); 
       Microsoft.Office.Interop.Excel.Chart chartPage = myChart.Chart; 

       chartRange = xlWorkSheet.get_Range("A1", "B" + gridviewID.Rows.Count); 
       chartPage.SetSourceData(chartRange, misValue); 
       chartPage.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlColumnClustered; 

       xlApp.Visible = true; 

      } 

     } 
    } 
+0

感謝しました!! :D – Kimie

+0

このinteropはサーバーで実行することはお勧めしません。 –

+0

あなたの歓迎:) @キミ – SelakaN

関連する問題