private void ExportWord() { WordHelper helper = new WordHelper(); String file = Server.MapPath("/resource/sciProject.dot"); helper.CreateNewWordDocument(file); DataClassesDataContext dc = new DataClassesDataContext(); String F_ID = Session[SessionMgm.SciProjectID].ToString(); ScienceProject project = dc.ScienceProject.SingleOrDefault(sp => sp.F_ID.Equals(F_ID)); if (project != null) { fillContent(helper, project); } project.F_name = UtilHelper.getValidatePath(project.F_name); String fileName = Server.MapPath("/resource/" + project.F_name + ".doc"); bool result = helper.SaveAs(fileName); helper.Close(); Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "Application/msword"; Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(project.F_name) + ".doc"); Response.TransmitFile(fileName); Response.Flush(); Response.Close(); Response.End(); }
public void TestReplaceBookMark() { var missing = Type.Missing; var wordHelper = new WordHelper(); var isOpen=wordHelper.OpenAndActive(@"D:\yibiyi\FileRoot\销售合同模板-安全.docx", false, false); if (isOpen) { wordHelper.ReplaceBookMark("ContractCode", "合同编号"); } wordHelper.Save(); wordHelper.Close(); }
private void ExportWord() { HttpResponse Response = context.Response; WordHelper helper = new WordHelper(); String file = context.Server.MapPath("/resource/sciProject.dot"); helper.CreateNewWordDocument(file); DataClassesDataContext dc = new DataClassesDataContext(); String F_ID = paras["id"].ToString(); ScienceProject project = dc.ScienceProject.SingleOrDefault(sp => sp.F_ID.Equals(F_ID)); if (project != null) { fillContent(helper, project); } String fileName = context.Server.MapPath("/resource/projectName.doc"); bool result = helper.SaveAs(fileName); helper.Close(); Response.Clear(); }
protected void Unnamed_Click(object sender, EventArgs e) { WordHelper helper = new WordHelper(); String file = Server.MapPath("/resource/socialProject.dot"); helper.CreateNewWordDocument(file); DataClassesDataContext dc = new DataClassesDataContext(); String F_ID = (sender as LinkButton).CommandArgument; SocialProject project = dc.SocialProject.SingleOrDefault(sp => sp.F_ID.Equals(F_ID)); fillContent(helper, project); fillParticipants(helper, project); fillAudit(helper, project); project.F_name = UtilHelper.getValidatePath(project.F_name); String fileName = Server.MapPath("/resource/" + project.F_name + ".doc"); bool result = helper.SaveAs(fileName); helper.Close(); Response.Clear(); Response.ContentType = "Application/msword"; Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.HtmlEncode(Path.GetFileName(fileName))); Response.TransmitFile(fileName); Response.End(); }
private void btnExport_Click(object sender, EventArgs e) { try { List <Entitys.CheckEntity> list = new List <Entitys.CheckEntity>(); for (int i = 0; i < this.dgv.Rows.Count; i++) { var row = this.dgv.Rows[i]; var entity = new Entitys.CheckEntity(); entity.BatchNo = (string)row.Cells["colBatchNo"].Value; entity.CheckQty = (int)row.Cells["colCheckQty"].Value; entity.ProductName = (string)row.Cells["colProductName"].Value; entity.Qty = (int)row.Cells["colQty"].Value; entity.Spec = (string)row.Cells["colSpec"].Value; entity.TestResult = (string)row.Cells["colTestResult"].Value; entity.TestTime = (string)row.Cells["colTestTime"].Value; entity.UserName = (string)row.Cells["colUserName"].Value; entity.PicPath = (string)row.Cells["colPicPath"].Value; entity.ExportPath = (string)row.Cells["colExportPath"].Value; list.Add(entity); } var database = DatabaseFactory.CreateDatabase(); DbCommand dbCommand = database.GetSqlStringCommand("select * from [T_PicSize]"); var picsizes = database.GetEntitys <Entitys.PicSizeEntity>(dbCommand); foreach (var item in list) { WordHelper helper = new WordHelper(); try { var files = Directory.GetFiles(item.PicPath, "*.jpg"); if (!piclens.Contains(files.Length)) { throw new Exception(string.Format("图片路径下jpg文件数必须为:{0}", piclenstring)); } var picsize = picsizes.First(t => t.FileCount == files.Length); helper.OpenAndActive(Path.GetFullPath(string.Format("WordTemplates\\{0}张.doc", files.Length)), false, false); helper.ReplaceBookMark("产品名称", item.ProductName); helper.ReplaceBookMark("规格型号", item.Spec); helper.ReplaceBookMark("产品批号", item.BatchNo); helper.ReplaceBookMark("生产数量", item.Qty.ToString()); helper.ReplaceBookMark("测量时间", item.TestTime); helper.ReplaceBookMark("抽检数量", item.CheckQty.ToString()); helper.ReplaceBookMark("测量结果", item.TestResult); helper.ReplaceBookMark("测量员", item.UserName); foreach (var file in files) { string filename = Path.GetFileNameWithoutExtension(file); var results = filename.Split(new char[] { '-' }); string bookmarkname = string.Format("图{0}", results[results.Length - 1]); bool isExist = helper.GoToBookMark(bookmarkname); if (isExist) { helper.InsertPic(file, picsize.Width, picsize.Height); //helper.InsertPic(file); //throw new Exception(string.Format("标签{0}不存在", bookmarkname)); } } helper.SaveAs(string.Format("{0}\\{1}", item.ExportPath, item.BatchNo + ".doc")); } catch (Exception) { throw; } finally { helper.Close(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }