//=====================write data with DataTable=======================================
Excel.Application xlApp=new Excel.Application();
Excel.Workbook xlBook;
Excel.Worksheet xlSheet;
int rowIndex=1;
int colIndex=0;
object missing = Missing.Value;
// object fileName = "normal2.xls"; // template file name
// object newTemplate = false;
// Create a new Document, by calling the Add function in the Documents collection
xlBook= xlApp.Workbooks .Add (true);//.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);
xlSheet=(Excel.Worksheet )xlApp.Worksheets.Add (missing,missing,missing,missing);
_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;
}