Monday, July 30, 2012

Excel Interop : System.Runtime.InteropServices.COMException (0x800A03EC): Microsoft Office Excel cannot access the file


Introduction

System.Runtime.InteropServices.COMException (0x800A03EC): Microsoft Office Excel cannot access the file 'D:\sunil\sunil.xls. There are several possible reasons: • the file name or path does not exist. •
This error occur on widows server 2008 r2 64 bit
Solution
Create folder Desktop.
Create directory "C:\Windows\SysWOW64\config\systemprofile\Desktop” (for 64 bit Windows) or "C:\Windows\System32\config\systemprofile\Desktop” (for 32 bit Windows)

Saturday, December 31, 2011

read csv file using streamreader in c#


DataTable dt = new DataTable();
string line = null;
int i = 0; 
using (StreamReader sr = File.OpenText(@"c:\Data_file.csv"))
{  
      while ((line = sr.ReadLine()) != null)
      {
            string[] data = line.Split(',');
            if (data.Length > 0)
            {
                  if (i == 0)
                  {
                  foreach (var item in data)
                  {
                        dt.Columns.Add(new DataColumn());
                  }
                  i++;
             }
             DataRow row = dt.NewRow();
             row.ItemArray = data;
             dt.Rows.Add(row);
             }
      }
}