private void button1_Click(object sender, System.EventArgs e)
 {
 System.Data.DataTable dt = new System.Data.DataTable();
 //获取选定的ID号
 string personId =listBox1.Text;
 //字符型转化为整型
 int x = Int32.Parse(personId);
 //连接数据库获取结果集
 PersonInfo pi = new PersonInfo();
 dt = pi.getPersonInfo(x);//浏览文件夹保存文件
 string savePath = "";
 FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) 
 {
 savePath = folderBrowserDialog1.SelectedPath;
 }object filename = savePath + "/" + "项目任务书";
object missing = System.Reflection.Missing.Value;
 /* \endofdoc is a predefined bookmark */
 object endOfDoc = "\\endofdoc"; //Word应用程序对象
 Word._Application wordApp;
 //Word文档对象
 Word._Document wordDoc;
 wordApp = new Word.Application();
 //wordApp.Caption = "My New Caption";
 wordApp.Visible = true;
 wordDoc = wordApp.Documents.Add(ref missing, ref missing,ref missing, ref missing);//使用默认打印机打印文档
 /*
 wordDoc.PrintOut( ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,
 ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,
 ref missing,ref missing,ref missing,ref missing,ref missing);
 */            //添加页眉
 if(wordApp.ActiveWindow.ActivePane.View.Type == Word.WdViewType.wdNormalView || wordApp.ActiveWindow.ActivePane.View.Type == Word.WdViewType.wdOutlineView)
 {
 wordApp.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintView;
 }
 wordApp.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;
 //wordApp.ActiveWindow.ActivePane.Selection.InsertAfter("Header");string sHeader = "XXXXXX股份有限公司";
 wordApp.Selection.HeaderFooter.LinkToPrevious = false;
 wordApp.Selection.HeaderFooter.Range.Text = sHeader;
 wordApp.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekMainDocument;//Insert a paragraph at the beginning of the document.
 Word.Paragraph para1;
 para1 = wordDoc.Content.Paragraphs.Add(ref missing);
 para1.Range.Font.Bold = 1;//字体加粗
 //para1.Range.Text = "                                职工调查表";//title
 para1.Range.Font.Size = 12;
 para1.Range.Font.Name = "Arial";
             para1.Range.Text = "                            项目任务书";//title
 //para1.Format.SpaceAfter = 7.3f;    //7.3 pt spacing after paragraph.
 para1.Range.InsertParagraphAfter();//Insert another paragraph.
 Word.Paragraph para2;
 //利用了bookmark确定text的插入位置
 object oRng = wordDoc.Bookmarks.Item(ref endOfDoc).Range;//Insert a picture
 wordDoc.InlineShapes.AddPicture("C:\\logo_color.gif",ref missing,ref missing,ref oRng);para2 = wordDoc.Content.Paragraphs.Add(ref oRng);
 para2.Range.Text = "Id号: "+dt.Rows[0]["Id"].ToString()+"                         总消费: "+dt.Rows[0]["all_money"].ToString()+"            ";
 para2.Range.Font.Bold = 0;
 //para2.Format.SpaceAfter = 24;
 para2.Range.InsertParagraphAfter();
 //Insert a 3 x 5 table, fill it with data, and make the first row
 Word.Table table;
 Word.Range wrdRng = wordDoc.Bookmarks.Item(ref endOfDoc).Range;
 table = wordDoc.Tables.Add(wrdRng, 8, 6, ref missing, ref missing);
 table.Range.ParagraphFormat.SpaceAfter = 4;
 /*
 int r, c;
 string strText;
 //生成8行6列的表
 for(r = 1; r <= 8; r++)
 {
 for(c = 1; c <= 6; c++)
 {
 strText = "r" + r + "c" + c;
 //给表的每个格赋值
 table.Cell(r, c).Range.Text = strText;
 }
 }
 */
 //wordDoc.Paragraphs.First.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;table.Cell(1,1).Range.Text="性别";
 table.Cell(1,2).Range.Text=dt.Rows[0]["sex"].ToString();
 table.Cell(1,3).Range.Text="年龄";
 table.Cell(1,4).Range.Text=dt.Rows[0]["age"].ToString();
 table.Cell(1,5).Range.Text= "婚否";
 table.Cell(1,6).Range.Text=dt.Rows[0]["marriage"].ToString();
 table.Cell(2,1).Range.Text= "出生地";
 table.Cell(2,2).Range.Text=dt.Rows[0]["brith_province"].ToString()+" 省 "+dt.Rows[0]["brith_city"].ToString()+" 市(县)";
             table.Cell(1,1).Range.Font.Bold = 1;//设置粗体
 table.Cell(1,1).Range.Font.Color = Word.WdColor.wdColorBlue;//设置表头字体颜色
 table.Cell(2,1).Shading.Texture = Word.WdTextureIndex.wdTexture25Percent;//设置阴影//Add some text after the table.
 Word.Paragraph para3;
 oRng = wordDoc.Bookmarks.Item(ref endOfDoc).Range;
 para3 = wordDoc.Content.Paragraphs.Add(ref oRng);
 para3.Range.InsertParagraphBefore();
 para3.Range.Text = "注:\n     1.此表由项目管理部门填写,主管开发的副总经理审批,发给开发部门的项目组。\n     2.功能描述可参考有关需求文档。\n     3.此页不足记录时,可以有附页,附页格式自定,总页数包括所有附页。";
 para3.Format.SpaceAfter = 24;
 para3.Range.InsertParagraphAfter();
             
 //限制word文档不能复制、粘贴功能,只能被查看
 if(wordApp.ActiveDocument.ProtectionType==Word.WdProtectionType.wdNoProtection)
 {
 wordApp.ActiveDocument.Protect(Word.WdProtectionType.wdAllowOnlyComments ,ref missing ,ref missing);
 }/*关闭文档而不保存*/
 //object SaveChanges  = Word.WdSaveOptions.wdDoNotSaveChanges;
 //wordDoc.Close( ref SaveChanges, ref missing, ref missing);            /*设置表第一行的属性*/
 //table.Rows.First.Range.Font.Italic = 1;
 //table.Rows.First.Range.Font.Color = Word.WdColor.wdColorBlue;//设置表头字体颜色
 //table.Rows.First.Range.Font.Bold = 1;//设置粗体
 //table.Rows.First.Shading.Texture = Word.WdTextureIndex.wdTexture25Percent;//设置阴影//保存文档
 wordDoc.SaveAs(ref filename,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing);
 //关闭文档
 wordDoc.Close(ref missing, ref missing, ref missing);
 //推出文档
 wordApp.Quit(ref missing, ref missing, ref missing);
 }