I have a similar problem
Dear all
I build a new application using c#.net2005 to read excel file I write the following code but it gives me a run time error
((Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))
can any one help me
using System;
using System.Reflection; // For Missing.Value and BindingFlags
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using Microsoft.Office.Core;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
using Excel;
namespace ReadExcelFiletest
{
public partial class Form1 : Form
{
private Excel.Application ExcelObj = null;
private string strConn = "";
private string[,] myArray = new string[5000, 2];
private int myarrayindex;
public Form1()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
this.txtLocation.Text = openFileDialog1.FileName;
}
private void btnUpdate_Click(object sender, EventArgs e)
{
// string Path = @"c:\test.xls";
string MyPath = this.txtLocation.Text.Trim().ToString();
// initialize the Excel Application class
Excel.ApplicationClass app = new ApplicationClass();
// create the workbook object by opening the excel file.
Excel.Workbook workBook = app.Workbooks.OpenMyPath , 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
// get the active worksheet using sheet name or active sheet
Excel.Worksheet workSheet = (Excel.Worksheet)workBook.ActiveSheet;
int index = 0;
// This row,column index should be changed as per your need.
// i.e. which cell in the excel you are interesting to read.
object rowIndex = 1;
object colIndex1 = 1;
object colIndex2 = 2;
object colIndex3 = 3;
myarrayindex = 0;
try
{
while (((Excel.Range)workSheet.Cells[rowIndex, colIndex1]).Value2 != null)
{
rowIndex = index + 1;
string PartNO = ((Excel.Range)workSheet.Cells[rowIndex, colIndex1]).Value2.ToString();
string Price = ((Excel.Range)workSheet.Cells[rowIndex, colIndex2]).Value2.ToString();
myArray[myarrayindex, 0] = PartNO;
myArray[myarrayindex, 1] = Price;
ListViewItem lvi = new ListViewItem();
lvi.Text = Convert.ToString(index + 1);
lvi.SubItems.Add(PartNO);
lvi.SubItems.Add(Price);
this.listView1.Items.Add(lvi);
myarrayindex++;
index++;
}
}
catch (Exception ex)
{
app.Quit();
}
}
}
From http://www.developmentnow.com/g/21_2005_9_0_0_592882/Excel-templates-Old-format-or-invalid-type-library-.ht
Posted via DevelopmentNow.com Group
http://www.developmentnow.com