|
楼主 |
发表于 2012-11-8 11:25
|
显示全部楼层
本帖最后由 liucqa 于 2012-11-17 00:04 编辑
http://www.cnblogs.com/brooks-dotnet/category/233027.html
一、概述
VSTO学习笔记.rar
(582.51 KB, 下载次数: 26)
VSTO学习笔记(二)Excel对象模型.rar
(870.14 KB, 下载次数: 28)
VSTO学习笔记(三) 开发Office 2010 64位COM加载项.rar
(1.02 MB, 下载次数: 43)
VSTO学习笔记(五)批量编辑Excel 2010 x64.rar
(37.96 KB, 下载次数: 22)
VSTO学习笔记(七)基于WPF的Excel分析、转换小程序.rar
(510.84 KB, 下载次数: 21)
VSTO学习笔记(九)浅谈Excel内容比较.rar
(653.91 KB, 下载次数: 28)
VSTO 学习笔记(十一)开发Excel 2010 64位自定义公式.rar
(611.37 KB, 下载次数: 18)
VSTO 学习笔记(十三)谈谈VSTO项目的部署.part3.rar
(1.18 MB, 下载次数: 65)
VSTO 学习笔记(十三)谈谈VSTO项目的部署.part2.rar
(1.2 MB, 下载次数: 58)
VSTO 学习笔记(十三)谈谈VSTO项目的部署.part1.rar
(1.2 MB, 下载次数: 58)
*************************************************************************************************
http://www.cnblogs.com/scy251147/archive/2011/02/11/1951447.html
抄来的代码
- 1、加载文本文档到Excel中
- private void Sheet1_Startup(object sender, System.EventArgs e)
- {
- #region the first method
- Application.ScreenUpdating = false;
- Application.Workbooks.Open(@"C:\regist.log",System.Type.Missing,false,System.Type.Missing,System.Type.Missing,System.Type.Missing,System.Type.Missing,System.Type.Missing,",",true,System.Type.Missing,System.Type.Missing,true,System.Type.Missing,System.Type.Missing);
- Application.ScreenUpdating = true;
- #endregion
- }
- 2、加载网络文本到Excel中
- private void Sheet1_Startup(object sender, System.EventArgs e)
- {
- #region the second method
- this.Application.Workbooks.OpenText("http://www.baidu.com", Excel.XlPlatform.xlMacintosh, 3, Excel.XlTextParsingType.xlDelimited, Excel.XlTextQualifier.xlTextQualifierNone, missing, missing, missing, false, missing, missing, missing, missing, missing, missing, missing, missing, missing);
- #endregion
- }
- 3、加载DataSet数据到Excel中
- private void Sheet1_Startup(object sender, System.EventArgs e)
- {
- #region the third method:load the dataset into the worksheet
- DataSet ds = GetDataSet();
- int count = 1;
- foreach (DataRow dr in ds.Tables[0].Rows)
- {
- Excel.Range rng = this.Application.get_Range("A"+count++,missing);
- rng.Value2 = dr.ItemArray;
- }
- #endregion
- }
- 4、删除Excel的sheet
- private void Sheet1_Startup(object sender, System.EventArgs e)
- {
-
- #region the forth method:delete the worksheet
- Application.DisplayAlerts = false;
- ((Excel.Worksheet)this.Application.ActiveWorkbook.Sheets[2]).Delete();
- Application.DisplayAlerts = true;
- Application.Quit();
- #endregion
- }
- 5、操作Excel的单元格
- private void Sheet1_Startup(object sender, System.EventArgs e)
- {
- #region the fifth method:the excel named ranges
- Microsoft.Office.Tools.Excel.NamedRange namedRange1 = Controls.AddNamedRange(this.Range["A1","A10"],"namedRange1");
- Microsoft.Office.Tools.Excel.NamedRange namedRange2 = Controls.AddNamedRange(this.Range["A1",missing],"namedRange2");
- namedRange1.Merge(false);
- namedRange2.Merge(false);
- namedRange1.BorderAround(missing, Excel.XlBorderWeight.xlHairline, Excel.XlColorIndex.xlColorIndexAutomatic, missing);
- namedRange1.AutoFormat(Excel.XlRangeAutoFormat.xlRangeAutoFormat3DEffects1,true,false,true,false,true,true);
- #endregion
- }
- 6、单元格式限定
- private void Sheet1_Startup(object sender, System.EventArgs e)
- {
- #region this sixth method:cell manipulation
- Excel.Range rng = Globals.Sheet1.Range["B3", "B3"] as Excel.Range;
- rng.Value2 = "123456";
- rng.NumberFormat = "$#,###.0";
- rng = rng.Cells[3, 3] as Excel.Range;
- rng.Value2 = "new";
- #endregion
- }
- 7、单元格合并
- private void Sheet1_Startup(object sender, System.EventArgs e)
- {
- #region the seventh method:working with unions
- Excel.Range rngA = Globals.Sheet1.Range["a2", "B3"] as Excel.Range;
- rngA.Value2 = "Mortgage";
- Excel.Range rngB = Globals.Sheet1.Range["a5", "B6"] as Excel.Range;
- rngB.Value2 = "Interest";
- Excel.Range unionRange = Application.Union(rngA,rngB,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing) as Excel.Range;
- unionRange.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
- #endregion
- }
- 8、密码保护
- private void Sheet1_Startup(object sender, System.EventArgs e)
- {
- #region the eighth method:workbook password protection
- string password = this.Password;
- this.Protect(password, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
- this.Unprotect(password);
- #endregion
- }
- 9、单元格事件触发
- private void Sheet1_Startup(object sender, System.EventArgs e)
- {
- #region the ninth method:responding to events
- Microsoft.Office.Tools.Excel.NamedRange namedRange1 = Globals.Sheet1.Controls.AddNamedRange(Globals.Sheet1.Range["A1",missing],"NamedRange");
- namedRange1.Change+=new Microsoft.Office.Interop.Excel.DocEvents_ChangeEventHandler(namedRange1_Change);
- #endregion
- }
- private void namedRange1_Change(Microsoft.Office.Interop.Excel.Range target)
- {
- if (!this.Application.CheckSpelling((string)target.Value2, null, true))
- {
- MessageBox.Show("拼写有误!");
- }
- }
复制代码
参照的书籍是:Wrox.Professional.VSTO.2005.Visual.Studio.2005.Tools.for.Office.May.2006.pdf
http://archive.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=OfficeDevFuture&ReleaseId=4172
Office Dev Samples for VS 2010 RTM
MSDN上的VSTO开发例子
|
|