2013-01-24 50 views
0

我已經寫的代碼,工程罰款。代碼需要傳輸的圖像以及文本到Excel單元格和客戶端的一臺計算機上工作正常,她得到的錯誤:如何導出datatable到excel 2002?

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Office.Interop.Excel, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies. The system cannot find the file specified. 
File name: 'Microsoft.Office.Interop.Excel, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' 

CODE出口DATATABLE TO EXCEL:

string ImageFolderPath = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\Images\\"; 

     SaveFileDialog saveFileDialog1=new SaveFileDialog(); 
     saveFileDialog1.Filter = "Excel (*.xls)|*.xls"; 
     // saveFileDialog1.Title = "Select Empty Excel Sheet"; 
     if (saveFileDialog1.ShowDialog() == DialogResult.OK) 
     { 
      if (!saveFileDialog1.FileName.Equals(String.Empty)) 
      { 
       FileInfo f = new FileInfo(saveFileDialog1.FileName); 
       if (f.Extension.Equals(".xls") || f.Extension.Equals(".xlsx")) 
       { 
        Microsoft.Office.Interop.Excel.Application xlApp; 
        Microsoft.Office.Interop.Excel.Workbook xlWorkBook; 
        Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; 
        object misValue = System.Reflection.Missing.Value; 

        xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); 
        xlWorkBook = xlApp.Workbooks.Add(misValue); 
        xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
        int i = 0; 
        int j = 0; 

        xlWorkSheet.Cells[3, 1] = "SKU"; 
        xlWorkSheet.Cells[3, 2] = "EAN IPC"; 
        xlWorkSheet.Cells[3, 3] = "ASIN"; 
        xlWorkSheet.Cells[3, 4] = "CONDITION"; 
        xlWorkSheet.Cells[3, 5] = "PRICE"; 
        xlWorkSheet.Cells[3, 6] = "PRODUCT TITLE"; 
        xlWorkSheet.Cells[3, 7] = "STATUS"; 
        xlWorkSheet.Cells[3, 8] = "PRODUCT IMAGE"; 
        xlWorkSheet.Cells[3, 9] = "PRODUCT URL"; 
        xlWorkSheet.Cells[3, 10] = "IMAGE NAME"; 


        xlWorkSheet.Cells[2, 1] = "Accept Value :"; 
        xlWorkSheet.Cells[2, 2] = textBox1.Text; 

        xlWorkSheet.Cells[2, 4] = "Location :"; 
        xlWorkSheet.Cells[2, 5] = "Amazon.co.uk"; 

        for (i = 0; i <= resultSheet.Rows.Count - 1; i++) 
        { 
         for (j = 0; j <= resultSheet.Columns.Count - 1; j++) 
         { 

          if (j == 7) 
          { 
           string imagString = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\Images\\"; 
           imagString = imagString + resultSheet.Rows[i][j+2].ToString(); 
           Microsoft.Office.Interop.Excel.Range oRange = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[i + 5, j + 1]; 
           float Left = (float)((double)oRange.Left); 
           float Top = (float)((double)oRange.Top); 
           const float ImageSize = 32; 
           xlWorkSheet.Shapes.AddPicture(imagString, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Left, Top, ImageSize, ImageSize); 
           oRange.RowHeight = ImageSize + 2; 

          } 
          else 
          { 
           xlWorkSheet.Cells[i + 5, j + 1] = resultSheet.Rows[i][j].ToString(); 

          } 
         } 
        } 

        xlWorkBook.SaveAs(saveFileDialog1.FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); 
        xlWorkBook.Close(true, misValue, misValue); 
        xlApp.Quit(); 

        Marshal.ReleaseComObject(xlWorkSheet); 
        Marshal.ReleaseComObject(xlWorkBook); 
        Marshal.ReleaseComObject(xlApp); 
        MessageBox.Show("Excel file created , you can find the file " + saveFileDialog1.FileName); 
       } 
       else 
       { 
        MessageBox.Show("Invalid file type"); 
       } 
      } 
      else 
      { 
       MessageBox.Show("You did pick a location " + 
           "to save file to"); 
      } 
     } 

客戶端使用的是excel 2002,所以不知道該怎麼辦!

+2

2002看起來像你引用了錯誤的版本/程序集不應該是版本10.x.x ..? – MethodMan

+0

您可能會發現這是您的問題的解決方案... http://amri-mlee.com/c-late-binding-excel-interop-tutorial-part-1/ –

+0

@SiddharthRout我使用的是文章中的代碼你附加,但無法找到如何將工作表添加到此應用程序.plz help – confusedMind

回答

2

這是一個關於如何與Excel進行後期綁定的簡單示例。這篇文章還包括以下

  1. 添加新的工作表(可根據您的要求)
  2. 重命名新添加的工作表
  3. 寫入細胞

我評論的代碼,所以你應該不會有任何問題的理解吧:)

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Reflection; 

namespace WindowsFormsApplication6 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      object xlApp; 
      object xlWbCol; 
      object xlWb; 
      object xlSheet; 
      object xlRange; 
      object xlWsCol; 

      //~~> create new Excel instance 
      Type tp; 
      tp = Type.GetTypeFromProgID("Excel.Application"); 
      xlApp = Activator.CreateInstance(tp); 

      object[] parameter = new object[1]; 
      parameter[0] = true; 
      xlApp.GetType().InvokeMember("Visible", BindingFlags.SetProperty, null, xlApp, parameter); 
      xlApp.GetType().InvokeMember("UserControl", BindingFlags.SetProperty, null, xlApp, parameter); 

      //~~> Get the xlWb collection 
      xlWbCol = xlApp.GetType().InvokeMember("Workbooks", BindingFlags.GetProperty, null, xlApp, null); 

      //~~> Create a new xlWb 
      xlWb = xlWbCol.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, xlWbCol, null); 

      //~~> Get the worksheet collection 
      xlWsCol = xlWb.GetType().InvokeMember("WorkSheets", BindingFlags.GetProperty, null, xlApp, null); 

      //~~> Create a new workxlSheet 
      xlSheet = xlWb.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, xlWsCol, null); 

      //~~> Rename the workxlSheet to your SO Handle 
      xlSheet.GetType().InvokeMember("Name", BindingFlags.SetProperty, null, xlSheet, new object[] { "confusedMind" }); 

      //~~> Assign cell D10 to xlRange object 
      xlRange = xlSheet.GetType().InvokeMember("Range", BindingFlags.GetProperty, null, xlSheet, new object[] { "D10" }); 

      //~~> Write to cell D10 
      xlRange.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, xlRange, new object[] { "Blah Blah" }); 

      //~~> Release the object 
      System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); 
     } 
    } 
} 

SCREENSHOT

enter image description here

隨訪

要添加圖像使用。更改如適用

object xlShape; 
xlShape = xlSheet.GetType().InvokeMember("Shapes", BindingFlags.GetProperty, null, xlSheet,null); 
String imagString = @"C:\Image.jpg"; 
xlSheet.GetType().InvokeMember("AddPicture", BindingFlags.InvokeMethod, null, xlShape, 
new object[] {imagString, false, true, 10, 10, 200, 200 }); 
+0

感謝很多,他們也不會有任何問題的圖像怎麼樣呢?他們現在會抄襲他們? – confusedMind

+0

示例我已經使用它,代碼的其餘部分工作得非常好,除了圖像部分,,,我如何包括它? – confusedMind

+0

我已更新上述帖子。見後面的部分。 –

0

消息很清楚;應用程序構建要求的Excel(2007)版本比客戶端安裝的Excel(2002)版本更新。

升級您的客戶端的Excel(2007)的新版本,或降級您的應用程序需要(如果你能找到一箇舊版本的Excel),或者切換到使用COM自動化Excel的,而不是對Excel互操作程序集。

+0

降級您的應用程序需要哪一個Excel的互操作程序集? – confusedMind

+0

您需要從版本12(應用程序需要的那個,根據引發的異常)降級到Excel 2002中使用的版本(10.x,我認爲?)。儘管如此,我不知道該怎麼說。我在答覆中說過。 :-) –

+0

該代碼甚至不適用於Excel 2010不知道爲什麼會出現同樣的錯誤? – confusedMind