VSTO学习.docx

上传人:b****8 文档编号:9368236 上传时间:2023-02-04 格式:DOCX 页数:53 大小:34.37KB
下载 相关 举报
VSTO学习.docx_第1页
第1页 / 共53页
VSTO学习.docx_第2页
第2页 / 共53页
VSTO学习.docx_第3页
第3页 / 共53页
VSTO学习.docx_第4页
第4页 / 共53页
VSTO学习.docx_第5页
第5页 / 共53页
点击查看更多>>
下载资源
资源描述

VSTO学习.docx

《VSTO学习.docx》由会员分享,可在线阅读,更多相关《VSTO学习.docx(53页珍藏版)》请在冰豆网上搜索。

VSTO学习.docx

VSTO学习

1VSTO可以通过项目创建office的智能文档。

2创建excle,有三中,工作簿,模板,外接程序。

我们发现使用模板可以调用form的控件,而工作簿不能。

我们需要对单元格进行操作是,需要将namedRange控件引入到sheet中。

然后就可以对其读写。

使用代码

this.namedRange1.Value2=this.dateTimePicker1.Text;

取得值。

3绑定数据

我们可以使用系统的数据源控制面板引入数据表,将数据源面板中的表字段拖入sheet中,既可以实现数据绑定,但是这是,只能显示一条数据,需要实现上下翻页,可以使用BindingSource的翻页功能。

代码如下:

this.t_UserBindingSource.MoveNext();

4存储过程绑定

在数据源中,我们也可以引用存储过程。

我们只需要将该存储过程拖入sheet,系统会自动将输出参数生成文本框,返回结果生成表格,然后还会自动生成一个执行按钮。

5创建操作面板

我们可以在excl工作簿右面创建一个操作面板来实现一些业务功能。

X

(1)在工作簿工程中创建新建项“操作窗格控件”ActionsPaneControl活动面板,然后我们可以在其中拖入一些控件来实现一些功能,例如拖入一个textBox,然后点击按钮后,将textBox1中的值写入到excle中的namedRang。

privatevoidbutton1_Click(objectsender,EventArgse)

{

Globals.Sheet1.namedRange1.Value=textBox1.Text;

}

(2)在工作簿中实例化该活动面板,并绑定到工作簿中

privatevoidSheet1_Startup(objectsender,System.EventArgse)

{

ActionsPaneControl1apc=newActionsPaneControl1();

Globals.ThisWorkbook.ActionsPane.Controls.Add(apc);

}

 

6给单元格绑定数据

通过VSTO可以在excle中改变单元格的名称,然后利用该名称对该单元格赋值。

例如:

我们将A1单元格改为aaa,然后再界面拖入一个textBox1。

将他们绑定在一起

this.aaa.DataBindings.Add("Value",textBox1,"text");

注意:

只有改过名字的单元格可以赋值,像A2这样的单元格就不能赋值的。

7用list绑定VSTO

默认情况下VSTO只能使用dataset来绑定数据库,但是我们三层架构返回的都是list。

怎么办呢?

我们可以变通的来解决这个问题。

VSTO是可以绑定BindingSource。

而BindingSource又可以绑定list。

通过这样的转接就实现VSTO绑定list了。

为了方便可以:

1在数据源面板中添加对象数据源,将model引入

2从数据源中将model拖入excel中。

Model是不能自动得到数据的。

但是拖入后系统会自动帮我们创建一个BindingSource。

而且这个BindingSource还与界面中的每一个字段都绑定好了。

3我们在后台代码中加入

base_UserMangeInfoBindingSource.DataSource

=MES.BASE.BLL.Base_UserMangeBLL.SelectAll();

通过bll将数据查询出来绑定到BindingSource。

但是:

该方法只能单向绑定,也就是从BindingSource绑定到界面控件。

无法从界面控件将修改后的数据传给BindingSource。

 

8数据缓存

Vsto可以进行数据缓存,当数据库离线后,在本地也可以打开缓存的数据。

但是我认为这个并不好用。

我们需要的是如何利用数据缓存写入数据库。

上面提到的方法都是从数据库中读出数据,如果要将数据写回数据库,还得自己想办法。

最直接的办法就是读取界面的数据控件。

然后调用bll写回。

9保存word

如果需要保存整个word文档包括他们的格式,可能VSTO就无能为力啦。

VSTO只能向数据库中写入数据,不能把格式同步保存。

要设置格式只能在word智能文档namedRange控件上编辑这一段落用上面的格式。

然后把这些namedRange段落写入数据显示出来。

问题这样编写word就不方便了。

很多时候我们保存word并不一定有固定的格式。

因此要保存word可能还是得走保存整个word文件的办法。

10命令行打开VSTO文档传入参数

很多时候,我们希望在打开VSTO文件的时候,传入参数(例如表ID),我们好在VSTO中根据参数查询绑定数据。

VSTO文档没有构造函数。

我们可用通过命令行传参数的办法变相实现这个方法。

1首先在使用Environment.GetCommandLineArgs()方法获取命令行中的参数。

privatevoidThisDocument_Startup(objectsender,System.EventArgse)

{

intid=0;

stringid1="";

foreach(StringiteminEnvironment.GetCommandLineArgs())

{

if(item.Contains("id:

"))//如果包含"id"

{

MessageBox.Show(item);

inta1=item.IndexOf(":

")+1;//起始点

inta2=item.IndexOf(";")-a1;//长度

//截取字符串中ID的值

id1=item.Substring(a1,a2);

id=System.Convert.ToInt32(id1);

}

}

//绑定到前台对象

bookmark1.Text=id.ToString();

}

2在调用VSTO的窗体,使用Process.Start()方法,通过命令行的方式调用

stringWordPath=@"d:

\MyDocuments\VisualStudio2008\Projects\WindowsFormsApplication3\WordTemplate1\bin\Debug\WordTemplate1.dotx";

stringid="1234567890";

Process.Start("winword","\""+WordPath+"\"/id:

"+id+";");

 

11excl导出导入数据和图表

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Web.UI.WebControls;

usingExcel;

usingSystem.Reflection;

usingSystem.Web;

namespaceBLL

{

publicclassExclOut

{

stringstrUrl;//保存路径

GridViewdgv;

publicintDateCount;//导出条数

intDataGridViewColumncount;//取得网格控件列数

publicintDataGridViewRowCount;//取得网格控件行数

publicObject[,]MyData;//用于接收数据的对象

intCount,i,j;

publicExclOut(GridViewGridView,stringurl)

{

strUrl=url;

dgv=GridView;//构造方法取得要导出的网格控件

DateCount=dgv.Rows.Count;

}

///

///导出excl和图表

///参数:

type输出类型strRange1,strRange2图表输出范围,str图表内部名称数组,workSheetName基础表标签页名

///

///

publicstringexutExcel(inttype,stringstrRange1,stringstrRange2,string[]str,stringworkSheetName)

{

//HttpContext.Current.Response.Write("1");

DataGridViewColumncount=dgv.Columns.Count;//取得网格控件列数

DataGridViewRowCount=dgv.Rows.Count+1;//取得网格控件行数

MyData=newObject[DataGridViewRowCount+1,DataGridViewColumncount];//用于接收数据的对象

//HttpContext.Current.Response.Write(":

2");

ApplicationClassMyExcel;//excel对象

WorkbooksMyWorkBooks;//工作簿对象集合

WorkbookMyWorkBook;//工作簿

WorksheetMyWorkSheet;//标签页

ChartMyChart;//图表对象

RangeMyRange,MyRange1;//范围对象

try

{

MyExcel=newApplicationClass();//实例化excle对象

//MyExcel.Visible=true;//在界面打开excle窗口

if(MyExcel==null)

{

//MessageBox.Show("Excel程序无法启动!

","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);

returnnull;

}

MyWorkBooks=MyExcel.Workbooks;

MyWorkBook=MyWorkBooks.Add(Missing.Value);

MyWorkSheet=(Worksheet)MyWorkBook.Worksheets[1];

MyWorkSheet.Name=workSheetName;//设置标签页名称

MyRange=MyWorkSheet.get_Range("A1","F1");

//取得标题

Count=0;

for(i=0;i

{

MyData[0,i]=dgv.HeaderRow.Cells[i].Text;

Count=Count+1;

}

//设标题为黑体字

MyWorkSheet.get_Range(MyWorkSheet.Cells[1,1],MyWorkSheet.Cells[1,Count]).Font.Name="黑体";

//标题字体加粗

MyWorkSheet.get_Range(MyWorkSheet.Cells[1,1],MyWorkSheet.Cells[1,Count]).Font.Bold=true;

//设表格边框样式

MyWorkSheet.get_Range(MyWorkSheet.Cells[1,1],MyWorkSheet.Cells[1,Count]).Borders.LineStyle=1;

//----------------升序排列--------------

j=1;

//从网格控件中遍历取得数据

foreach(GridViewRowvarindgv.Rows)

{

inti1=0;

for(i=0;i

{

stringstr1=var.Cells[i].Text;

if(str1!

=""&&var.Cells[i].Visible==true)

{

MyData[j,i1++]=str1;

}

}

j++;

}

MyRange=MyRange.get_Resize(DataGridViewRowCount+1,DataGridViewColumncount);//定义数据写入区域

//删除多余标签页

foreach(Excel.WorksheetwsinMyWorkBook.Worksheets)

if(ws!

=MyExcel.ActiveSheet)

{

ws.Delete();

}

foreach(Excel.ChartchtinMyWorkBook.Charts)

cht.Delete();

//生成图表

switch(type)

{

case1:

//导出多个图表月表

orderbydesc();

exportChatr(MyWorkBook,MyWorkSheet,DataGridViewRowCount,outMyChart,outMyRange1);

break;

case2:

//导出柱线图周表

orderbydesc();//降序

//-------ProductionPlanAdherance-------

string[]str1={"ProductionPlanAdherance","MONTH","total","柱轴标题","柱轴标签","ProductionPlanAdherance"};

stringstrR1="D1:

D"+DataGridViewRowCount+",AB1:

AC"+DataGridViewRowCount;

chart1(MyWorkBook,MyWorkSheet,outMyChart,outMyRange1,strR1,null,str1[5],str1);

//------SafetyStock-------

string[]str2={"SafetyStock","MONTH","total","柱轴标题","柱轴标签","SafetyStock"};

stringstrR2="D1:

D"+DataGridViewRowCount+",X1:

Y"+DataGridViewRowCount;

chart1(MyWorkBook,MyWorkSheet,outMyChart,outMyRange1,strR2,null,str2[5],str2);

break;

case3:

//导出雷达图

//降序排列

//orderbydesc();

string[]str3={"radar","MONTH","total","柱轴标题","柱轴标签","radar"};

stringstrR3="A1:

C"+(DataGridViewRowCount);

chartRadar(MyWorkBook,MyWorkSheet,outMyChart,outMyRange1,strR3,null,str3[5],str3);

break;

}

//HttpContext.Current.Response.Write(":

5");

//----------将数据和图表输出到excel----------------

MyRange.Value2=MyData;//将接收了数据的对象数组传入range中,同时将数据写入excel中

MyRange.EntireColumn.AutoFit();//列宽自动

 

//MyWorkBook.SaveAs("c:

\\exle.xls",Excel.XlFileFormat.xlWorkbookNormal,

//"","",false,false,0,

//"",0,"","","");

System.Windows.Forms.Application.DoEvents();

MyWorkBook.Saved=true;

//保存excl

MyWorkBook.SaveCopyAs(strUrl);

returnnull;

}

catch(ExceptionMyEx)

{

//MessageBox.Show(MyEx.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);

returnMyEx.ToString();

}

}

//------------降序排列---------------

privatevoidorderbydesc()

{

intk=1;

for(j=DataGridViewRowCount-2;j>=0;j--)

{

for(i=0;i

{

stringstr1=dgv.Rows[j].Cells[i].Text;

if(str1!

=""&&dgv.Rows[j].Cells[i].Visible==true)

{

MyData[k,i]=str1;

}

}

k++;

}

}

//同时导出多个图表(月表)

privatestaticvoidexportChatr(WorkbookMyWorkBook,WorksheetMyWorkSheet,intDataGridViewcount,outChartMyChart,outRangeMyRange1)

{

//--------DPPM--------

string[]str2={"DPPM","MONTH","VALUE","柱轴标题","柱轴标签","DPPM"};

stringstrR2="C1:

C"+DataGridViewcount+",L1:

N"+DataGridViewcount;

chart2(MyWorkBook,MyWorkSheet,strR2,null,str2[5],str2);

//-------OTIF-------

string[]str1={"OTIF","MONTH","percent","柱轴标题","柱轴标签","OTIF"};

stringstrR1="C1:

C"+DataGridViewcount+",F1:

H"+DataGridViewcount;

chart2(MyWorkBook,MyWorkSheet,strR1,null,str1[5],str1);

//---------ProblemResolution-----

string[]str3={"ProblemResolution","MONTH","VALUE","柱轴标题","柱轴标签","ProblemResolution"};

stringstrR3="C1:

C"+DataGridViewcount+",O1:

Q"+DataGridViewcount;

chartColumLine(MyWorkBook,MyWorkSheet,outMyChart,outMyRange1,strR3,null,str3[5],str3);

//-------AuditPerformance-------

string[]str4={"AuditPerformance","MONTH","VALUE","柱轴标题","柱轴标签","AuditPerformance"};

stringstrR4="C1:

C"+DataGridViewcount+",AI1:

AI"+DataGridViewcount;

chartDf(MyWorkBook,MyWorkSheet,outMyChart,outMyRange1,strR4,null,str4[5],str4);

//-------AuditPerformance-------

string[]str5={"LoadCapacity","MONTH","total","柱轴标题","柱轴标签","LoadCapacity"};

stringstrR5="C1:

C"+DataGridViewcount+",U1:

U"+DataGridViewcount;

chartDf(MyWorkBook,MyWorkSheet,outMyChart,outMyRange1,strR5,null,str5[5],str5);

//-------FAIRRightFirstTime-------

string[]str6={"FAIRRightFirstTime","MONTH","value","柱轴标题","柱轴标签","FAIRRightFirstTime"};

stringstrR6="C1:

C"+DataGridViewcount+",AD1:

AE"+DataGridViewcount;

chartColum2All(MyWorkBook,MyWorkSheet,outMyChart,outMyRange1,strR6,null,str6[5],str6);

//-------ProductionPlanAdherance-------

string[]str7={"ProductionPlanAdherance","MONTH","total","柱轴标题","柱轴标签","ProductionPlanAdherance"};

stringstrR7="C1:

C"+DataGridViewcount+",AA1:

AB"+DataGridViewcount;

chart1(MyWorkBook,MyWorkSheet,outMyChart,outMyRange1,strR7,null,str

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1