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);
}
Word文档的读取与分析
原创
©著作权归作者所有:来自51CTO博客作者biyusr216的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:winform调用outlook
下一篇:C#与Excel的交互示例
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
python读取word中的公式 python读取word文档
Hello,上个周末没能搞事情,被一个代码需求给绊住了:朋友在平时工作中会经常重复性地打开不同PDF文件,选取其中特定的几组信息复制粘贴到不同的Word文档中,完成一份PDF文件平均耗时15分钟,想试试Python代码能否帮忙。 由于其涉及文件隐私,将需求简化如下:我这提供一份PDF版《笨办法学Python》,想把其中第五页的第1段和第4段填充到Word文档 “笔记.docx” 特定位置
python读取word中的公式 java读取word文档内容 js读取word文档内容 mybatisplus批量插入原理 python docx转pdf