说明:显示的excel是利用模板创建的
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//*************************************************
// 先要添加对Excel的引用。选择项目-〉添加引用-〉COM-〉添加Microsoft Excel 9.0。(不同的office讲会有不同版本的dll文件)。
//*************************************************
using Excel;
using System.Reflection; // For Missing.Value and BindingFlags
using System.Runtime.InteropServices; // For COMException
namespace ExcelTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//外部操作
private void button1_Click(object sender, EventArgs e)
{
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
path = path.Substring(0, path.LastIndexOf('\\'));
//创建Excel对象
Excel.Application ExcelApp = new Excel.Application();
//新建工作簿
//Excel.Workbook ExcelWb = ExcelApp.Workbooks.Add(true);
Excel.Workbook ExcelWb = ExcelApp.Workbooks.Add(path + "\\TestExcel");
//新建工作表
Excel.Worksheet ExcelWs = ExcelWb.ActiveSheet as Excel.Worksheet;
Excel.Range ExcelRange = ExcelWs.Cells;
//ExcelRange.Cells.Clear();
ExcelRange.Cells.set_Item(1, 6, "2010-03-03");
ExcelRange.Cells.set_Item(1, 21, "9:30");
ExcelRange.Cells.set_Item(1, 38, "会议室");
ExcelRange.Cells.set_Item(2, 7, "王三");
ExcelRange.Cells.set_Item(3, 7, "全体人员");
ExcelRange.Cells.set_Item(5, 1, "关于作息制度的新安排");
ExcelApp.Visible = true;
}
private void button2_Click(object sender, EventArgs e)
{
}
//内嵌操作
private void button3_Click(object sender, EventArgs e)
{
//-------------------------------------------------------------------------------
// 1、右击工具箱,选择自定义工具箱,添加COM组件,选择“Microsoft Web 浏览器”(对应文件是\winnt\system32\shdocvw.dll),确定。在工具箱中将会出现文本为Explorer的WebBroser控件图标。
// 2、在Form1中添加WebBrowser控件。(对象名却省是axWebBrowser1)
//----------------------------------------------------------------------
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
path = path.Substring(0, path.LastIndexOf('\\'));
Object refmissing = System.Reflection.Missing.Value;
axWebBrowser1.Navigate(path + "\\TestExcel.xlt", ref refmissing, ref refmissing, ref refmissing, ref refmissing);
}
private void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
{
Object refmissing = System.Reflection.Missing.Value;
axWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref refmissing, ref refmissing);
Object o = e.pDisp;
Object oDocument = o.GetType().InvokeMember("Document", BindingFlags.GetProperty, null, o, null);
Object oApplication = o.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, oDocument, null);
//Object oName = o.GetType().InvokeMember("Name",BindingFlags.GetProperty ,null,oApplication,null);
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
path = path.Substring(0, path.LastIndexOf('\\'));
//由于打开的是excel文件,所以这里的oApplication 其实就是Excel.Application
Excel.Application ExcelApp = (Excel.Application)oApplication;//这样就可以象上文中所述来操作Excel了
Workbooks workbooks = ExcelApp.Workbooks;
Excel.Workbook ExcelWb = workbooks.get_Item(1);
Sheets sheets = ExcelWb.Worksheets;
Worksheet ExcelWs = (Worksheet)sheets.get_Item(1);
Excel.Range ExcelRange = ExcelWs.Cells;
ExcelRange.Cells.set_Item(1, 6, "2010-03-03");
ExcelRange.Cells.set_Item(1, 21, "9:30");
ExcelRange.Cells.set_Item(1, 38, "会议室");
ExcelRange.Cells.set_Item(2, 7, "王三");
ExcelRange.Cells.set_Item(3, 7, "全体人员");
ExcelRange.Cells.set_Item(5, 1, "关于作息制度的新安排");
}
}
}