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();
}
}