Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Excel .NET4
Trying to add the cell's data to an ArrayList but keep getting 'Cannot convert type Double To String'.
 
The value in the cell is '1' and the type is general. When I pulled in the headers in the workbook, they all worked.
 
[Edit1]While I was typing I changed (duh)
col1.Add((string)(ws.Cells[currentRow, "A"] as Excel1.Range).Value2);

TO
col1.Add((double)(ws.Cells[currentRow, "A"] as Excel1.Range).Value2);

and everything works now.
 
So my question now is, are all numbers from excel doubles?
 
[Edit2]I looked in Task Manager and there are a whole lot of Excel.exe's open, how do I close them?
 
public void ReadSheet()
		{
			try
			{
				Excel1.Application app = new Excel1.Application();
				Excel1.Workbook wb = app.Workbooks.Open(this.txtBoxFile.Text, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
				Excel1.Sheets sheets = wb.Worksheets;
				Excel1.Worksheet ws = (Excel1.Worksheet)sheets[1];
				int currentRow = 2;
				while ((ws.Cells[currentRow, "A"] as Excel1.Range).Value2 != null && (ws.Cells[currentRow, "B"] as Excel1.Range).Value2 != null)
				{
					MessageBox.Show("1");
					col1.Add((string)(ws.Cells[currentRow, "A"] as Excel1.Range).Value2);
					col2.Add((string)(ws.Cells[currentRow, "B"] as Excel1.Range).Value2);
					col3.Add((string)(ws.Cells[currentRow, "C"] as Excel1.Range).Value2);
					col4.Add((string)(ws.Cells[currentRow, "D"] as Excel1.Range).Value2);
					MessageBox.Show("2");
					currentRow++;
				}
				GC.Collect();
				GC.WaitForPendingFinalizers();
				
				Marshal.FinalReleaseComObject(ws);
				Marshal.FinalReleaseComObject(sheets);
				
				wb.Close(false, Type.Missing, Type.Missing);
				Marshal.FinalReleaseComObject(wb);
				
				app.Quit();
				Marshal.FinalReleaseComObject(app);
			}
			catch(InvalidCastException ic)
			{
				MessageBox.Show(ic.Message, "CastException");
			}
			catch (Exception e)
			{
				MessageBox.Show(e.Message, "Exception");
			}
			finally
			{
				//
				ExcelToTxt();
			}
		}
Posted 24-Oct-11 1:20am
Edited 24-Oct-11 1:23am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

use this code to kill the instance of excel
[DllImport("user32")]
private static extern IntPtr GetWindowThreadProcessId(int hWnd,ref IntPtr lpdwProcessId);
 
private void KillProcess(Microsoft.Office.Interop.Excel.Application exl)
{
    IntPtr processId = new IntPtr();
    GetWindowThreadProcessId(exl.Hwnd,ref processId);
    Process excelProcess = Process.GetProcessById(processId.ToInt32());
    excelProcess.Kill();
    
 
}
  Permalink  
v2
Comments
M Bester at 24-Oct-11 7:33am
   
Does this kill the process that was used in the program?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

To close excel completely, you have to close all COM objects.
 
public void CloseXLS(String savedFileName)
{
    // Cleanup
    GC.Collect();
    GC.WaitForPendingFinalizers();
 
    Marshal.FinalReleaseComObject(objectRng);
    Marshal.FinalReleaseComObject(worksheet);
 
    workbook.Close(true, savedFileName, Type.Missing);
    Marshal.FinalReleaseComObject(workbook);
 
    app.Quit();
    Marshal.FinalReleaseComObject(app);
}
 
Where objectRng is a Range object, and app is the excel instance.
If possible do not use process kills.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 3 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100