In this Code I am getting some error that class factory is not available. The Program is about converting csv(comma separated Values) file int xls(Microsoft Excel) file on the basis of semicolons. Can anybody help me out in it.

using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
namespace ReadWriteCSV
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application excel = new Excel.Application();
            Excel.Workbook workBook = excel.Workbooks.Add();
            Excel.Worksheet sheet = workBook.ActiveSheet;

            string fullPath = @"D:\Work\Sep-14\ReadWriteCSV\ReadWriteCSV\File\diff_16122014095440.csv";
            string[] fileRows = File.ReadAllLines(fullPath, Encoding.UTF8);

            foreach (string rows in fileRows)
            {
                var columns = rows.Split(';');

                for (int j = 0; j < fileRows.Length; j++)
                {
                    for (int i = 0; i < columns.Length; i++)
                    {
                        List<string> elements = new List<string>();
                        foreach (string col in columns)
                        {
                            elements.Add(col);
                            sheet.Cells[i + 1, j + 1] = col;
                        }
                        }
                    }
                }
            workBook.SaveAs(@"D:\Work\Sep-14\ReadWriteCSV\ReadWriteCSV\File\WriteXls.xlsx");
            workBook.Close();
            }
        }
    }

Related posts

Recent Viewed