刚学c#,现在写一个关于EXCEL的例程,可以从指定的EXCEL指定的SHEET中读取信息(存放DATAGRIDVIEW中),但我想把DATAGRIDVIEW修改后的记录重新存会EXCEL.此时就要在cs文件头增加 (using excel;),可总提示
The type or namespace name 'excel' could not be found (are you missing a using directive or an assembly reference?) C:\visual\chapter6\DataImport\DataImport\Form1.cs
我现在有EXCEL.DLL文件了。
在线等..
The type or namespace name 'excel' could not be found (are you missing a using directive or an assembly reference?) C:\visual\chapter6\DataImport\DataImport\Form1.cs
我现在有EXCEL.DLL文件了。
在线等..
using Excel = Microsoft.Office.Interop.Excel;
see:http://www.eggheadcafe.com/articles/20021012.asp
com
Microsoft Excel 12.0 Ojbect LibraryLS 正解
头文件已经包含“using Excel;”其是有效的:public static int Excel() {
Application exc = new Application();
if (exc == null) {
Console.WriteLine("ERROR: EXCEL couldn"t be started!");
return 0;
}exc.set_Visible(0, true);
Workbooks workbooks = exc.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0);
Sheets sheets = workbook.Worksheets;_Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
if (worksheet == null) {
Console.WriteLine ("ERROR: worksheet == null");
}Range range1 = worksheet.get_Range("C1", Missing.Value);
if (range1 == null) {
Console.WriteLine ("ERROR: range == null");
}
const int nCells = 1;
Object[] args1 = new Object[1];
args1[0] = nCells;
range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null,range1, args1);
return 100;
}
}
问题如下:
Error 1 'Application' is an ambiguous reference between 'System.Windows.Forms.Application' and 'Excel.Application' C:\visual\chapter6\DataImport\DataImport\Form1.cs 67 10 DataImportError 2 'Application' is an ambiguous reference between 'System.Windows.Forms.Application' and 'Excel.Application' C:\visual\chapter6\DataImport\DataImport\Form1.cs 67 32 DataImportError 3 The type 'System.Windows.Forms.Application' has no constructors defined C:\visual\chapter6\DataImport\DataImport\Form1.cs 67 28 DataImport
Application exc = new Application();
改为:
Excel.Application exc = new Excel.Application();
就不存在上面三个问题,可是又有很多其它问题。
private void ChangeExcel()
{
Excel.Application exc = new Excel.Application();
if (exc == null)
{
Console.WriteLine("ERROR: EXCEL couldn't be started!");
}
exc.Visible = true;
Workbooks workbooks = exc.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Sheets sheets = workbook.Worksheets;
_Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
if (worksheet == null)
{
Console.WriteLine("ERROR: worksheet == null");
} Range range1 = worksheet.get_Range("C1", Missing.Value);
if (range1 == null)
{
Console.WriteLine("ERROR: range == null");
}
const int nCells = 1;
Object[] args1 = new Object[1];
args1[0] = nCells;
range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range1, args1);
}