Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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)
C#
col1.Add((string)(ws.Cells[currentRow, "A"] as Excel1.Range).Value2);

TO
C#
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?

C#
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
Updated 24-Oct-11 0:23am
v2

use this code to kill the instance of excel
C#
[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();
    

}
 
Share this answer
 
v2
Comments
M Bester 24-Oct-11 7:33am    
Does this kill the process that was used in the program?
To close excel completely, you have to close all COM objects.

C#
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900