1、using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using Excel=Microsoft.Office.Interop.Excel;using System.Reflection;namespace WindowsApplication5public partial class Form1 : Formpublic Form1(
2、)InitializeComponent();Excel.Application app = new Excel.Application(); private void Form1_Load(object sender, EventArgs e)string strFileName = “d:a.xls“;Object refmissing = System.Reflection.Missing.Value;this.webBrowser1.Navigate(strFileName,false);object axWebBrowser = this.webBrowser1.ActiveXIns
3、tance;private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)Object refmissing = System.Reflection.Missing.Value;object axWebBrowser =this.webBrowser1.ActiveXInstance;object oApplication = axWebBrowser.GetType().InvokeMember(“Document“,BindingFlags.GetProper
4、ty,null, axWebBrowser,null);Excel.Workbook wbb = (Excel.Workbook) oApplication;Excel.ApplicationClass excel = wbb.Application as Excel.ApplicationClass;Excel.Workbook wb = excel.Workbooks1;Excel.Worksheet ws = wb.Worksheets1as Excel.Worksheet;ws.Cells.Font.Name =“Verdana“;ws.Cells.Font.Size = 14;ws.
5、Cells.Font.Bold =true;Excel.Range range = ws.Cells;Excel.Range oCell = range10, 10 as Excel.Range;oCell.Value2 =“你好“;wb.Save();private void button1_Click(object sender, EventArgs e) Object refmissing = System.Reflection.Missing.Value;foreach (Excel.Workbook temp in app.Workbooks)MessageBox.Show(temp
6、.Name.ToString();temp.Save();temp.Saved = true;temp.Close(false, refmissing, refmissing);/没效果app.Workbooks.Close();/出错,异常来自 HRESULT:0x800A03ECExcel.Application excelApp = new Excel.Application();protected void Form1_Load(object sender, EventArgs e)webBrowser1.Navigate(“d:a.xls“, false);protected voi
7、d webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)object axWebBrowser = this.webBrowser1.ActiveXInstance;Excel.Workbook workbook = (Excel.Workbook) axWebBrowser.GetType().InvokeMember(“Document“,BindingFlags.GetProperty, null,axWebBrowser, null);Excel.Workbook sec
8、ondWorkbook = (Excel.ApplicationClass)workbook.Application).Workbooks1;Excel.Worksheet secondWorksheet = (Excel.Worksheet)secondWorkbook.Worksheets1;secondWorksheet.Cells.Font.Name = “Verdana“;secondWorksheet.Cells.Font.Size = 14;secondWorksheet.Cells.Font.Bold = true;(Excel.Range)secondWorksheet.Ce
9、lls10, 10).Value2 = “你好“;secondWorkbook.Save();protected void button1_Click(object sender, EventArgs e)Missing refmissing = System.Reflection.Missing.Value;foreach (Excel.Workbook temp in excelApp.Workbooks)MessageBox.Show(temp.Name.ToString();temp.Save();temp.Saved = true;temp.Close(false, refmissing, refmissing);excelApp.Workbooks.Close();/没报错