0

i m creating excel file from text file using Microsoft.Office.Interop.Excel; here is my code

    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());
        }
    }

but i m getting "Retrieving the COM class factory for component with CLSID" error. i dont have microsoft office installed in my PC.is tht the problem? due to that m getting this error? anyone have idea .pls help.

Neelam Prajapati
  • 3,764
  • 3
  • 28
  • 62

1 Answers1

0

Unfortunately, you can't use Microsoft.Office.Interop.Excel without having Microsoft Office Excel 2007 or later version installed.

... you must have Microsoft Office Excel 2007 and Microsoft Office Word 2007, or later versions, installed on your computer.

Check this link for more information.

ManishChristian
  • 3,759
  • 3
  • 22
  • 50
  • is there any other way to convert text to excel without using Microsoft.Office.Interop.Excel.. – Neelam Prajapati Jun 23 '16 at 04:01
  • There are several ways but they are outdated, so not sure on that but you can check [**this**](http://stackoverflow.com/a/11448322/1652222) and [**this**](http://forums.asp.net/post/4203658.aspx) links for example. – ManishChristian Jun 23 '16 at 13:11