那位大师进来帮忙看看难题 我想对 excle 进行操作.........
在我想对 excel操作时 想写代码: Excel.Application app = new Excel.Application();
可是怎么也 NEW 不出来了 这是怎么回事 我在线等待 给高分的.......
我添加的引用是 microsoft excel 11.0 object library 类型库版本是 1.5 这个COM 组件
------解决方案--------------------在引用中using Excel = Microsoft.Office.Interop.Excel;
然后
Excel.Application excel = new Excel.Application();
------解决方案--------------------出异常了吗?什么异常。
------解决方案--------------------[C#]
using Excel;
_Application xlApp = null;
_Workbook xlWorkbook = null;
_Worksheet xlWorksheet = null;
System.Reflection.Missing oMissing = System.Reflection.Missing.Value;
string saveAsPath = "";
try
{
xlApp = new ApplicationClass();
xlApp.Visible = true;
xlWorkbook = xlApp.Workbooks.Add(oMissing);
xlWorksheet = xlWorkbook.Worksheets.Add(oMissing, oMissing, 1, oMissing) as _Worksheet;
xlWorksheet.Name = "NewWorksheet";
xlWorksheet.Cells[1, 1] = "Topic: ";
xlWorksheet.Cells[1, 2] = ".Net Interop Excel Demo";
saveAsPath = System.Windows.Forms.Application.StartupPath + "\\" + xlWorkbook.Name;
xlWorkbook.SaveAs(saveAsPath, oMissing, oMissing, oMissing, oMissing,
oMissing, Excel.XlSaveAsAccessMode.xlShared, oMissing, oMissing, oMissing,
oMissing, oMissing);
xlApp.Quit();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
GC.Collect();
}
------解决方案--------------------new ApplicationClass(); 用这个。
------解决方案-------------------- Workbook workbook = excelApp.Workbooks.Open(stExcelPath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
//创建工作簿
Worksheet workSheet = (Worksheet)workbook.ActiveSheet;
------解决方案--------------------你去.net 引用里面找 Microsoft.Office.Interop.Excel
然后写上面的代码。
我觉得是你因用错了。
你转到定义看看引用的是不是你想要的。双击看看 对象浏览器 都引用的什么。