private void button1_Click(object sender, EventArgs e) { //this.pictureBox1.Image. = (System.Drawing.Image)global::WinTest.Properties.Resources.ResourceManager.GetObject("主界面副本.jpg"); this.pictureBox1.ImageLocation = @"../../resources/ZAE.gif"; pictureBox1.Refresh(); Word.Application app = new Word.Application(); //模板文件 string TemplateFile = @"H:/科目二场地项目考试成绩表.doc"; //生成的具有模板样式的新文件 string FileName = @"H:/" + DateTime.Now.ToString("姓名—考试成绩表") + ".doc"; //string FileName = @"H:/" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ".doc"; //string Fname = DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ".doc"; //FileName = Server.MapPath("~/UpLoad/TestWord/" + Fname ); //模板文件拷贝到新文件 File.Copy(TemplateFile, FileName); Word.Document doc = new Word.Document(); object Obj_FileName = FileName; object Visible = false; object; object missing = System.Reflection.Missing.Value; //打开文件 doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref Visible, ref missing, ref missing, ref missing, ref missing); doc.Activate(); //foreach (Word.Bookmark bm in doc.Bookmarks) //{ // if (bm.Name == "姓名") // { // bm.Select(); // //bm.Range.Font.Color = Color.Black; // //bm.Range.Font.Size = 12; // bm.Range.Text = "王小明";//ViewState["FK_ProdurcePlanID"].ToString(); // } // // else if (bm.Name == "class") // // { // // bm.Select(); // // bm.Range.Text = ViewState["_class"].ToString(); // // } //} Object Nothing = System.Reflection.Missing.Value; //文档中创建表格 Word.Table newTable = doc.Tables.Add(app.Selection.Range, 12, 3, ref Nothing, ref Nothing); //设置表格样式 newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap; newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; newTable.Columns[1].Width = 100f; newTable.Columns[2].Width = 220f; newTable.Columns[3].Width = 105f; //填充表格内容 newTable.Cell(1, 1).Range.Text = "产品详细信息表"; newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体 //合并单元格 newTable.Cell(1, 1).Merge(newTable.Cell(1, 3)); app.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中 app.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中 //填充表格内容 newTable.Cell(2, 1).Range.Text = "产品基本信息"; newTable.Cell(2, 1).Range.Font.Color = Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色 //合并单元格 newTable.Cell(2, 1).Merge(newTable.Cell(2, 3)); app.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; //填充表格内容 newTable.Cell(3, 1).Range.Text = "品牌名称:"; newTable.Cell(3, 2).Range.Text = "dfsdf"; //纵向合并单元格 newTable.Cell(3, 3).Select();//选中一行 object moveUnit = Word.WdUnits.wdLine; object moveCount = 5; object moveExtend = Word.WdMovementType.wdExtend; app.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend); app.Selection.Cells.Merge(); object IsSave = true; doc.Close(ref IsSave, ref missing, ref missing); //Response.Write("<mce:script language='javascript'><!-- alert('生成word成功!'); // --></mce:script>"); }