C#的word导出

C# 专栏收录该内容
1 篇文章 0 订阅

引用Microsoft.Office.Interop.Word或是Aspose.Words

原本我是使用Microsoft的,但是服务器的电脑不允许装word等其他应用,所以后面就用了Aspose.words,所以这里区别就出来了,Microsoft的需要在本地安装word,而Aspose.words不用安装也能完成导出操作,至于导出文本样式,我用起来感觉没什么差别。

Microsoft.Office.Interop.Word

参考了很多,注意右键属性嵌入互操作类型设置为false不然项目起不来。

object path;                              //文件路径变量
            string strContent;                        //文本内容变量
            MSWord.Application wordApp;                   //Word应用程序变量 
            MSWord.Document wordDoc;                  //Word文档变量

            string rootPath = AppDomain.CurrentDomain.BaseDirectory;

            path = $"{rootPath}\\Document\\WORD\\" + uuid + ".doc"; //文档保存位置

            wordApp = new MSWord.ApplicationClass(); //初始化

            //wordApp.Visible = true;//使文档可见

            //如果已存在,则删除
            if (File.Exists((string)path))
            {
                File.Delete((string)path);
            }

            //由于使用的是COM库,因此有许多变量需要用Missing.Value代替
            Object Nothing = Missing.Value;
            wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

            #region 页面设置、页眉图片和文字设置,最后跳出页眉设置

            //页面设置
            wordDoc.PageSetup.PaperSize = MSWord.WdPaperSize.wdPaperA4;//设置纸张样式为A4纸
            wordDoc.PageSetup.Orientation = MSWord.WdOrientation.wdOrientPortrait;//排列方式为垂直方向
            wordDoc.PageSetup.TopMargin = 57.0f;
            wordDoc.PageSetup.BottomMargin = 57.0f;
            wordDoc.PageSetup.LeftMargin = 57.0f;
            wordDoc.PageSetup.RightMargin = 57.0f;
            wordDoc.PageSetup.HeaderDistance = 30.0f;//页眉位置


            //去掉页眉的横线
            wordApp.ActiveWindow.ActivePane.Selection.ParagraphFormat.Borders[MSWord.WdBorderType.wdBorderBottom].LineStyle = MSWord.WdLineStyle.wdLineStyleNone;
            wordApp.ActiveWindow.ActivePane.Selection.Borders[MSWord.WdBorderType.wdBorderBottom].Visible = false;
            wordApp.ActiveWindow.ActivePane.View.SeekView = MSWord.WdSeekView.wdSeekMainDocument;//退出页眉设置
            #endregion

            #region 页码设置并添加页码

            //为当前页添加页码
            MSWord.PageNumbers pns = wordApp.Selection.Sections[1].Headers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers;//获取当前页的号码
            pns.NumberStyle = MSWord.WdPageNumberStyle.wdPageNumberStyleNumberInDash;//设置页码的风格,是Dash形还是圆形的
            pns.HeadingLevelForChapter = 0;
            pns.IncludeChapterNumber = false;
            pns.RestartNumberingAtSection = false;
            pns.StartingNumber = 0; //开始页页码?
            object pagenmbetal = MSWord.WdPageNumberAlignment.wdAlignPageNumberCenter;//将号码设置在中间
            object first = true;
            wordApp.Selection.Sections[1].Footers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers.Add(ref pagenmbetal, ref first);

            #endregion

            #region 行间距与缩进、文本字体、字号、加粗、斜体、颜色、下划线、下划线颜色设置

            wordApp.Selection.ParagraphFormat.LineSpacing = 16f;//设置文档的行间距
            wordApp.Selection.ParagraphFormat.FirstLineIndent = 30;//首行缩进的长度
            


            //写入15号字体文本
            object unite = MSWord.WdUnits.wdStory;
            wordApp.Selection.EndKey(ref unite, ref Nothing);//将光标移到文本末尾
            wordApp.Selection.ParagraphFormat.FirstLineIndent = 0;
            strContent = "安全环保现场督查数据周报" + "\n";
            wordApp.Selection.EndKey(ref unite, ref Nothing);
            wordDoc.Paragraphs.Last.Range.Font.Size = 30;
            wordDoc.Paragraphs.Last.Range.Font.Name = "黑体";
            wordDoc.Paragraphs.Last.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
            wordDoc.Paragraphs.Last.Range.Font.Bold = 1;
            wordDoc.Paragraphs.Last.Range.Text = strContent;

            wordApp.Selection.EndKey(ref unite, ref Nothing);//将光标移到文本末尾
            wordApp.Selection.ParagraphFormat.FirstLineIndent = 0;

            strContent = "安全环保现场督查数据统计、主要问题(隐患)及整改完成情况" + "\n";
            wordApp.Selection.EndKey(ref unite, ref Nothing);
            wordDoc.Paragraphs.Last.Range.Font.Size = 15;
            wordDoc.Paragraphs.Last.Range.Font.Name = "宋体";
            wordDoc.Paragraphs.Last.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
            wordDoc.Paragraphs.Last.Range.Text = strContent;


            strContent = "一、隐患分类统计" + "\n";
            wordApp.Selection.EndKey(ref unite, ref Nothing);
            wordDoc.Paragraphs.Last.Range.Font.Size = 15;
            wordDoc.Paragraphs.Last.Range.Font.Name = "宋体";
            wordDoc.Paragraphs.Last.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
            wordDoc.Paragraphs.Last.Range.Text = strContent;
            


            #endregion


            #region 插入图片、居中显示,设置图片的绝对尺寸和缩放尺寸,并给图片添加标题

          

            for (int a = 0; a < 1; a++)
            {

                wordDoc.Content.InsertAfter("\n");//这一句与下一句的顺序不能颠倒,原因还没搞透
                wordApp.Selection.EndKey(ref unite, ref Nothing); //将光标移动到文档末尾
                wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
                wordApp.Selection.Font.Size = 8;

                int tableRow = AreaNum.Rows.Count + 1;
                int tableColumn = 11;
                MSWord.Table table = wordDoc.Tables.Add(wordApp.Selection.Range,
                tableRow, tableColumn, ref Nothing, ref Nothing);





                //默认创建的表格没有边框,这里修改其属性,使得创建的表格带有边框 
                table.Borders.Enable = 1;//这个值可以设置得很大,例如5、13等等

                //表格的索引是从1开始的。
                //wordDoc.Tables[1].Cell(1, 1).Range.Text = "列\n行";
                for (int i = 1; i <= tableRow; i++)
                {
                    for (int j = 1; j <= tableColumn; j++)
                    {
                        int nnum;
                        if (i == 1)
                        {
                            nnum = j - 1;
                        }
                        else
                        {
                            nnum = (i - 1) * tableColumn + j - 1;
                        }


                        if (i == 1)
                        {
                            if (j == 1)
                            {
                                table.Cell(i, j).Range.Text = "被督查单位";
                            }
                            else if (j == 2)
                            {
                                table.Cell(i, j).Range.Text = "合计";
                            }
                            else if (j == 3)
                            {
                                table.Cell(i, j).Range.Text = "安全基础管理";
                            }
                            else if (j == 4)
                            {
                                table.Cell(i, j).Range.Text = "工艺";
                            }
                            else if (j == 5)
                            {
                                table.Cell(i, j).Range.Text = "设备";
                            }
                            else if (j == 6)
                            {
                                table.Cell(i, j).Range.Text = "电气仪表";
                            }
                            else if (j == 7)
                            {
                                table.Cell(i, j).Range.Text = "环保";
                            }
                            else if (j == 8)
                            {
                                table.Cell(i, j).Range.Text = "消气防";
                            }
                            else if (j == 9)
                            {
                                table.Cell(i, j).Range.Text = "承包商管理";
                            }
                            else if (j == 10)
                            {
                                table.Cell(i, j).Range.Text = "现场";
                            }
                            else
                            {
                                table.Cell(i, j).Range.Text = "其他";
                            }
                        }
                        else
                        {
                            if (j == 1)
                            {
                                table.Cell(i, j).Range.Text = AreaNum.Rows[i - 2]["COMPANY_NAME"].ToString();
                            }
                            else if (j == 2)
                            {
                                table.Cell(i, j).Range.Text = "";
                            }
                            else if (j == 3)
                            {
                                table.Cell(i, j).Range.Text = AreaNum.Rows[i - 2]["SAFE"].ToString();

                            }
                            else if (j == 4)
                            {
                                table.Cell(i, j).Range.Text = AreaNum.Rows[i - 2]["CRAFT"].ToString();

                            }
                            else if (j == 5)
                            {
                                table.Cell(i, j).Range.Text = AreaNum.Rows[i - 2]["DEVICE"].ToString();

                            }
                            else if (j == 6)
                            {
                                table.Cell(i, j).Range.Text = AreaNum.Rows[i - 2]["EIE_METER"].ToString();

                            }
                            else if (j == 7)
                            {
                                table.Cell(i, j).Range.Text = AreaNum.Rows[i - 2]["ENVIRONMENT"].ToString();

                            }
                            else if (j == 8)
                            {
                                table.Cell(i, j).Range.Text = AreaNum.Rows[i - 2]["FIREFIGHT"].ToString();

                            }
                            else if (j == 9)
                            {
                                table.Cell(i, j).Range.Text = AreaNum.Rows[i - 2]["CONTRACTOR"].ToString();

                            }
                            else if (j == 10)
                            {
                                table.Cell(i, j).Range.Text = AreaNum.Rows[i - 2]["SITE"].ToString();

                            }
                            else
                            {
                                table.Cell(i, j).Range.Text = AreaNum.Rows[i - 2]["QT"].ToString();

                            }
                        }
                    }
                }
            }

            wordApp.Selection.EndKey(ref unite, ref Nothing);//将光标移到文本末尾
            wordApp.Selection.ParagraphFormat.FirstLineIndent = 0;
            strContent = "二、督查主要问题(隐患)" + "\n";
            wordApp.Selection.EndKey(ref unite, ref Nothing);
            wordDoc.Paragraphs.Last.Range.Font.Size = 30;
            wordDoc.Paragraphs.Last.Range.Font.Name = "黑体";
            wordDoc.Paragraphs.Last.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
            wordDoc.Paragraphs.Last.Range.Font.Bold = 1;
            wordDoc.Paragraphs.Last.Range.Text = strContent;


            List<Object> rlist = new List<Object>();
            List<Object> llist = new List<Object>();
            List<Object> slist = new List<Object>();
            List<Object> flist = new List<Object>();
            for (int i = 0; i < dctable.Rows.Count; i++)
            {
                wordApp.Selection.EndKey(ref unite, ref Nothing);//将光标移到文本末尾
                wordApp.Selection.ParagraphFormat.FirstLineIndent = 0;
                strContent = dctable.Rows[i]["DC_NAME"].ToString() + "\n";
                wordApp.Selection.EndKey(ref unite, ref Nothing);
                wordDoc.Paragraphs.Last.Range.Font.Size = 30;
                wordDoc.Paragraphs.Last.Range.Font.Name = "黑体";
                wordDoc.Paragraphs.Last.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
                wordDoc.Paragraphs.Last.Range.Font.Bold = 1;
                wordDoc.Paragraphs.Last.Range.Text = strContent;

                wordApp.Selection.EndKey(ref unite, ref Nothing);//将光标移到文本末尾
                wordApp.Selection.ParagraphFormat.FirstLineIndent = 0;

                wordApp.Selection.EndKey(ref unite, ref Nothing);//将光标移到文本末尾
                wordApp.Selection.ParagraphFormat.FirstLineIndent = 0;
                strContent = dctable.Rows[i]["COMPANY_NAME"].ToString() + "\n";
                wordApp.Selection.EndKey(ref unite, ref Nothing);
                wordDoc.Paragraphs.Last.Range.Font.Size = 25;
                wordDoc.Paragraphs.Last.Range.Font.Name = "黑体";
                wordDoc.Paragraphs.Last.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
                wordDoc.Paragraphs.Last.Range.Font.Bold = 1;
                wordDoc.Paragraphs.Last.Range.Text = strContent;

                wordApp.Selection.EndKey(ref unite, ref Nothing);//将光标移到文本末尾
                wordApp.Selection.ParagraphFormat.FirstLineIndent = 0;

                strContent = dctable.Rows[i]["DC_CONTENT"].ToString() + "\n";
                wordApp.Selection.EndKey(ref unite, ref Nothing);
                wordDoc.Paragraphs.Last.Range.Font.Size = 15;
                wordDoc.Paragraphs.Last.Range.Font.Name = "宋体";
                wordDoc.Paragraphs.Last.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
                wordDoc.Paragraphs.Last.Range.Text = strContent;


                //将光标移动到文档末尾
                //图片文件的路径
                //string filename = Environment.CurrentDirectory + "\\6.jpg";
                string filename = dctable.Rows[i]["DC_IMGURL"].ToString();
                flist.Add(filename);
                //要向Word文档中插入图片的位置
                rlist.Add(wordDoc.Paragraphs.Last.Range);
                llist.Add(false);
                slist.Add(true);
                wordApp.Selection.EndKey(ref unite, ref Nothing);

                wordDoc.Paragraphs.Last.Range.Font.Size = 15;
                wordDoc.Paragraphs.Last.Range.Font.Name = "宋体";
                wordDoc.Paragraphs.Last.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
                wordDoc.Paragraphs.Last.Range.Text = "   ";

            }

            for (int i = 0; i < dctable.Rows.Count; i++)
            {
                wordDoc.InlineShapes.AddPicture(flist[i].ToString(), llist[i], slist[i], rlist[i]);
                wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;//居中显示图片

            }

            //WdSaveFormat为Word 2003文档的保存格式
            object format = MSWord.WdSaveFormat.wdFormatDocument;// office 2007就是wdFormatDocumentDefault
            //将wordDoc文档对象的内容保存为DOCX文档
            wordDoc.SaveAs(ref path, ref format, 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文档对象

            //看是不是要打印
            //wordDoc.PrintOut();



            wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
            //关闭wordApp组件对象
            wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
			//返回文档路径给前台
            return path.ToString();

导出样子如下
在这里插入图片描述
在这里插入图片描述

Aspose.Words

namespace NewGIS.Mms.Data.Util
{
    public class MExport
    {
        public string word(DataTable dt, DataTable qh, DataTable ZGKHGZS, DataTable dckhhj, DataTable zyczwt, DataTable zyglfl, DataTable yhzl, DataTable bgt, DataTable xxdt, DataTable wtcs,DataTable syyh, string uuid)
        {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            builder.PageSetup.PaperSize = PaperSize.A4;  //设置成A4大小
            Aspose.Words.Font font = builder.Font;
            font.Size = 18; //字体大小
            font.Bold = false; //是否加粗
            font.Color = System.Drawing.Color.Black; //字体颜色
            font.Underline = Underline.None; //下划线
            ParagraphFormat paragraphFormat = builder.ParagraphFormat;

            //日期格式
           
            string mon = DateTime.Now.Month.ToString();
            string year = DateTime.Now.Year.ToString();
            string day = DateTime.Now.Day.ToString();

            builder.Writeln("第" + qh.Rows[0]["TERM"].ToString() + "期");
            builder.Writeln("                                  (总第" + qh.Rows[0]["TOTALTERM"].ToString() + "期)");
            
            paragraphFormat.Alignment = ParagraphAlignment.Left;
           
            
            builder.Writeln(" ");
            font.Bold = false;
            font.Size = 16;

            //
            //paragraphFormat.Alignment = ParagraphAlignment.Center;
            builder.Writeln("           巨化QHSE中心安全环保      "+year+"年"+mon+"月"+day+"日");
            builder.Writeln(" ");
            font.Size = 14;
            font.Bold = true;
            
            builder.Writeln("       本期导读:事故事件情况、隐患数据分析、安全风险公告情况、特殊作业监管情况、引起重视的问题、安全管理改善项目实施情况、问题分析和建议、曝光台、整体反馈、信息动态、存在主要问题。");
            font.Size = 14;
            font.Bold = false;

            string qhdate = qh.Rows[0]["GMCREATE"].ToString();
            DateTime qdate = Convert.ToDateTime(qhdate);
            

            builder.Writeln("       "+qdate.Year.ToString()+"年"+ qdate.Month.ToString()+ "月"+qdate.Day.ToString()+"日,下发《安全环保督查告知书》"+ dckhhj.Rows[0]["DC_AQHBGZS"]+ "份,共发现问题隐患"+ dckhhj.Rows[0]["DC_WTYH"]+"条;下发《区域安全管理考核告知书》"+ dckhhj.Rows[0]["DC_QYAQGZS"] + "份,共处罚款"+ dckhhj.Rows[0]["DC_CFJE"] + "元.");
            builder.Writeln(" ");
            font.Size = 14;
            font.Bold = true;
            
            builder.Writeln("  【事故事件情况】");
            for (int i = 0; i < dt.Rows.Count; i++) {
                if (dt.Rows[i]["GUIDE_TYPE"].ToString() == "1") {
                    font.Size = 14;
                    font.Bold = false;
                    builder.Writeln("       "+dt.Rows[i]["GUIDE_CONTENT"].ToString());
                    break;
                }
            }
            font.Size = 14;
            font.Bold = true;
            builder.Writeln("  【隐患数据分析】");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["GUIDE_TYPE"].ToString() == "2")
                {
                    font.Size = 14;
                    font.Bold = false;
                    builder.Writeln("       "+dt.Rows[i]["GUIDE_CONTENT"].ToString());
                    break;
                }
            }
            font.Size = 14;
            font.Bold = true;
            builder.Writeln("  【安全风险公告情况】");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["GUIDE_TYPE"].ToString() == "3")
                {
                    font.Size = 14;
                    font.Bold = false;
                    builder.Writeln("       "+dt.Rows[i]["GUIDE_CONTENT"].ToString());
                    break;
                }
            }
            font.Size = 14;
            font.Bold = true;
            builder.Writeln("  【特殊作业监管情况】");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["GUIDE_TYPE"].ToString() == "4")
                {
                    font.Size = 14;
                    font.Bold = false;
                    builder.Writeln("       "+dt.Rows[i]["GUIDE_CONTENT"].ToString());
                    break;
                }
            }
            font.Size = 14;
            font.Bold = true;
            builder.Writeln("  【引起重视的问题】");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["GUIDE_TYPE"].ToString() == "5")
                {
                    font.Size = 14;
                    font.Bold = false;
                    builder.Writeln("       "+dt.Rows[i]["GUIDE_CONTENT"].ToString());
                    break;
                }
            }
            font.Size = 14;
            font.Bold = true;
            builder.Writeln("  【安全管理改善项目实施情况】");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["GUIDE_TYPE"].ToString() == "6")
                {
                    font.Size = 14;
                    font.Bold = false;
                    builder.Writeln("       "+dt.Rows[i]["GUIDE_CONTENT"].ToString());
                    break;
                }
            }
            font.Size = 14;
            font.Bold = true;
            builder.Writeln("  【问题分析和意见措施】");
            if (wtcs.Rows.Count != 0) {
                builder.Writeln("  督查一");
                for (int a = 0; a < wtcs.Rows.Count; a++)
                {
                    if (wtcs.Rows[a]["DC_NAME"].ToString() == "督查一") {
                        int index = a + 1;
                        font.Size = 14;
                        font.Bold = false;

                        builder.Writeln("  " + index + "、" + wtcs.Rows[a]["GUIDE_CONTENT"] + "");
                        builder.Writeln("   建议");
                        builder.Writeln(wtcs.Rows[a]["BAK2"] + "");
                    }
                   
                }
                builder.Writeln("  督查二");
                for (int a = 0; a < wtcs.Rows.Count; a++)
                {
                    if (wtcs.Rows[a]["DC_NAME"].ToString() == "督查二")
                    {
                        int index = a + 1;
                        font.Size = 14;
                        font.Bold = false;

                        builder.Writeln("  " + index + "、" + wtcs.Rows[a]["GUIDE_CONTENT"] + "");
                        builder.Writeln("   建议");
                        builder.Writeln(wtcs.Rows[a]["BAK2"] + "");
                    }
                }

                builder.Writeln("  督查三");
                for (int a = 0; a < wtcs.Rows.Count; a++)
                {
                    if (wtcs.Rows[a]["DC_NAME"].ToString() == "督查三")
                    {
                        int index = a + 1;
                        font.Size = 14;
                        font.Bold = false;

                        builder.Writeln("  " + index + "、" + wtcs.Rows[a]["GUIDE_CONTENT"] + "");
                        builder.Writeln("   建议");
                        builder.Writeln(wtcs.Rows[a]["BAK2"] + "");
                    }
                }
            }


            font.Size = 14;
            font.Bold = true;
            builder.Writeln("  【整改反馈】");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["GUIDE_TYPE"].ToString() == "7")
                {
                    font.Size = 14;
                    font.Bold = false;
                    builder.Writeln(dt.Rows[i]["GUIDE_CONTENT"].ToString());
                    break;
                }
            }
            builder.InsertBreak(BreakType.PageBreak);
            font.Size = 14;
            font.Bold = true;
            builder.Writeln("  【曝光台】");
            builder.Writeln("  督查一");
            int y = 0;
            if (bgt.Rows.Count != 0) {
                for (int b = 0; b < bgt.Rows.Count; b++)
                {
                    font.Size = 14;
                    font.Bold = false;
                    
                    if (bgt.Rows[b]["DC_NAME"].ToString() == "督查一") {
                        y++;
                        builder.Writeln("  " + bgt.Rows[b]["GUIDE_CONTENT"].ToString());
                        string imgPath = bgt.Rows[b]["GUIDE_IMGURL"].ToString();
                        try
                        {
                            string[] arr = imgPath.Split(',');
                            for (int i = 0; i < arr.Length; i++)
                            {
                                builder.InsertImage(arr[i], 400, 225);
                            }
                        }
                        catch
                        {
                            continue;
                        }

                        builder.Writeln(" ");
                        if (y != 0 && y / 2 == 0)//两个隐患分一页
                        {
                            builder.InsertBreak(BreakType.PageBreak);
                        }
                    }
                }

                builder.Writeln("  督查二");
                int t = 0;
                for (int b = 0; b < bgt.Rows.Count; b++)
                {
                    font.Size = 14;
                    font.Bold = false;
                    

                    if (bgt.Rows[b]["DC_NAME"].ToString() == "督查二")
                    {
                        t++;
                        builder.Writeln("  " + bgt.Rows[b]["GUIDE_CONTENT"].ToString());
                        string imgPath = bgt.Rows[b]["GUIDE_IMGURL"].ToString();
                        try
                        {
                            string[] arr = imgPath.Split(',');
                            for (int i = 0; i < arr.Length; i++)
                            {
                                builder.InsertImage(arr[i], 400, 225);
                            }
                        }
                        catch
                        {
                            continue;
                        }

                        builder.Writeln(" ");
                        if (t != 0 && t / 2 == 0)//两个隐患分一页
                        {
                            builder.InsertBreak(BreakType.PageBreak);
                        }
                    }
                }
                builder.Writeln("  督查三");
                int u = 0;
                for (int b = 0; b < bgt.Rows.Count; b++)
                {
                    font.Size = 14;
                    font.Bold = false;
                    


                    if (bgt.Rows[b]["DC_NAME"].ToString() == "督查三")
                    {
                        u++;
                        builder.Writeln("  " + bgt.Rows[b]["GUIDE_CONTENT"].ToString());
                        string imgPath = bgt.Rows[b]["GUIDE_IMGURL"].ToString();
                        try
                        {
                            string[] arr = imgPath.Split(',');
                            for (int i = 0; i < arr.Length; i++)
                            {
                                builder.InsertImage(arr[i], 400, 225);
                            }
                        }
                        catch
                        {
                            continue;
                        }

                        builder.Writeln(" ");
                        if (u != 0 && u / 2 == 0)//两个隐患分一页
                        {
                            builder.InsertBreak(BreakType.PageBreak);
                        }
                    }
                }
            }
            font.Size = 14;
            font.Bold = true;
            builder.Writeln("  【信息动态】");
            if (bgt.Rows.Count != 0) {
                for (int a = 0; a < xxdt.Rows.Count; a++)
                {
                    font.Size = 14;
                    font.Bold = false;
                    int index = a + 1;
                    builder.Writeln("  " + xxdt.Rows[a]["BAK1"] + "");
                    builder.Writeln("       "+xxdt.Rows[a]["GUIDE_CONTENT"] + "");
                }
            }
            builder.InsertBreak(BreakType.PageBreak);//分页插入下一张表

            builder.Writeln("");
            builder.Writeln("");
            builder.Writeln("附件:");
            builder.Writeln("1.事故隐患分类报表");
            builder.Writeln("2.督查(考核)告知书统计表");
            builder.Writeln("3.上月整改情况统计表");
            builder.Writeln("4.存在主要问题");
            
            for (int a = 0; a < 33; a++) {
                builder.Writeln("");
            }
            font.Underline = Underline.Single; //设置下划线
            builder.Writeln("发:公司领导,部,室。");
            builder.Writeln("巨化QHSE中心安全环保督查组             "+DateTime.Now.Year+"年"+DateTime.Now.Year+"月"+DateTime.Now.Day+"日印发");

            builder.InsertBreak(BreakType.PageBreak);//分页插入下一张表
            font.Underline = Underline.None;
            builder.Writeln(" ");
            builder.Writeln("附件1:事故隐患种类分类报表");
            builder.Writeln(" ");

            var table8 = builder.StartTable();

            font.Size = 13;
            font.Bold = false;
            font.Color = System.Drawing.Color.Black;
            font.Name = "FangSong";

            builder.InsertCell();
            builder.Write("被督查单位");
            builder.InsertCell();
            builder.Write("合计");
            builder.InsertCell();
            builder.Write("安全基础管理");
            builder.InsertCell();
            builder.Write("工艺");
            builder.InsertCell();
            builder.Write("设备");
            builder.InsertCell();
            builder.Write("电气仪表");
            builder.InsertCell();
            builder.Write("环保");
            builder.InsertCell();
            builder.Write("消气防");
            builder.InsertCell();
            builder.Write("承包商管理");
            builder.InsertCell();
            builder.Write("现场");
            builder.InsertCell();
            builder.Write("其他");
            builder.EndRow();

            for (int a = 0; a < zyglfl.Rows.Count; a++)
            {
                builder.InsertCell();
                builder.Write(zyglfl.Rows[a]["COMPANY_NAME"].ToString());
                builder.InsertCell();
                builder.Write(zyglfl.Rows[a]["TOTAL"].ToString());
                builder.InsertCell();
                builder.Write(zyglfl.Rows[a]["SAFE"].ToString());
                builder.InsertCell();
                builder.Write(zyglfl.Rows[a]["CRAFT"].ToString());
                builder.InsertCell();
                builder.Write(zyglfl.Rows[a]["DEVICE"].ToString());
                builder.InsertCell();
                builder.Write(zyglfl.Rows[a]["EIE_METER"].ToString());
                builder.InsertCell();
                builder.Write(zyglfl.Rows[a]["ENVIRONMENT"].ToString());
                builder.InsertCell();
                builder.Write(zyglfl.Rows[a]["FIREFIGHT"].ToString());
                builder.InsertCell();
                builder.Write(zyglfl.Rows[a]["CONTRACTOR"].ToString());
                builder.InsertCell();
                builder.Write(zyglfl.Rows[a]["SITE"].ToString());
                builder.InsertCell();
                builder.Write(zyglfl.Rows[a]["QT"].ToString());
                builder.EndRow();
            }
            table8.AutoFit(AutoFitBehavior.AutoFitToWindow); //自适应宽度
            builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; //垂直居中;
            builder.EndTable(); //一个表格画完了把它结束掉

            builder.InsertBreak(BreakType.PageBreak);//分页插入下一张表
         

            var table1 = builder.StartTable();

            font.Size = 13;
            font.Bold = false;
            font.Color = System.Drawing.Color.Black;
            font.Name = "FangSong";
            //插入表头
            builder.InsertCell();
            builder.Write("单位");
            builder.InsertCell();
            builder.Write("合计");
            builder.InsertCell();
            builder.Writeln("防护");
            builder.Writeln("保险");
            builder.Writeln("信号");
            builder.Writeln("装置");
            builder.Writeln("缺乏");
            builder.Writeln("或缺");
            builder.Writeln("陷");
            builder.InsertCell();
            builder.Writeln("设备");
            builder.Writeln("工具");
            builder.Writeln("附件");
            builder.Writeln("有缺");
            builder.Writeln("陷");
            builder.InsertCell();
            builder.Writeln("个体");
            builder.Writeln("防护");
            builder.Writeln("用品");
            builder.Writeln("缺乏");
            builder.Writeln("或有");
            builder.Writeln("缺陷");
            builder.InsertCell();
            builder.Writeln("光线");
            builder.Writeln("不足");
            builder.Writeln("或工");
            builder.Writeln("作地");
            builder.Writeln("点及");
            builder.Writeln("通道");
            builder.Writeln("不良");
            builder.InsertCell();
            builder.Writeln("劳动");
            builder.Writeln("组织");
            builder.Writeln("不合");
            builder.Writeln("理");
            builder.InsertCell();
            builder.Writeln("对现");
            builder.Writeln("场缺");
            builder.Writeln("乏检");
            builder.Writeln("查或");
            builder.Writeln("指导");
            builder.Writeln("有误");
            builder.InsertCell();
            builder.Writeln("设计");
            builder.Writeln("有缺");
            builder.Writeln("陷");
            builder.InsertCell();
            builder.Writeln("不懂");
            builder.Writeln("操作");
            builder.Writeln("技术");
            builder.Writeln("和知");
            builder.Writeln("识");
            builder.InsertCell();
            builder.Writeln("违反");
            builder.Writeln("操作");
            builder.Writeln("规程");
            builder.Writeln("或劳");
            builder.Writeln("动纪");
            builder.Writeln("律");
            builder.InsertCell();
            builder.Writeln("没有");
            builder.Writeln("安全");
            builder.Writeln("操作");
            builder.Writeln("规程");
            builder.Writeln("或制");
            builder.Writeln("度不");
            builder.Writeln("健全");
            builder.InsertCell();
            builder.Write("其他");
            builder.Write("含");
            builder.Write("环保");
            builder.EndRow();

            for (int a = 0; a < yhzl.Rows.Count; a++)
            {
                builder.InsertCell();
                builder.Write(yhzl.Rows[a]["COMPANY_NAME"].ToString());
                builder.InsertCell();
                builder.Write(yhzl.Rows[a]["TOTAL"].ToString());
                builder.InsertCell();
                builder.Write(yhzl.Rows[a]["BHBX"].ToString());
                builder.InsertCell();
                builder.Write(yhzl.Rows[a]["SBGJ"].ToString());
                builder.InsertCell();
                builder.Write(yhzl.Rows[a]["GTFH"].ToString());
                builder.InsertCell();
                builder.Write(yhzl.Rows[a]["GXBZ"].ToString());
                builder.InsertCell();
                builder.Write(yhzl.Rows[a]["LDZZ"].ToString());
                builder.InsertCell();
                builder.Write(yhzl.Rows[a]["XCGZ"].ToString());
                builder.InsertCell();
                builder.Write(yhzl.Rows[a]["SJQX"].ToString());
                builder.InsertCell();
                builder.Write(yhzl.Rows[a]["CZJS"].ToString());
                builder.InsertCell();
                builder.Write(yhzl.Rows[a]["WFCZ"].ToString());
                builder.InsertCell();
                builder.Write(yhzl.Rows[a]["ZDBQ"].ToString());
                builder.InsertCell();
                builder.Write(yhzl.Rows[a]["QT"].ToString());
                builder.EndRow();
            }
            table1.AutoFit(AutoFitBehavior.AutoFitToWindow); //自适应宽度
            builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; //垂直居中;
            builder.EndTable();
            builder.InsertBreak(BreakType.PageBreak);



            builder.Writeln("附件2:督 查(考核)告知书统计表");
            builder.Writeln("");
            var table2 = builder.StartTable();

            font.Size = 13;
            font.Bold = false;
            font.Color = System.Drawing.Color.Black;
            font.Name = "FangSong";

            builder.InsertCell();
            builder.CellFormat.VerticalMerge = CellMerge.First;
            builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; //垂直居中;
            builder.Writeln("《安全环保督查告知书》");
            builder.InsertCell();
            builder.CellFormat.VerticalMerge = CellMerge.None;
            builder.InsertCell();
            builder.CellFormat.VerticalMerge = CellMerge.None;
            builder.InsertCell();
            builder.CellFormat.VerticalMerge = CellMerge.None;
            builder.InsertCell();
            builder.CellFormat.VerticalMerge = CellMerge.None;
            builder.InsertCell();
            builder.CellFormat.VerticalMerge = CellMerge.None;

            builder.InsertCell();
            builder.Writeln("《安全环保督查告知书》");
            builder.InsertCell();
            builder.CellFormat.VerticalMerge = CellMerge.None;
            builder.EndRow();
            //合并单元格设置起始位置和结束位置
            Cell cellStartRange = table2.Rows[0].Cells[0];
            Cell cellEndRange = table2.Rows[0].Cells[5];

            Cell cellStartRange1 = table2.Rows[0].Cells[6];
            Cell cellEndRange1 = table2.Rows[0].Cells[7];
            // Merge all the cells between the two specified cells into one.
            //合并单元格方法
            MergeCells(cellStartRange, cellEndRange);
            MergeCells(cellStartRange1, cellEndRange1);
            builder.InsertCell();
            builder.Write("督查组");
            builder.InsertCell();
            builder.Write("告知书份数");
            builder.InsertCell();
            builder.Write("问题隐患");
            builder.InsertCell();
            builder.Write("完成整改");
            builder.InsertCell();
            builder.Write("未整改");
            builder.InsertCell();
            builder.Write("整改率(%)");
            builder.InsertCell();
            builder.Write("告知书份数");
            builder.InsertCell();
            builder.Write("处罚金额");
            builder.EndRow();

            for (int a = 0; a < ZGKHGZS.Rows.Count; a++)
            {
                builder.InsertCell();
                builder.Write(ZGKHGZS.Rows[a]["DC_NAME"].ToString());
                builder.InsertCell();
                builder.Write(ZGKHGZS.Rows[a]["DC_AQHBGZS"].ToString());
                builder.InsertCell();
                builder.Write(ZGKHGZS.Rows[a]["DC_WTYH"].ToString());
                builder.InsertCell();
                builder.Write(ZGKHGZS.Rows[a]["DC_WCZG"].ToString());
                builder.InsertCell();
                builder.Write(ZGKHGZS.Rows[a]["DC_WZG"].ToString());
                builder.InsertCell();
                builder.Write(ZGKHGZS.Rows[a]["DC_ZGL"].ToString());
                builder.InsertCell();
                builder.Write(ZGKHGZS.Rows[a]["DC_QYAQGZS"].ToString());
                builder.InsertCell();
                builder.Write(ZGKHGZS.Rows[a]["DC_CFJE"].ToString());
                builder.EndRow();
            }
            table2.AutoFit(AutoFitBehavior.AutoFitToWindow); //自适应宽度
            builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; //垂直居中;
            builder.EndTable();
            builder.InsertBreak(BreakType.PageBreak);


            builder.Writeln("附件3:上月整改情况统计表");
            builder.Writeln("");
            var table4 = builder.StartTable();

            font.Size = 13;
            font.Bold = false;
            font.Color = System.Drawing.Color.Black;
            font.Name = "FangSong";

            builder.InsertCell();
            builder.Write("督查组");
            builder.InsertCell();
            builder.Write("问题隐患");
            builder.InsertCell();
            builder.Write("完成整改");
            builder.InsertCell();
            builder.Write("未整改");
            builder.InsertCell();
            builder.Write("整改率(%)");
            builder.EndRow();

            if (syyh == null)
            {


            }
            else {
                for (int a = 0; a < syyh.Rows.Count; a++)
                {
                    builder.InsertCell();
                    builder.Write(syyh.Rows[a]["DC_NAME"].ToString());
                    builder.InsertCell();
                    builder.Write(syyh.Rows[a]["DC_WTYH"].ToString());
                    builder.InsertCell();
                    builder.Write(syyh.Rows[a]["DC_WCZG"].ToString());
                    builder.InsertCell();
                    builder.Write(syyh.Rows[a]["DC_WZG"].ToString());
                    builder.InsertCell();
                    builder.Write(syyh.Rows[a]["DC_ZGL"].ToString());
                    builder.EndRow();
                }
            }

            
            table4.AutoFit(AutoFitBehavior.AutoFitToWindow); //自适应宽度
            builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; //垂直居中;
            builder.EndTable();
            builder.InsertBreak(BreakType.PageBreak);


            builder.Writeln("   附件4:存在主要问题");

            var table3 = builder.StartTable();

            font.Size = 13;
            font.Bold = false;
            font.Color = System.Drawing.Color.Black;
            font.Name = "FangSong";

            builder.InsertCell();
            builder.Write("单位");
            builder.InsertCell();
            builder.Write("存在主要问题");
            builder.EndRow();

            for (int a = 0; a < zyczwt.Rows.Count; a++)
            {
                builder.InsertCell();
                builder.Write(zyczwt.Rows[a]["COMPANY_NAME"].ToString());
                builder.InsertCell();
                builder.Write(zyczwt.Rows[a]["PROBLEM"].ToString());
                builder.EndRow();
            }
            table3.AutoFit(AutoFitBehavior.AutoFitToWindow); //自适应宽度
            builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; //垂直居中;
            builder.EndTable();
            paragraphFormat.Alignment = ParagraphAlignment.Center;

            //Aspose.Words.Drawing.WrapType.Through  穿越型
            //Aspose.Words.Drawing.WrapType.None 于文字上方
            //Aspose.Words.Drawing.WrapType.TopBottom 上下型
            //Aspose.Words.Drawing.WrapType.Tight 紧密型
            //Aspose.Words.Drawing.WrapType.Inline 钳入型
            //Aspose.Words.Drawing.WrapType.Square 四周型


            //Path.Combine("D://ceshiword//", uuid + ".docx")



            doc.Save(WH_PublicFunc.WordExportUrl(uuid));

            //返回文档路径
            return WH_PublicFunc.WordUrl(uuid);
        }
        //public static void DownFile(string filePath, string fileName)
        //{
        //    // filePath 文件路径 例如:/File/记录.xlsx 
        //    // fileName 文件名称 例如:记录.xlsx (要后缀哦)
        //    Encoding encoding; // 申明编码
        //    string outputFileName; // 输出名字
        //    Debug.Assert(HttpContext.ApplicationInstance.Request.UserAgent != null, "HttpContext.ApplicationInstance.Request.UserAgent != null");
        //    string browser = HttpContext.ApplicationInstance.Request.UserAgent.ToUpper();
        //    // 微软的浏览器和ie过滤
        //    if (browser.Contains("MS") && browser.Contains("IE"))
        //    {
        //        outputFileName = HttpUtility.UrlEncode(filePath);
        //        encoding = Encoding.Default;
        //    }
        //    //火狐
        //    else if (browser.Contains("FIREFOX"))
        //    {
        //        outputFileName = fileName;
        //        encoding = Encoding.GetEncoding("GB2312");
        //    }
        //    else
        //    {
        //        outputFileName = HttpUtility.UrlEncode(fileName);
        //        encoding = Encoding.Default;
        //    }
        //    string absoluFilePath = Server.MapPath(filePath); //获取上传文件路径
        //    FileStream fs = new FileStream(absoluFilePath, FileMode.Open);
        //    byte[] bytes = new byte[(int)fs.Length];
        //    fs.Read(bytes, 0, bytes.Length);
        //    fs.Close(); //关闭流,释放资源
        //    HttpContext.ApplicationInstance.Response.Clear();
        //    HttpContext.ApplicationInstance.Response.Buffer = true;
        //    HttpContext.ApplicationInstance.Response.ContentEncoding = encoding;
        //    HttpContext.ApplicationInstance.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", string.IsNullOrEmpty(outputFileName) ? DateTime.Now.ToString("yyyyMMddHHmmssfff") : outputFileName));
        //    Response.BinaryWrite(bytes);
        //    Response.Flush();
        //    HttpContext.ApplicationInstance.Response.End();
        //}
        internal static void MergeCells(Cell startCell, Cell endCell)
        {
            Table parentTable = startCell.ParentRow.ParentTable;

            // Find the row and cell indices for the start and end cell.
            Point startCellPos = new Point(startCell.ParentRow.IndexOf(startCell), parentTable.IndexOf(startCell.ParentRow));
            Point endCellPos = new Point(endCell.ParentRow.IndexOf(endCell), parentTable.IndexOf(endCell.ParentRow));
            // Create the range of cells to be merged based off these indices. Inverse each index if the end cell if before the start cell. 
            Rectangle mergeRange = new Rectangle(System.Math.Min(startCellPos.X, endCellPos.X), System.Math.Min(startCellPos.Y, endCellPos.Y),
                System.Math.Abs(endCellPos.X - startCellPos.X) + 1, System.Math.Abs(endCellPos.Y - startCellPos.Y) + 1);

            foreach (Row row in parentTable.Rows)
            {
                foreach (Cell cell in row.Cells)
                {
                    Point currentPos = new Point(row.IndexOf(cell), parentTable.IndexOf(row));

                    // Check if the current cell is inside our merge range then merge it.
                    if (mergeRange.Contains(currentPos))
                    {
                        if (currentPos.X == mergeRange.X)
                            cell.CellFormat.HorizontalMerge = CellMerge.First;
                        else
                            cell.CellFormat.HorizontalMerge = CellMerge.Previous;

                        if (currentPos.Y == mergeRange.Y)
                            cell.CellFormat.VerticalMerge = CellMerge.First;
                        else
                            cell.CellFormat.VerticalMerge = CellMerge.Previous;
                    }
                }
            }
        }
    }
}

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
还是有很多能改进的地方。

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值