2016-06-22 6 views
0

私は、Microsoft.Office.Interop.Excelを使用してテキストファイルからExcelファイルを作成しています。ここ は私のコードファイル内のテキストを変換する

private void button1_Click(object sender, EventArgs e) 
    { 
     // Reading the text file - StreamReader include System.IO namespace 
     StreamReader objReader = new StreamReader(@"C:\Users\pci218\Desktop\TextToExcelFormApplication\TextToExcelFormApplication\Text\pop3.txt");// Please give the file path 
     string sLine = ""; 
     ArrayList arrText = new ArrayList();// Include System.Collections.Generic namespace 
     while (sLine != null) 
     { 
      sLine = objReader.ReadLine(); 
      if (sLine != null) 
       arrText.Add(sLine); 
     } 
     callExcel(arrText, true); 
    } 

    private void callExcel(ArrayList arrText, bool value) 
    { 
     try 
     { 
      // Change Your String here 
      String textString = null; 
      foreach (var item in arrText) 
      { 
       textString = textString + item + Environment.NewLine; 
      } 
      Clipboard.SetText(textString); 
      Microsoft.Office.Interop.Excel.Application xlexcel; 
      Microsoft.Office.Interop.Excel.Workbook xlWorkBook; 
      Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; 
      object misValue = System.Reflection.Missing.Value; 
      xlexcel = new Excel.Application(); 
      // for excel visibility 
      //xlexcel.Visible = true; 
      // Creating a new workbook 
      xlWorkBook = xlexcel.Workbooks.Add(misValue); 
      // Putting Sheet 1 as the sheet you want to put the data within 
      xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
      // creating the range 
      Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1]; 
      CR.Select(); 
      xlWorkSheet.Paste(CR, false); 
      if (value == true) 
      { 
       try 
       { 
        // saving the file as .xls 
        xlWorkSheet.SaveAs(@"C:\Users\U0153056\Desktop\receivedNew.xls"); 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("File already exist"); 
       } 
      } 
      else 
      { 
       try 
       { 
        // saving the file as .xlsx 
        xlWorkSheet.SaveAs(@"C:\Users\U0153056\Desktop\receivedNew.xlsx"); 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("File already exist"); 
       } 
      } 
      xlexcel.Quit(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString()); 
     } 
    } 

ですが、私は エラー「CLSIDを持つコンポーネントのCOMクラスファクトリを取得」取得メートル。私は自分のPCにインストールされたMicrosoft Officeを持っていません。問題はありますか?このエラーが発生しているためですか?誰でもアイデアがある.plsヘルプ。

答えて

0

残念ながら、Microsoft Office Excel 2007以降のバージョンがインストールされていないと、Microsoft.Office.Interop.Excelを使用することはできません。

... Microsoft Office Excel 2007およびMicrosoft Office Word 2007またはそれ以降のバージョンがコンピュータにインストールされている必要があります。詳細については、

チェックthisリンク。

+0

Microsoft.Office.Interop.Excelを使用せずにテキストをExcelに変換する他の方法はありません.. –

+0

いくつかの方法がありますが、それらは古くなっていますので、確かではありませんが[** this **] (http://stackoverflow.com/a/11448322/1652222)と[** this **](http://forums.asp.net/post/4203658.aspx)のリンクなどがあります。 – ManishChristian

+0

ありがとうございました........... –

関連する問題