从官网上下了LightSwitch_Office_Integration_Pack_Extension 安装包,在LS属性中将扩展包加入到工程。
先制做一个WORD模板 ,示意如下:注意书签和控件名
将文件复制到LS的客户端工程中,并设为内嵌的资源。
在LS客户端需要导出WORD的地方,加一个命令按钮,生成相应的事件处理:
partial void GenerateDocument_Execute()
{
// Write your code here.
string path= Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Assembly ass = Assembly.GetExecutingAssembly();
Stream s = ass.GetManifestResourceStream(this.GetType(), "MonthPlan.docx");
byte[] bt = new byte[s.Length];
s.Read(bt, 0, bt.Length);
s.Close();
s.Dispose();
var fileName = this.WorkPlanHeads.SelectedItem.PlanYearMonth + "工作计划";
FileStream fs = new FileStream(path + "\\"+fileName+".docx", FileMode.Create);
fs.Write(bt, 0, bt.Length);
fs.Close();
fs.Dispose();
List<OfficeIntegration.ColumnMapping> planHeadColumn = new List<ColumnMapping>() {
new ColumnMapping("PlanYear","PlanYear"),
new ColumnMapping("PlanMonth","PlanMonth"),
new ColumnMapping("FillDate","FillDate")
};
var doc = OfficeIntegration.Word.GenerateDocument(path + "\\"+ fileName+".docx", this.WorkPlanHeads.SelectedItem, planHeadColumn);
List<string> detailsHeader = new List<string>() {
"" ,"TypeName","PlanContent","WorkEmp","WorkDept","PlanEndDate","Remark"
}; //要导到WORD中的对应的数据源中的列名,第一个为空,对应WORD中序号一列,这是在WORD模板中使用了自动编号的原因。
OfficeIntegration.Word.ExportEntityCollection(doc, "PlanDetail", 2, false, this.WorkPlanHeads.SelectedItem.WorkPlanDetails, detailsHeader);
OfficeIntegration.Word.SaveAsPDF(doc, path + "\\" + fileName + ".pdf", true);
}
接下来,进行调试,可以看到LS窗口中选中的数据被导入到WORD中。但是OfficeIntegration好象没有将生成的WORD自动保存的功能。