C# 之 word 操作類

來源:互聯網
上載者:User

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Word;

namespace WordControl
{
    public class ToWord
    {
        private Application _app;
        public Application app
        {
            get { return _app; }
            set { _app = value; }
        }

        private _Document _doc;
        public _Document doc
        {
            get { return _doc; }
            set { _doc = value; }
        }

        private Table _tbl;
        public Table tbl
        {
            get { return _tbl; }
            set { _tbl = value; }
        }

        private Table _tblFoot;
        public Table tblFoot
        {
            get { return _tblFoot; }
            set { _tblFoot = value; }
        }

        public ToWord() //建構函式
        {
            Create();
        }

        public void Create()
        {
            _app = new Application();
            //_app.Visible = true;

            object oMissing = System.Reflection.Missing.Value;
            _doc = _app.Documents.Add(ref oMissing, ref oMissing,
                ref oMissing, ref oMissing);
        }

        public bool SaveAs(object FileName)//文檔另存新檔
        {
            try
            {
                object oMissing = System.Reflection.Missing.Value;
                _doc.SaveAs(ref FileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing);
                return true;

            }
            catch (Exception ex)
            {
                return false;

            }
        }

        public void Close()//關閉一個Microsoft.Office.Interop.Word對象,銷毀對象
        {
            object missing = System.Reflection.Missing.Value;
            object oFalse = false;
            //釋放document對象
            if (_doc != null)
            {
                _doc.Close(ref oFalse, ref missing, ref missing);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(_doc);
            }

            //釋放Word.Application
            if (_app != null)
            {
                _app.Quit(ref oFalse, ref missing, ref missing);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(_app);
            }

            _doc = null;
            _app = null;

            //記憶體回收,如果不使用這句會,再次載入控制項時,會出現異常
            GC.Collect();
        }

        public void SetPage(int leftMargin, int rightMargin, int pgWidth, int pgHeight) //設定頁面邊界
        {
            _doc.PageSetup.LeftMargin = leftMargin;
            _doc.PageSetup.RightMargin = rightMargin;
            _doc.PageSetup.PageWidth = pgWidth;
            _doc.PageSetup.PageHeight = pgHeight;
        }

        public void AddSimpleHeader(string HeaderText,
            WdParagraphAlignment alignment, int fontSize, int bold) //添加頁首
        {
            _app.ActiveWindow.View.Type = WdViewType.wdOutlineView;
            _app.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
            _app.ActiveWindow.ActivePane.Selection.Font.Size = fontSize;
            _app.ActiveWindow.ActivePane.Selection.Font.Bold = bold;
            _app.ActiveWindow.ActivePane.Selection.Text += HeaderText;
            _app.Selection.ParagraphFormat.Alignment = alignment;

            _app.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
        }

        public void AddSimpleFooter(string FooterText,
            WdParagraphAlignment alignment, int fontSize, int bold) //添加頁尾
        {
            _app.ActiveWindow.View.Type = WdViewType.wdOutlineView;
            _app.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryFooter;
            _app.ActiveWindow.ActivePane.Selection.Font.Size = fontSize;
            _app.ActiveWindow.ActivePane.Selection.Font.Bold = bold;
            _app.ActiveWindow.ActivePane.Selection.Text = FooterText;
            _app.Selection.ParagraphFormat.Alignment = alignment;
            _app.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
        }

        public void AddSimpleFooter(WdParagraphAlignment alignment, int rowCount, int colCount,
            WdLineStyle outStyle, WdLineStyle inStyle, int fontSize, int bold) //添加頁尾,頁尾裡加表格
        {
            _app.ActiveWindow.View.Type = WdViewType.wdOutlineView;
            _app.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryFooter;

            _app.ActiveWindow.ActivePane.Selection.Font.Size = fontSize;
            _app.ActiveWindow.ActivePane.Selection.Font.Bold = bold;

            object oMissing = System.Reflection.Missing.Value;
            _tblFoot = _app.ActiveWindow.ActivePane.Selection.Tables.Add(
                _app.ActiveWindow.ActivePane.Selection.Range, rowCount, colCount, ref oMissing, ref oMissing);

            _app.Selection.ParagraphFormat.Alignment = alignment;
            _app.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
        }

        public void AddTable(int rowCount, int colCount,
            WdLineStyle outStyle, WdLineStyle inStyle) //添加表格
        {
            object oMissing = System.Reflection.Missing.Value;
            _tbl = _doc.Tables.Add(_app.Selection.Range, rowCount, colCount, ref oMissing, ref oMissing);
            _tbl.Borders.OutsideLineStyle = outStyle;
            _tbl.Borders.InsideLineStyle = inStyle;
        }

        public void AddRow()
        {
            Object Nothing = System.Reflection.Missing.Value;
            _doc.Content.Tables[1].Rows.Add(ref Nothing);
        }

        #region 設定儲存格的值
        public void SetCellValue(int x, int y, string value)//X行Y列 value值
        {
            _tbl.Cell(x, y).Range.Text = value;
        }
        #endregion

        #region 設定儲存格的值(頁尾)
        public void SetFooterCellValue(int x, int y, string value)//X行Y列 value值
        {
            _tblFoot.Cell(x, y).Range.Text = value;
        }
        #endregion

        #region 設定儲存格格式
        public void UniteCells(int x1, int y1, int x2, int y2) //合併儲存格
        {
            _tbl.Cell(x1, y1).Merge(_tbl.Cell(x2, y2));
        }

        public void SetCellAlignmentH(int x, int y, //儲存格對齊1(水平)
            WdRowAlignment alignment)
        {
            _tbl.Cell(x, y).Select();
            _app.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
        }

        public void SetCellAlignmentH(int Startx, int Starty, //儲存格對齊2(水平)
            int Endx, int Endy, WdRowAlignment alignment)
        {
            for (int i = Startx; i <= Endx; i++)
                for (int j = Starty; j <= Endy; j++)
                {
                    SetCellAlignmentH(i, j, alignment);
                }
        }

        public void SetCellAlignmentV(int x, int y, //儲存格對齊1(豎直)
            WdCellVerticalAlignment alignment)
        {
            _tbl.Cell(x, y).Select();
            _app.Selection.Cells.VerticalAlignment = alignment;
        }

        public void SetCellAlignmentV(int Startx, int Starty, //儲存格對齊2(豎直)
            int Endx, int Endy, WdCellVerticalAlignment alignment)
        {
            for (int i = Startx; i <= Endx; i++)
                for (int j = Starty; j <= Endy; j++)
                {
                    SetCellAlignmentV(i, j, alignment);
                }
        }

        public void SetRowHeight(int rowIndex, int height) //設定行高
        {
            _tbl.Rows[rowIndex].Select();
            _app.Selection.Cells.Height = height;
        }

        public void SetColWidth(int colIndex, int width) //設定列寬
        {
            _tbl.Columns[colIndex].Select();
            _app.Selection.Cells.Width = width;
 
        }

        public void SetCellFont(int x, int y, int fontSize, int bold) //字型
        {
            _tbl.Cell(x, y).Range.Font.Size = fontSize;
            _tbl.Cell(x, y).Range.Font.Bold = bold;
        }

        public void SetCellBorder(int x, int y, WdLineStyle lineStyle) //邊框1
        {
            _tbl.Cell(x, y).Range.Borders[WdBorderType.wdBorderLeft].LineStyle = lineStyle;
            _tbl.Cell(x, y).Range.Borders[WdBorderType.wdBorderRight].LineStyle = lineStyle;
            _tbl.Cell(x, y).Range.Borders[WdBorderType.wdBorderTop].LineStyle = lineStyle;
            _tbl.Cell(x, y).Range.Borders[WdBorderType.wdBorderBottom].LineStyle = lineStyle;
        }

        public void SetCellBorder(int Startx, int Starty, int Endx, int Endy, WdLineStyle lineStyle) //邊框2
        {
            for (int i = Startx; i <= Endx; i++)
                for (int j = Starty; j <= Endy; j++)
                {
                    SetCellBorder(i, j, lineStyle);
                }
        }

        public void setCellWidth(int x, int y, int width) //設定儲存格寬度1
        {
            _tbl.Cell(x, y).Range.Columns.Width = width;
        }

        public void setCellHeight(int x, int y, int height) //設定儲存格高度1
        {
            _tbl.Cell(x, y).Range.Rows.Height = height;
        }

        public void setCellWidth(int Startx, int Starty, int Endx, int Endy, int width) //設定儲存格寬度2
        {
            for (int i = Startx; i <= Endx; i++)
                for (int j = Starty; j < Endy; j++)
                {
                    setCellWidth(i, j, width);
                }
        }

        public void SetCellSize(int x, int y, int height, int width) //行高、列寬1
        {
            _tbl.Cell(x, y).Range.Columns.Width = width;
            _tbl.Cell(x, y).Range.Rows.Height = height;
        }

        public void SetCellSize(int Startx, int Starty, int Endx, int Endy, int height, int width) //行高、列寬2
        {
            for (int i = Startx; i <= Endx; i++)
                for (int j = Starty; j < Endy; j++)
                {
                    SetCellSize(i, j, height, width);
                }
        }

        #endregion

        #region 設定儲存格格式(頁尾)
        public void UniteFooterCellsH(int x1, int y1, int x2, int y2) //橫向合併儲存格
        {
            _app.ActiveWindow.View.Type = WdViewType.wdOutlineView;
            _app.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryFooter;

            _tblFoot.Cell(x1, y1).Merge(_tbl.Cell(x2, y2));

            _app.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
        }

        public void UniteFooterCellsV(int x1, int y1, object moveCount) //縱向合併儲存格
        {
            _app.ActiveWindow.View.Type = WdViewType.wdOutlineView;
            _app.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryFooter;

            _tblFoot.Cell(x1, y1).Select(); //選中一行

            object moveUnit = WdUnits.wdLine;
            object moveExtend = WdMovementType.wdExtend;
            _app.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);

            _app.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
        }

        public void SetFooterCellAlignment(int x, int y, //儲存格對齊1
            WdRowAlignment alignment)
        {
            _app.ActiveWindow.View.Type = WdViewType.wdOutlineView;
            _app.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryFooter;

            _tblFoot.Cell(x, y).Range.Rows.Alignment = alignment;

            _app.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
        }

        public void SetFooterCellAlignment(int Startx, int Starty, //儲存格對齊2
            int Endx, int Endy, WdRowAlignment alignment)
        {
            for (int i = Startx; i < Endx; i++)
                for (int j = Starty; j < Endy; j++)
                {
                    SetFooterCellAlignment(i, j, alignment);
                }
        }

        public void SetFooterCellFont(int x, int y, int fontSize, int bold) //字型
        {
            _app.ActiveWindow.View.Type = WdViewType.wdOutlineView;
            _app.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryFooter;

            _tblFoot.Cell(x, y).Range.Font.Size = fontSize;
            _tblFoot.Cell(x, y).Range.Font.Bold = bold;

            _app.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
        }

        public void SetFooterCellBorder(int x, int y, WdLineStyle lineStyle) //邊框1
        {
            _app.ActiveWindow.View.Type = WdViewType.wdOutlineView;
            _app.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryFooter;

            _tblFoot.Cell(x, y).Range.Borders[WdBorderType.wdBorderLeft].LineStyle = lineStyle;
            _tblFoot.Cell(x, y).Range.Borders[WdBorderType.wdBorderRight].LineStyle = lineStyle;
            _tblFoot.Cell(x, y).Range.Borders[WdBorderType.wdBorderTop].LineStyle = lineStyle;
            _tblFoot.Cell(x, y).Range.Borders[WdBorderType.wdBorderBottom].LineStyle = lineStyle;

            _app.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
        }

        public void SetFooterCellBorder(int Startx, int Starty, int Endx, int Endy, WdLineStyle lineStyle) //邊框2
        {
            for (int i = Startx; i <= Endx; i++)
                for (int j = Starty; j < Endy; j++)
                {
                    SetFooterCellBorder(i, j, lineStyle);
                }
        }

        public void SetFooterRowHeight(int rowIndex, int height) //設定行高
        {
            _app.ActiveWindow.View.Type = WdViewType.wdOutlineView;
            _app.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryFooter;

            _tblFoot.Cell(rowIndex, 0).Select();
            _app.Selection.Cells.Height = height;

            _app.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
        }

        public void SetFooterColWidth(int colIndex, int width) //設定列寬
        {
            _app.ActiveWindow.View.Type = WdViewType.wdOutlineView;
            _app.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryFooter;

            _tblFoot.Cell(0, colIndex).Select();
            _app.Selection.Cells.Width = width;

            _app.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
        }
        public static string GetPath()
        {
            Random r = new Random();
            string str = r.Next().ToString();
            string path = DateTime.Now.ToString("yyyyMMddhhmmss") + str;
            return path;
        }

        #endregion
    }
}

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/zwk_9/archive/2010/09/16/5887831.aspx

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.