国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#生成Word文檔代碼示例

2020-01-24 01:47:48
字體:
來源:轉載
供稿:網友
public bool CreateWordFile(string _filename, "數據List或者你C#要寫的數據")    {            #region 開始生成Word      try      {        string strtitle = "任務導出";        object oEndOfDoc = "http://endofdoc";        Object Nothing = System.Reflection.Missing.Value;        Object filename = _filename;        //創建Word文檔        Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();        Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);        //設置頁眉        WordApp.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;        WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;        WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("WPFOA任務導出");        WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//設置右對齊        WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;//跳出頁眉設置        //任務導出------名字        Word.Paragraph oPara1;        oPara1 = WordDoc.Content.Paragraphs.Add(ref Nothing);        oPara1.Range.Text = strtitle;        oPara1.Range.Font.Bold = 1;        oPara1.Range.Font.Name = "宋體";        oPara1.Range.Font.Size = 20;        oPara1.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;        oPara1.Format.SpaceAfter = 5;  //24 pt spacing after paragraph.        oPara1.Range.InsertParagraphAfter();        #region 循環每個表        foreach (var v in lst_task)        {          #region 循環每一個列,產生一行數據          //描述信息          Word.Paragraph oPara3;          object oRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;          oPara3 = WordDoc.Content.Paragraphs.Add(ref oRng);          oPara3.Range.Text = "";          oPara3.Range.Font.Bold = 0;          oPara3.Range.Font.Name = "宋體";          oPara3.Range.Font.Size = 9;          oPara3.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;          oPara3.Format.SpaceBefore = 1;          oPara3.Format.SpaceAfter = 1;          oPara3.Range.InsertParagraphAfter();          //插入表格               Word.Table newTable;          Word.Range wrdRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;          newTable = WordDoc.Tables.Add(wrdRng, 5, 4, ref Nothing, ref Nothing);          newTable.Columns[1].Width = 60;          newTable.Columns[2].Width = 145;          newTable.Columns[3].Width = 80;          newTable.Columns[4].Width = 145;          newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;          newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;          //填充表格內容          newTable.Cell(1, 1).Range.Text = "項目名稱";          newTable.Cell(1, 2).Range.Text = "1";          newTable.Cell(1, 3).Range.Text = "模塊名稱";          newTable.Cell(1, 4).Range.Text = "2";          newTable.Cell(2, 1).Range.Text = "負責人名";          newTable.Cell(2, 2).Range.Text = "3";          newTable.Cell(2, 3).Range.Text = "狀態";          newTable.Cell(2, 4).Range.Text = "4";          newTable.Cell(3, 1).Range.Text = "發布時間";          newTable.Cell(3, 2).Range.Text = "5";          newTable.Cell(3, 3).Range.Text = "要求完成時間";          newTable.Cell(3, 4).Range.Text = "6";          newTable.Cell(4, 1).Range.Text = "完成時間";          newTable.Cell(4, 2).Range.Text = "7";          newTable.Cell(4, 3).Range.Text = "分配人";          newTable.Cell(4, 4).Range.Text = "8";          newTable.Cell(5, 1).Range.Text = "任務描述";          newTable.Cell(5, 2).Merge(newTable.Cell(5, 4));          newTable.Cell(5, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;          newTable.Cell(5, 2).Range.Text = "12313213123213";          #endregion        }        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);        #endregion        //關閉WinWord進程        System.Diagnostics.Process[] MyProcess = System.Diagnostics.Process.GetProcessesByName("WINWORD");        MyProcess[0].Kill();        return true;      }      catch      {        System.Diagnostics.Process[] MyProcess = System.Diagnostics.Process.GetProcessesByName("WINWORD");        MyProcess[0].Kill();        return false;      }      #endregion    }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 全州县| 宁安市| 沂源县| 姚安县| 安吉县| 宜黄县| 黄大仙区| 永新县| 伊通| 怀仁县| 东明县| 阿勒泰市| 寿宁县| 鹤山市| 海南省| 邢台县| 长武县| 昌吉市| 蕲春县| 平原县| 寻甸| 曲阳县| 抚顺县| 西充县| 定州市| 科技| 马公市| 阿荣旗| 广西| 杭锦后旗| 巩留县| 视频| 古浪县| 贵南县| 镇沅| 中方县| 吴忠市| 襄汾县| 西华县| 永州市| 武陟县|