1. 程式人生 > >C#word模板新增多個表格,新增圖片程式案例

C#word模板新增多個表格,新增圖片程式案例

  public string CreateWordFile(string CheckedInfo)
        {
            string message = "";
            try
            {
                Object Nothing = System.Reflection.Missing.Value;
                Directory.CreateDirectory("F:/CNSI");  //建立檔案所在目錄
                string name = "CNSI_DATA.doc";
                object filename = "F://CNSI//" + name;  //檔案儲存路徑
                //建立Word文件
                Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                object missing = System.Reflection.Missing.Value;
                object templatefile = CheckedInfo;
                Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref templatefile, ref missing, ref missing, ref missing);
                WordDoc.Activate();


                //新增頁首
                WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
                WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[頁首內容]");
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//設定右對齊
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出頁首設定
                WordApp.Selection.ParagraphFormat.LineSpacing = 10f;//設定文件的行間距


                //移動焦點並換行
                object count = 5;
                object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//換一行;
                Microsoft.Office.Interop.Word.Table newTableTitle = WordDoc.Tables.Add(WordApp.Selection.Range, 2, 3, ref Nothing, ref Nothing);
                //設定表格樣式
                newTableTitle.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone;
                newTableTitle.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone;
                //newTableTitle.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
                //newTableTitle.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
                newTableTitle.Columns[1].Width = 150f;
                newTableTitle.Columns[2].Width = 130f;
                newTableTitle.Columns[3].Width = 150f;


                newTableTitle.Cell(1, 1).Select();
                newTableTitle.Cell(1, 3).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
                //object moveCount = 5;
                object moveCount = 1;
                object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
                WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
                //WordApp.Selection.Cells.Merge();
                //插入圖片
                string FileName = @"F:\leo\project\pic1.jpg";//圖片所在路徑
                object LinkToFile = false;
                object SaveWithDocument = true;
                object Anchor = WordDoc.Application.Selection.Range;
                WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                //WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 130f;//圖片寬度
                //WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 90f;//圖片高度


                newTableTitle.Cell(1, 3).Select();
                newTableTitle.Cell(1, 3).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
                object moveUnit1 = Microsoft.Office.Interop.Word.WdUnits.wdLine;
                object moveCount1 = 1;
                object moveExtend1 = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
                WordApp.Selection.MoveDown(ref moveUnit1, ref moveCount1, ref moveExtend1);
                //插入圖片
                string FileName1 = @"F:\leo\project\pic2.jpg";//圖片所在路徑
                object LinkToFile1 = false;
                object SaveWithDocument1 = true;
                object Anchor1 = WordDoc.Application.Selection.Range;
                WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName1, ref LinkToFile1, ref SaveWithDocument1, ref Anchor1);


                newTableTitle.Cell(1, 2).Range.Text = "RTM圓桌會議申請表";
                newTableTitle.Cell(1, 2).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                newTableTitle.Cell(1, 2).Range.Font.Size = 16;
                newTableTitle.Cell(1, 2).Range.Bold = 2;
                newTableTitle.Cell(1, 2).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom;


                object ncount = 2;
                WordApp.Selection.MoveDown(ref WdLine, ref ncount, ref Nothing);//移動焦點
                WordApp.Selection.TypeParagraph();//插入段落


                Microsoft.Office.Interop.Word.Table newTableHead = WordDoc.Tables.Add(WordApp.Selection.Range, 2, 4, ref Nothing, ref Nothing);
                //設定表格樣式
                newTableHead.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone;
                newTableHead.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone;
                //newTableHead.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
                //newTableHead.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;


                newTableHead.Cell(1, 1).Range.Text = "EventNo:";
                newTableHead.Cell(1, 1).Range.Bold = 2;//設定單元格中字型為粗體
                newTableHead.Cell(1, 1).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;


                newTableHead.Cell(1, 2).Range.Text = "RTM17000016";
                newTableHead.Cell(1, 2).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;


                newTableHead.Cell(2, 1).Range.Text = "  Venue:";
                newTableHead.Cell(2, 1).Range.Bold = 2;//設定單元格中字型為粗體
                newTableHead.Cell(2, 1).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;


                newTableHead.Cell(2, 2).Range.Text = "陝西省西安市西京醫院陝西省西安市西京醫院陝西省西安市西京醫院陝西省西安市西京醫院";
                newTableHead.Cell(2, 2).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;


                newTableHead.Cell(2, 3).Range.Text = "Meeting Date:";
                newTableHead.Cell(2, 3).Range.Bold = 2;//設定單元格中字型為粗體
                newTableHead.Cell(2, 3).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
                newTableHead.Cell(2, 4).Range.Text = "2017-05-26 06:09:09";
                newTableHead.Cell(2, 4).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;


                object tcount = 2;//移動行數
                WordApp.Selection.MoveDown(ref WdLine, ref tcount, ref Nothing);//移動焦點
                WordApp.Selection.TypeParagraph();//插入段落


                //獲取資料來源
                DataTable dt = GetDataInfo();--此處需要方法實現資料來源,具體列名參照下面表格處理;


                //文件中建立表格
                Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, dt.Rows.Count, 5, ref Nothing, ref Nothing);
                //設定表格樣式
                newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
                newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;


                //填充表格內容
                newTable.Cell(1, 1).Range.Text = "Name";
                newTable.Cell(1, 1).Range.Bold = 2;//設定單元格中字型為粗體
                WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中


                //填充表格內容
                newTable.Cell(1, 2).Range.Text = "Title";
                newTable.Cell(1, 2).Range.Bold = 2;//設定單元格中字型為粗體
                newTable.Cell(1, 2).VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
                newTable.Cell(1, 2).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;


                //填充表格內容
                newTable.Cell(1, 3).Range.Text = "Hospitals&Speciaty/Organ";
                newTable.Cell(1, 3).Range.Bold = 2;//設定單元格中字型為粗體
                newTable.Cell(1, 3).VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
                newTable.Cell(1, 3).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                //縱向合併單元格
                //newTable.Cell(3, 3).Select();//選中一行
                newTable.Cell(1, 4).Range.Text = "Signature";
                newTable.Cell(1, 4).Range.Bold = 2;//設定單元格中字型為粗體
                newTable.Cell(1, 4).VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
                newTable.Cell(1, 4).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;


                newTable.Cell(1, 5).Range.Text = "Remarks";
                newTable.Cell(1, 5).Range.Bold = 2;//設定單元格中字型為粗體
                newTable.Cell(1, 5).VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
                newTable.Cell(1, 5).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    newTable.Cell(2 + i, 1).Range.Text = dt.Rows[i]["Name"].ToString();
                    newTable.Cell(2 + i, 1).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                    newTable.Cell(2 + i, 1).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    newTable.Cell(2 + i, 1).Range.Font.Size = 10;
                    newTable.Cell(2 + i, 2).Range.Text = dt.Rows[i]["Title"].ToString();
                    newTable.Cell(2 + i, 2).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                    newTable.Cell(2 + i, 2).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    newTable.Cell(2 + i, 2).Range.Font.Size = 10;
                    newTable.Cell(2 + i, 3).Range.Text = dt.Rows[i]["Hospitals"].ToString(); 
                    newTable.Cell(2 + i, 3).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                    newTable.Cell(2 + i, 3).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    newTable.Cell(2 + i, 3).Range.Font.Size = 10;
                    newTable.Cell(2 + i, 4).Range.Text = dt.Rows[i]["Signature"].ToString();
                    newTable.Cell(2 + i, 4).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                    newTable.Cell(2 + i, 4).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    newTable.Cell(2 + i, 4).Range.Font.Size = 10;
                    newTable.Cell(2 + i, 5).Range.Text = dt.Rows[i]["Remarks"].ToString();
                    newTable.Cell(2 + i, 5).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                    newTable.Cell(2 + i, 5).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    newTable.Cell(2 + i, 5).Range.Font.Size = 10;
                }


                //newTable.Cell(2, 1).Range.Text = "丁肇中";
                //newTable.Cell(2, 1).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                //newTable.Cell(2, 1).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                //newTable.Cell(2, 1).Range.Font.Size = 10;
                //newTable.Cell(2, 2).Range.Text = "主治醫師";
                //newTable.Cell(2, 2).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                //newTable.Cell(2, 2).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                //newTable.Cell(2, 2).Range.Font.Size = 10;
                //newTable.Cell(2, 3).Range.Text = "陝西省西安市西京醫院";
                //newTable.Cell(2, 3).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                //newTable.Cell(2, 3).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                //newTable.Cell(2, 3).Range.Font.Size = 10;
                //newTable.Cell(2, 4).Range.Text = "丁肇中";
                //newTable.Cell(2, 4).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                //newTable.Cell(2, 4).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                //newTable.Cell(2, 4).Range.Font.Size = 10;
                //newTable.Cell(2, 5).Range.Text = "備註";
                //newTable.Cell(2, 5).Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                //newTable.Cell(2, 5).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                //newTable.Cell(2, 5).Range.Font.Size = 10;


                //在表格中增加行
                //WordDoc.Content.Tables[1].Rows.Add(ref Nothing);


                //檔案儲存
                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);
                //if(newTableTitle.Rows.Count > 1)
                //{
                //    newTableTitle.Rows[newTableTitle.Rows.Count ].Delete();
                //}
                WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                message = name + "文件生成成功,以儲存到C:CNSI下";
            }
            catch(Exception ex)
            {
                message = "檔案匯出異常!";
            }
            return message;

        }

     -------特殊講解:

               object tcount = 2;//移動行數
                WordApp.Selection.MoveDown(ref WdLine, ref tcount, ref Nothing);//移動焦點
                WordApp.Selection.TypeParagraph();//插入段落

擷取的以上三行程式碼,MoveDown開始我也認知不是很清楚,其實就是移動當前焦點到下個模組,移動之後,需要WordApp.Selection.TypeParagraph();插入段落,

就可以重新開始插入表格,圖片,《 object tcount = 2;//移動行數》重要引數,移動位置的引數,假設你上面已經錄入兩行,那麼,它的值就可以設定成2,下一個模組內容

就和上面無縫銜接,不然會產生內容之間有較大的空格,甚至整個佈局會混亂,尤為注意,互相學習,多多提意見;

相關推薦

C#word模板增多表格新增圖片程式案例

  public string CreateWordFile(string CheckedInfo)         {             string message = "";             try             {              

python 列表list和字典dict同時增多分別用extend、update方法:

>>> d = [2,3,4] >>> e = [1,2,3] >>> d.extend(e)  # 用列表用extend()方法 >>> d [2, 3, 4, 1, 2, 3]   >>&

通過javaMail傳送郵件可選增多收件人密送抄送附件,超實用

        自己通過學習多人的程式碼,並整理了一個簡單,呼叫方便的通過javaMail傳送郵件。只需填寫發件郵箱地址,密碼;收件人地址,附件,選擇是否儲存,設定自己傳送郵件時的暱稱就ok了。程式碼自動匹配設定smtp服務地址和埠。    傳送郵件需要郵箱地址和密碼,開啟P

OSG中增多物體 OSG::Group的使用

之前的文章可以針對單個物體,進行平移,旋轉,縮放。 現在我想在場景中放置兩頭牛。在OSG中,放置多個物體,可以將這個多個物體放置到一個Group中,然後再將場景設定成這個Group,就可以了。 #include<osgViewer/Viewer> #inc

C# DataGridView單元格中動態增多控制元件

簡介:          在DataGridView的單元格中動態新增多個控制元件。例如在DataGridViewTextBox單元格中,新增CheckBox和Button控制元件。主題思路就是一個動態控制元件的大小,位置,顯示,事件設定,和平常

封裝RecyclerView Adapter 實現可增多header和footer,可設定loadingView低耦合的多種佈局。

多種佈局的recyclerview的普通寫法是重寫RecyclerView.Adapter的getItemViewType返回不同position上的type,在onCreateViewHolder(ViewGroup parent,int viewType

增多UIWindow時使用keyWindow要注意一點

最近的專案裡有個關於建立多個uiwindow時使用keywindow出現的bug。有個任務是在開啟資訊展示時,要求不被任何東西遮擋。這裡設計方案是新增一個新的HXUIWindow繼承與UIWindow。測試時打開了HXUIWindow顯示在螢幕上,執行開啟 彈框 按鈕(ui

innosetup多語言選擇增多

其實很簡單: [Languages] Name: "chinesesimp"; MessagesFile: "compiler:Default.isl" Name: "english"; MessagesFile: "compiler:Languages\Englis

iOS開發tableView側滑刪除的實現或者增多側滑顯示按鈕

如果想要實現簡單的刪除操作,1 + 2 + 4方法就可以實現 ,如果你要滑動出現多個操作按鈕的話,就要實現方法5了,第五個方法說白了就是集合了前幾個方法,而且可以新增多個按鈕,如果用第5個方法,1、2、4方法可以不實現 //1 - (BOOL)tableView:(UITa

ios開發時在Xcode中增多targets進行版本控制管理多相同內容的專案

 在ios開發時,我們經常會遇到對同一個app開發多個版本(Pro、Lite、Free)的情況,這裡就涉及到xcode裡通過新增多個targets來進行版本控制的問題了,下面就簡單說明一下: 點選左側的工程名稱,右側會出現PROJECT和TARGETS,點選你現在的target,假如叫A,右鍵彈出選單中,選擇

C#之WinForm基礎 增多窗體

       慈心積善融學習,技術願為有情學。善心速造多好事,前人栽樹後乘涼。我今於此寫經驗,願見文者得啟發。 感恩曾經幫助過 心少樸 的人。 C#優秀,值得學習。Winform,WPF 都可以關

VS2015基礎 在同一解決方案下增多專案

       慈心積善融學習,技術願為有情學。善心速造多好事,前人栽樹後乘涼。我今於此寫經驗,願見文者得啟發。剛開始的時候,是這樣的。解決方案管理器找不到的話,可以通過下面的方式開啟儲存該解決方案的資料

java 中List包含List如何增多list,Map中包含多list如何新增

1、List中新增list public class TestList { public static void main(String[] args) { List<List<Integer>> vecvecRes = new ArrayLi

百度地圖 迴圈載入marker並增多資訊視窗解決只顯示最後一個視窗資訊的問題

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ShowNurseryInfo.aspx.cs"     Inherits="Bim5D_Web.Nursery.ShowNurseryInfo" %> <!DOCTY

Easyui 繫結左側選單右側顯示內容動態增多tab

老規矩還是先上圖看效果:左側選單,右側展示內容以及title 接著看前臺jsp程式碼:前臺程式碼很簡單,按照官方的要求寫就可以,重點是紅色框裡別忘了加最後一步就是js繫結事件在這裡我給出程式碼需要的同學直接copy,這裡是onclick()點選事件,資料為測試資料,替換自己真

oracle 一個表增多欄位根據條件重新整理欄位值

2. Oracleupdate TA a set(name, remark)=(select b.name, b.remark from TB b where b.id=a.id)   where exists(select 1 from TB b where b.id=a.

c# word 轉pdf 導出失敗因為此功能尚未安裝

完整版 class 如果 b- href 支持 text 還需要 lan savePDF應該是office2007以上的版本才支持的,而且必須是完整版的office。 如果2007提示這個錯誤,還需要安裝一個插件 http://download.microsof

centos批量增多IP地址

在/etc/sysconfig/network-scripts/中新建檔案ifcfg-eth0-range0   vi /etc/sysconfig/network-scripts/ifcfg-eth0-range0   輸入以下檔案內容格式: &nb

iOS Swift百度地圖 增多標註

搞了一天終於把新增多個標註做出來了,又深刻的反應了寫程式碼一味的copy,不知其所以然的後果太嚴重,一點錯誤找個大半天,好了,不過多bb,說說新增多個標註要注意的一些東西吧。 首先,新增大頭針的方法要實現在_mapview.delegate = self 之後,否則過早添加

 (轉)windows一臺電腦增多git賬號

  概述 電腦上已經配置了github的ssh連線。現在又有一個不同的git賬戶,也就是要在一臺電腦上配置兩個git賬號。 下面記錄一下我配置的方法。 取消git全域性配置 之前配置github的時候,用命令 git config --golbal u