Generate a Word file with .NET

xiaoxiao2021-03-06  40

Using system; use system.io;

Namespace Ysj.publicClass {///

/// Word summary description. /// public class wordclass {

Word.Application WordApp = NULL; Word.Document WordDoc = NULL;

object Nothing = System.Reflection.Missing.Value; object format = Word.WdSaveFormat.wdFormatDocument; object objt = true; object unit = Word.WdUnits.wdStory; object pBreak = (int) Word.WdBreakType.wdPageBreak; public WordClass () {} ///

/// Create a Word object /// public void createWordApp () {Object Daltert = false; WordApp = new word.applicationclass (); Wordapp.displayAlerts = word.wdalertlevel.wdalertsnone }

///

/// Add a picture to a word file //// /// picture path public void addimgtoword (StrimGPath) {///// image if (System.IO.File.Exists (strImgPath)) {wordApp.Selection.EndKey (ref unit, ref Nothing); wordApp.Selection.InlineShapes.AddPicture (strImgPath, ref Nothing, ref objt, ref Nothing); wordApp. Selection.Range.Insertafter (" '/ n');}} /// /// Insert images in the current cursor position /// /// image path public void AddImgToWordLocation (string strImgPath) {// image if (System.IO.File.Exists (strImgPath)) {wordApp.Selection.Range.InlineShapes.AddPicture (strImgPath, ref Nothing, ref objt, Ref nothing);}}} /// /// Insert the specified content in a specific row and enter /// /// content /// font /// font size public void insertcaption (String Title, String StrfontName, Float Fontsize, Bool Center) { Word.Range rg = wordapp.selection.ran ge; Rg.InsertBefore ( '/ n' Title); Rg.Font.Name = strFontName; Rg.Font.Size = FontSize; if (Center) {Rg.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;}}

///

/// Insert Enter /// /// Enter a number of Enter public void insertenter (int how) {for (INT i = 0; i <= how; i ) {WordApp.seLection.Range.insertbefore (" '/ n');}} /// /// Open file with Word /// /// file path public void OpenFileToWord (string strFilePath) {if (System.IO.File.Exists (strFilePath)) {object srcFileName = @ strFilePath; object wdDelete = Word. WdDeleteCells.wdDeleteCellsEntireColumn; object wdCount = 1; wordDoc = wordApp.Documents.Open (ref srcFileName, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, Ref nothing); Wordapp.Serection.TypebackSpace (); Wordapp.Serection.TypeBackspace (); Wordapp.Serection.Range.insertBreak (Ref pbreak);}} /// /// Close Word object /// < / summary> public void closeword () {WordDoc.close (ref nothing, ref nothing, ref nothing); Wordapp.quit (Ref Nothin g, ref nothing, ref nothing;} /// /// save into a word file /// /// public void saveword (String strWordFilePath) {// save as to the htm file doc file object dstFileName = @ strWordFilePath; wordDoc.SaveAs (ref dstFileName, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, REF NOTHING, REF NOTHING;

}

转载请注明原文地址:https://www.9cbs.com/read-61450.html

New Post(0)