C#操作Word

来源:互联网 发布:淘宝产品上架教程视频 编辑:程序博客网 时间:2024/05/01 01:12

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
    }
}