你是要打开这个文件吧?
private void OpenExcelFile(string sPath,string sFileName)
{
try
{
Directory.SetCurrentDirectory(sPath);
Excel_App.Workbooks.Open(sPath+sFileName,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing);
}
catch(Exception e)
{
throw e;
}
}
private void OpenExcelFile(string sPath,string sFileName)
{
try
{
Directory.SetCurrentDirectory(sPath);
Excel_App.Workbooks.Open(sPath+sFileName,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing);
}
catch(Exception e)
{
throw e;
}
}
下面就让我们具体看看C#是如何使用这些东东吧。
1. 创建一个新Excel的Application:
Application exc = new Application();
if (exc == null) {
Console.WriteLine("ERROR: EXCEL couldn't be started");
return 0;
} 2. 让这个工程可见:
exc.set_Visible(0, true); 3. 获取WorkBooks集合:
Workbooks workbooks = exc.Workbooks; 4. 加入新的WorkBook:
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0); 5. 获取WorkSheets集合:
_Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
if (worksheet == null) {
Console.WriteLine ("ERROR in worksheet == null");
} 6. 给单元格设置变量:
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); 例程:
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using Excel; class Excel {
public static int Main() {
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;
}
} 现在我们来看看如何使用数组,他有些类似于设置单元格。仅仅需要的改变只是args2[0] = array2;
const int nCell = 5;
Range range2 = worksheet.get_Range("A1", "E1");
int[] array2 = new int [nCell];
for (int i=0; i < array2.GetLength(0); i++) {
array2[i] = i+1;
}
Object[] args2 = new Object[1];
args2[0] = array2;
range2.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range2, args2);
Excel.Application myExcel = new Excel.Application();
myExcel.Application.Workbooks.Add(true);
//让Excel文件可见0
myExcel.Visible=true;
//第一行为报表名称
myExcel.Cells[1,4]="普通报表";
//逐行写入数据,
for(int i=0;i<11;i++)
{
for(int j=0;j<7;j++)
{
//以单引号开头,表示该单元格为纯文本
myExcel.Cells[2+i,1+j]="'"+myData[i,j];
}
}
“myExcel.Cells[1,4]="普通报表";”这段代码肯定是不行的。
根据我的经验,要使用Excel要记住用Range.
就是说是对区域操作,例如:Range r = worksheet.get_Range("C6", Missing.Value);
r.Value2 = "Hello";
这样才能成功。
下面是我写的一段示例代码,你可以学习一下: public void SaveExcelFile()
{
excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
try
{
//_Workbook ji = (_Workbook)excel.ActiveWorkbook;
excel.Workbooks.Add(System.Reflection.Missing.Value);
Worksheet worksheet = (Worksheet) excel.ActiveSheet;
Range r = worksheet.get_Range("C6", Missing.Value);
r.Value2 = "Hello";
r.Interior.ColorIndex = 6;
Range r2 = worksheet.get_Range("A1","B2");
r2.Merge(0); worksheet.SaveAs(@"c:\ExcelExample.xls",
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value);
// Workbook book = (Workbook)excel.ActiveWorkbook;
// book.Close(1,Missing.Value,Missing.Value );
System.Windows.Forms.MessageBox.Show ("OK!");
//excel.Quit();
}
catch
{
Console.WriteLine("Excel file Create unseccess!");
excel.Quit();
}
}
Excel._Application exc=new Excel.ApplicationClass();
exc.Visible=true;
还是这个错误:
System.InvalidCastException: 接口 Excel._Application 的 QueryInterface 失败。
http://www.21disk.com/21disk/upload/tjq_tang/00%5BShareFile%5D/copySheet.rar
如果还是不行.就很有可能就是excel的问题.
如果你旁边有多的pc,你在它上面安装好excel测试一下.
hope it helps