加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C# 导出到Word中的四种格式

(2013-08-23 13:01:57)
标签:

标题

数据

格式

四种

单元格

it

分类: C#
///
        /// 导出Word
        ///
        /// 类容
        /// 列数
        public static void OutPutWordDT(List strContent, int Col)
        {
            Object Nothing = System.Reflection.Missing.Value;
            Application oword = new Application();
            Document odoc = oword.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

            try
            {
                int Rows = strContent.Count % Col;
                if (Rows != 0)
                    Rows = Rows + 1;
                else
                    Rows = strContent.Count / Col;

                odoc.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

                Table otable = odoc.Tables.Add(oword.Selection.Range, Rows, Col, ref Nothing, ref Nothing);
                otable.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//设置对其方式
                otable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置表格边框样式

                //otable.Cell(1, 1).Merge(otable.Cell(2, 1)); //合并单元格

                int index = 0;

                for (int i = 1; i <= Rows; i++)
                {
                    for (int j = 1; j <= Col && index < strContent.Count; j++, index++)
                    {
                        otable.Cell(i, j).Range.Text = strContent[index];
                        otable.Cell(i, j).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式
                    }
                }
                oword.Visible = true;
            }
            catch (Exception) { }
        }

        ///
        /// 导出Word
        ///
        /// 导出的数据DataTable
        /// 是否显示列名
        public static void OutPutWordDT(DataTable dt, bool isColname)
        {
            Object Nothing = System.Reflection.Missing.Value;
            Application oword = new Application();
            Document odoc = oword.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            odoc.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            try
            {
                Table otable = odoc.Tables.Add(oword.Selection.Range, dt.Rows.Count + 1, dt.Columns.Count, ref Nothing, ref Nothing);
                otable.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//设置对其方式
                otable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置表格边框样式

                if (isColname)
                {
                    int intcol = 0;
                    for (int ii = 0; ii < dt.Columns.Count; ii++)
                    {
                        intcol += 1;
                        otable.Cell(1, intcol).Range.Text = dt.Columns[ii].ColumnName;
                        otable.Cell(1, intcol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式
                    }
                }

                int intRow = 1;
                for (int ii = 0; ii < dt.Rows.Count; ii++)
                {
                    intRow += 1;
                    int intCol = 0;
                    for (int jj = 0; jj < dt.Columns.Count; jj++)
                    {
                        intCol += 1;
                        otable.Cell(intRow, intCol).Range.Text = dt.Rows[ii][jj].ToString();
                        otable.Cell(intRow, intCol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式
                    }
                }
                oword.Visible = true;
            }
            catch (Exception) { }
        }

        ///
        /// 导出Word
        ///
        /// 标题
        /// 导出的数据DataTable
        /// 是否显示列名
        public static void OutPutWordDT(string strTitle, DataTable dt, bool isColname)
        {
            Object Nothing = System.Reflection.Missing.Value;
            Application oword = new Application();
            Document odoc = oword.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            odoc.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            try
            {
                Table otable = odoc.Tables.Add(oword.Selection.Range, dt.Rows.Count + 2, dt.Columns.Count, ref Nothing, ref Nothing);
                otable.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//设置对其方式
                otable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置表格边框样式

                otable.Cell(1, 1).Merge(otable.Cell(1, dt.Columns.Count)); //合并单元格
                otable.Cell(1, 1).Range.Text = strTitle;
                otable.Cell(1, 1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                otable.Cell(1, 1).Range.Font.Bold = 1;
                otable.Cell(1, 1).Range.Font.Size = 20;

                if (isColname)
                {
                    int intCol = 0;
                    for (int ii = 0; ii < dt.Columns.Count; ii++)
                    {
                        intCol += 1;
                        otable.Cell(2, intCol).Range.Text = dt.Columns[ii].ColumnName;
                        otable.Cell(2, intCol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式
                    }
                }

                int intRow = 2;
                for (int ii = 0; ii < dt.Rows.Count; ii++)
                {
                    intRow += 1;
                    int intcol = 0;
                    for (int jj = 0; jj < dt.Columns.Count; jj++)
                    {
                        intcol += 1;
                        otable.Cell(intRow, intcol).Range.Text = dt.Rows[ii][jj].ToString();
                        otable.Cell(intRow, intcol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式
                    }
                }
                oword.Visible = true;
            }
            catch (Exception) { }
        }

        ///
        /// 导出为Word
        ///
        /// 导出的数据
        /// 多少列
        /// 标题
        public static void OutPutWordDT(DataTable dt, int Col, string strTitle)
        {
            Object Nothing = System.Reflection.Missing.Value;
            Application oword = new Application();
            Document odoc = oword.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            odoc.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            try
            {
                int Rowint = (dt.Columns.Count * 2) % Col;
                if (Rowint != 0)
                    Rowint = Rowint + 1;
                else
                    Rowint = (dt.Columns.Count * 2) / Col;

                Table otable = odoc.Tables.Add(oword.Selection.Range, Rowint + 1, Col, ref Nothing, ref Nothing);
                otable.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//设置对其方式
                otable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置表格边框样式

                otable.Cell(1, 1).Merge(otable.Cell(1, Col)); //合并单元格
                otable.Cell(1, 1).Range.Text = strTitle;
                otable.Cell(1, 1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                otable.Cell(1, 1).Range.Font.Bold = 1;
                otable.Cell(1, 1).Range.Font.Size = 20;

                int icol = 0;

                for (int iRow = 2; iRow <= Rowint + 1; iRow++)
                {
                    for (int incol = 1; incol <= Col && icol < dt.Columns.Count; incol++)
                    {
                        if (incol % 2 != 0)
                        {
                            otable.Cell(iRow, incol).Range.Text = dt.Columns[icol].ColumnName + ":";
                            otable.Cell(iRow, incol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式
                        }
                        else
                        {
                            otable.Cell(iRow, incol).Range.Text = dt.Rows[0][icol].ToString();
                            otable.Cell(iRow, incol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式
                            icol++;
                        }
                    }
                }

                oword.Visible = true;
            }
            catch (Exception) { }

        }

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有