文档库 最新最全的文档下载
当前位置:文档库 › c#生成word

c#生成word

bool CreateWordFile_all(string apath)//参数是文件路径
{
bool message = false;
string mheaderstring = "";//页眉内容
try
{
object filename = ""; //文件保存路径
Object Nothing = System.Reflection.Missing.Value;
//创建Word文档
Microsoft.Office.Interop.Word.ApplicationClass WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

WordDoc.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; //设置对齐方式
https://www.wendangku.net/doc/e52369958.html, = "宋体";
WordApp.Selection.Font.Size = 9f;
WordApp.ActiveDocument.PageSetup.HeaderDistance = 42.525f;//28.35 = 1厘米
object wdFieldPage = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage;
object wdFieldPages = Microsoft.Office.Interop.Word.WdFieldType.wdFieldNumPages;//wdFielddNumPages
Object preserverFormatting = true;
WordApp.Selection.TypeText(mheaderstring);
WordApp.Selection.Fields.Add(WordApp.Selection.Range, ref wdFieldPage, ref Nothing, ref preserverFormatting);
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置

WordApp.Selection.ParagraphFormat.LineSpacing = 13f;//设置文档的行间距
WordApp.Selection.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpaceExactly;//设置文档的行间距
WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//设置文档的行间距

//设置页边距
WordApp.ActiveWindow.ActivePane.Selection.Range.PageSetup.LeftMargin = 3.17f * 28.35f;
WordApp.ActiveWindow.ActivePane.Selection.Range.PageSetup.RightMargin = 3.17f * 28.35f;
WordApp.ActiveWindow.ActivePane.Selection.Range.PageSetup.TopMargin = 0.77f * 28.35f;
WordApp.ActiveWindow.ActivePane.Selection.Range.PageSetup.BottomMargin = 1.28f * 28.35f;
WordApp.Selection.ParagraphFormat.LineSpacing = 13f;//设置文档的行间距
https://www.wendangku.net/doc/e52369958.html, = "宋体";
https://www.wendangku.net/doc/e52369958.html,st.Range.Font.Size = 10;
https://www.wendangku.net/doc/e52369958.html,st.Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack;//设置字体颜色

//调用写入
toword_all(apath, WordDoc);//写入文件方法

https://www.wendangku.net/doc/e52369958.html,st.Alignment = Mi

crosoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
//文件保存
WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
message = true;
}
catch
{
MessageBox.Show("生成word文件异常,请联系***");
}
return message;
}
void toword_all(string apath, Microsoft.Office.Interop.Word.Document WordDoc)//把这个文件夹下的文件写入到word中
{
string[] files = Directory.GetFiles(apath);
string[] dirs = Directory.GetDirectories(apath);
foreach (string item in files)
{
//createword_all(item, WordDoc);//
}
//处理子文件夹
foreach (string item in dirs)
{
//toword_all(item, WordDoc);
}
}

相关文档
相关文档 最新文档