OS: Windows 7 - 64 bit
Office 2016 Professional - 64 bit
Before I uninstalled Excel 97, the code below did run.
After I uninstalled Excel 97 and then I installed Office 2016 Professional the code below now has an error.
Error CS0246 The type or namespace name 'Excel' could not be found
(are you missing a using directive or an assembly reference?)
Wherever the Excel.Application, Excel.Workbook, Excel.Worksheet and Excel.Range is referenced it flagged an error.
Excel 2016 is currently installed, but Excel 97 is not installed.
/////////////////////////////////////////////////////////////////////////////////
Office 2016 Professional - 64 bit
Before I uninstalled Excel 97, the code below did run.
After I uninstalled Excel 97 and then I installed Office 2016 Professional the code below now has an error.
Error CS0246 The type or namespace name 'Excel' could not be found
(are you missing a using directive or an assembly reference?)
Wherever the Excel.Application, Excel.Workbook, Excel.Worksheet and Excel.Range is referenced it flagged an error.
Excel 2016 is currently installed, but Excel 97 is not installed.
/////////////////////////////////////////////////////////////////////////////////
C#:
private void Button_Read_Excel_File_Click(object sender, EventArgs e)
{
Excel.Application xlApp; <<----- ERROR CODE IS FLAGGED HERE
Excel.Workbook xlWorkBook; <<----- ERROR CODE IS FLAGGED HERE
Excel.Worksheet xlWorkSheet; <<----- ERROR CODE IS FLAGGED HERE
Excel.Range range; <<----- ERROR CODE IS FLAGGED HERE
string str = "ABC";
int rCnt;
int cCnt;
int rw = 0;
int cl = 0;
DateTime trueDate;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(@"C:\Users\Linh\Desktop\C_Sharp_Programming\csharp-Excel.xls", 0,
true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
rw = range.Rows.Count;
cl = range.Columns.Count;
Excel.Application excelApp = new Excel.Application(); //Add on 11-18-2022
Excel.Workbook excelWorkbook = excelApp.Workbooks.Add(); //Add on 11-18-2022
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelWorkbook.Sheets.Add(); //Add on 11-18-2022
for (rCnt = 1; rCnt <= rw; rCnt++)
{
for (cCnt = 1; cCnt <= cl; cCnt++)
{
if ((rCnt != 1) && (cCnt == 2))
{
trueDate = DateTime.FromOADate((range.Cells[rCnt, cCnt] as Excel.Range).Value2);
excelWorksheet.Cells[rCnt, cCnt] = trueDate;
MessageBox.Show(trueDate.ToString("MM-dd-yyyy"));
}
else
{
str = (range.Cells[rCnt, cCnt] as Excel.Range).Value2.ToString();
excelWorksheet.Cells[rCnt, cCnt] = str;
MessageBox.Show(str);
}
}
}
xlWorkBook.Close(true, null, null);
xlApp.Quit();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
excelWorkbook.SaveAs
(@"C:\Users\Linh\Desktop\C_Sharp_Programming\abc.xls", Excel.XlFileFormat.xlWorkbookNormal);
excelWorkbook.Close();
excelApp.Quit();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelWorksheet);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelWorkbook);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelApp);
GC.Collect();
GC.WaitForPendingFinalizers();
}
Last edited: