无法将double类型转换为字符串//Excel数字为double? [英] Cannot convert type double to string // Excel numbers are double?

查看:611
本文介绍了无法将double类型转换为字符串//Excel数字为double?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图将单元格的数据添加到ArrayList中,但不断获取无法将类型Double转换为String".

单元格中的值为"1",类型为常规.当我拉入工作簿中的标题时,它们全部起作用.

[Edit1]我在打字时更改了(duh)

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.

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


现在一切正常.

所以我现在的问题是,Excel中的所有数字都是双精度吗?

[Edit2]我在任务管理器"中查看了很多Excel.exe的打开信息,如何关闭它们?


and everything works now.

So my question now is, are all numbers from excel doubles?

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

推荐答案

使用此代码杀死excel的实例
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();
    

}


要完全关闭excel,必须关闭所有COM对象.

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



其中objectRng是Range对象,而app是excel实例.
如果可能的话,不要使用进程终止.



Where objectRng is a Range object, and app is the excel instance.
If possible do not use process kills.


这篇关于无法将double类型转换为字符串//Excel数字为double?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆