zoukankan      html  css  js  c++  java
  • C#后台导出word

    在后台上自己定义导出word的格式时遇到的问题进行了一下完整的步骤的整理:

    后台上导出的方法代码:

    /// <summary>
    ///数据以word的形式生成
    /// </summary>
    /// <returns></returns>
    [Security]
    public string ExportWord(string ID)
    {
    try
    {
    //查询要显示的数据
    DataTable dtdata = SqlHlper.ExecuteDt("select * from 表");
    if (dtdata.Rows.Count > 0)
    {
    Object Nothing = System.Reflection.Missing.Value;
    string filepath = "C:/ProjectBuild";
    //判断文件夹是否存在
    if (!Directory.Exists(filepath))
    {
    Directory.CreateDirectory(filepath); //创建文件所在目录
    }
    Object EndOfDoc = "\endofdoc";
    string name = Server.UrlDecode(SafeString.ReplaceStr(文档名)) + DateTime.Now.ToString("yyyyMMddhhmmssfff") + ".doc";//文件名
    object filename = "C://ProjectBuild//" + name; //文件保存路径

    //创建Word文档
    Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
    Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
    WordApp.Selection.PageSetup.LeftMargin = 85f;
    WordApp.Selection.PageSetup.RightMargin = 85f;
    WordApp.Selection.PageSetup.PageWidth = 650f; //页面宽度
    //添加页眉
    WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
    WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距
    //移动焦点并换行
    object count = 14;
    object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
    WordApp.Selection.Text = "表头名";
    WordApp.Selection.ParagraphFormat.LineSpacing = 30f;
    WordApp.Selection.Range.Bold = 2;
    WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
    WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
    WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
    WordApp.Selection.TypeParagraph();//插入段落
    WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
    WordApp.Selection.ParagraphFormat.LineSpacing = 16;
    WordApp.Selection.Font.Size = 10f;
    WordApp.Selection.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorGray40;
    //文档中创建表格
    Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 22, 6, ref Nothing, ref Nothing);
    //设置表格样式
    newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
    newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
    //表格中文字的颜色
    newTable.Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack;
    newTable.Columns[1].Width = 100f;
    newTable.Columns[2].Width = 66f;
    newTable.Columns[3].Width = 100f;
    newTable.Columns[4].Width = 66f;
    newTable.Columns[5].Width = 100f;
    newTable.Columns[6].Width = 66f;

    newTable.Cell(1, 1).Select();
    newTable.Cell(1, 1).Merge(newTable.Cell(1, 6));
    Microsoft.Office.Interop.Word.Table table_name = WordDoc.Tables.Add(WordApp.Selection.Range, 1, 4, ref Nothing, ref Nothing);
    table_name.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
    table_name.Borders.Enable = 0;//默认表格没有边框
    table_name.Cell(1, 1).Range.Text = "固定的文字:";
    table_name.Cell(1, 1).Width = 93;
    table_name.Cell(1, 2).Range.Text = dtdata.Rows[0]["project_name"].ToString();
    table_name.Cell(1, 2).Merge(table_name.Cell(1, 3));
    table_name.Cell(1, 3).Select();
    //判断类型并显示出相应的结果
    string addurl = Server.MapPath("/Images/ProjectBuild/3.png");//本地图片所在路径 默认状态为添加状态
    if (dtdata.Rows[0]["types"].ToString().Contains("1")) //延续状态
    {
    addurl = Server.MapPath("/Images/ProjectBuild/4.png");//本地图片所在路径
    }

    object Link = false;
    object SaveWith = true;
    object Anc = WordDoc.Application.Selection.Range;
    WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(addurl, ref Link, ref SaveWith, ref Anc);
    WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 73f;//图片宽度
    WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 14f;//图片高度

    //纵向合并单元格
    newTable.Cell(2, 1).Select();//选中一行
    object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
    object moveCount = 1;
    object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
    WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
    WordApp.Selection.Cells.Merge();
    //填充表格内容
    newTable.Cell(2, 1).Range.Text = "固定的文字";

    WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;

    newTable.Cell(2, 2).Range.Text = "固定的文字:";
    newTable.Cell(2, 3).Range.Text = dtdata.Rows[0]["zyfzr"].ToString();
    newTable.Cell(2, 4).Range.Text = "固定的文字:";
    newTable.Cell(2, 5).Range.Text = dtdata.Rows[0]["xmfzr"].ToString();
    //合并单元格 第二行的第五列合并到第六列
    newTable.Cell(2, 5).Merge(newTable.Cell(2, 6));

    newTable.Cell(3, 2).Range.Text = "固定的文字:";
    newTable.Cell(3, 3).Range.Text = dtdata.Rows[0]["ryfg"].ToString();
    //合并单元格
    newTable.Cell(3, 3).Merge(newTable.Cell(3, 6));

    newTable.Cell(4, 1).Range.Text = "固定的文字:";
    newTable.Cell(4, 2).Range.Text = dtdata.Rows[0]["venues_name"].ToString();
    //合并单元格
    newTable.Cell(4, 2).Merge(newTable.Cell(4, 6));

    newTable.Cell(5, 1).Range.Text = "固定的文字:";
    newTable.Cell(5, 2).Range.Text = dtdata.Rows[0]["sxszs"].ToString();
    newTable.Cell(5, 3).Range.Text = "固定的文字:";
    newTable.Cell(5, 4).Range.Text = dtdata.Rows[0]["sbzz"].ToString();
    newTable.Cell(5, 5).Range.Text = "固定的文字:";
    newTable.Cell(5, 6).Range.Text = dtdata.Rows[0]["sjz"].ToString();

    newTable.Cell(6, 1).Range.Text = "固定的文字:";
    newTable.Cell(6, 2).Range.Text = dtdata.Rows[0]["ysje"].ToString();
    newTable.Cell(6, 3).Range.Text = "固定的文字:";
    newTable.Cell(6, 4).Range.Text = dtdata.Rows[0]["fzss"].ToString();
    newTable.Cell(6, 5).Range.Text = "固定的文字:";
    newTable.Cell(6, 6).Range.Text = dtdata.Rows[0]["gly"].ToString();

    #region 立项依据的内容
    newTable.Cell(7, 1).Select();//选中行
    newTable.Cell(7, 1).Merge(newTable.Cell(7, 6));

    Microsoft.Office.Interop.Word.Table table_lxyj = WordDoc.Tables.Add(WordApp.Selection.Range, 3, 1, ref Nothing, ref Nothing);
    table_lxyj.Borders.Enable = 0;//默认表格没有边框
    table_lxyj.Cell(1, 1).Range.Text = "一、立项依据:";
    table_lxyj.Cell(1, 1).Range.Bold = 2; //加粗
    //立项依据的内容
    string lxyj = dtdata.Rows[0]["lxyj"].ToString();
    if (!string.IsNullOrEmpty(lxyj))
    {
    //去除掉所有的html标签
    table_lxyj.Cell(2, 1).Range.Text = SafeString.content(lxyj);
    table_lxyj.Cell(3, 1).Select();//选中行
    //图片的地址
    string[] imglist = SafeString.GetHtmlImageUrlList(lxyj);
    foreach (var url in imglist)
    {
    string FileName = "";//本地图片所在路径
    //网页上的地址
    if (url.Contains("http"))
    {
    FileName = url;
    }
    else
    {
    FileName = Server.MapPath(url);//本地图片所在路径
    }
    object LinkToFile = false;
    object SaveWithDocument = true;
    object Anchor = WordDoc.Application.Selection.Range;
    //在显示图片时先判断图片是否存在
    if (!System.IO.File.Exists(FileName) && !FileName.Contains("http"))
    {
    }
    else
    {
    WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
    WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
    //将图片设置为四周环绕型
    Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
    s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
    }
    }
    }
    else
    {
    table_lxyj.Rows.Height = 30f;//表格的最低高度
    }
    #endregion

    #region 项目建设目标内容
    newTable.Cell(8, 1).Select();//选中行
    newTable.Cell(8, 1).Merge(newTable.Cell(8, 6));

    Microsoft.Office.Interop.Word.Table table_jsmb = WordDoc.Tables.Add(WordApp.Selection.Range, 3, 1, ref Nothing, ref Nothing);
    table_jsmb.Borders.Enable = 0;//默认表格没有边框
    table_jsmb.Cell(1, 1).Range.Text = "固定的文字";
    table_jsmb.Cell(1, 1).Range.Bold = 2; //加粗
    //建设目标
    string jsmb = dtdata.Rows[0]["jsmb"].ToString();
    if (!string.IsNullOrEmpty(jsmb))
    {
    table_jsmb.Cell(2, 1).Range.Text = SafeString.content(jsmb);
    table_jsmb.Cell(3, 1).Select();//选中行
    //图片的地址
    string[] imglist = SafeString.GetHtmlImageUrlList(jsmb);
    foreach (var url in imglist)
    {
    string FileName = "";//本地图片所在路径
    //网页上的地址
    if (url.Contains("http"))
    {
    FileName = url;
    }
    else
    {
    FileName = Server.MapPath(url);//本地图片所在路径
    }
    object LinkToFile = false;
    object SaveWithDocument = true;
    object Anchor = WordDoc.Application.Selection.Range;
    //在显示图片时先判断图片是否存在
    if (!System.IO.File.Exists(FileName) && !FileName.Contains("http"))
    {
    }
    else
    {
    WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
    WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
    //将图片设置为四周环绕型
    Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
    s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
    }
    }
    }
    else
    {
    table_jsmb.Rows.Height = 40f;//表格的最低高度
    }
    #endregion

    #region 项目主要内容
    newTable.Cell(9, 1).Select();//选中行
    newTable.Cell(9, 1).Merge(newTable.Cell(9, 6));

    Microsoft.Office.Interop.Word.Table table_zynr = WordDoc.Tables.Add(WordApp.Selection.Range, 3, 1, ref Nothing, ref Nothing);
    table_zynr.Borders.Enable = 0;//默认表格没有边框
    table_zynr.Cell(1, 1).Range.Text = "固定的文字:";
    table_zynr.Cell(1, 1).Range.Bold = 2;//字体加粗
    string zynr = dtdata.Rows[0]["zynr"].ToString();
    if (!string.IsNullOrEmpty(zynr))
    {
    table_zynr.Cell(2, 1).Range.Text = SafeString.content(zynr);
    table_zynr.Cell(3, 1).Select();//选中行
    //图片的地址
    string[] imglist = SafeString.GetHtmlImageUrlList(zynr);
    foreach (var url in imglist)
    {
    string FileName = "";//本地图片所在路径
    //网页上的地址
    if (url.Contains("http"))
    {
    FileName = url;
    }
    else
    {
    FileName = Server.MapPath(url);//本地图片所在路径
    }

    object LinkToFile = false;
    object SaveWithDocument = true;
    object Anchor = WordDoc.Application.Selection.Range;
    //在显示图片时先判断图片是否存在
    if (!System.IO.File.Exists(FileName) && !FileName.Contains("http"))
    {
    }
    else
    {
    WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
    WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
    //将图片设置为四周环绕型
    Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
    s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
    }
    }
    }
    else
    {
    table_zynr.Rows.Height = 93f;//表格的最低高度
    }
    #endregion

    newTable.Cell(10, 1).Range.Text = "固定的文字";
    newTable.Cell(10, 1).Merge(newTable.Cell(10, 6));
    newTable.Cell(10, 1).Range.Bold = 2;//设置单元格中字体为粗体

    newTable.Cell(11, 1).Range.Text = "固定的文字";
    newTable.Cell(11, 2).Range.Text = dtdata.Rows[0]["coursemanager_name"].ToString();
    //合并单元格
    newTable.Cell(11, 2).Merge(newTable.Cell(11, 6));

    newTable.Cell(12, 1).Range.Text = "固定的文字";
    newTable.Cell(12, 2).Range.Text = dtdata.Rows[0]["item_name"].ToString();
    //合并单元格
    newTable.Cell(12, 2).Merge(newTable.Cell(12, 6));

    newTable.Cell(13, 1).Range.Text = "固定的文字";
    newTable.Cell(13, 2).Range.Text = dtdata.Rows[0]["major_name"].ToString();
    //合并单元格
    newTable.Cell(13, 2).Merge(newTable.Cell(13, 6));

    newTable.Cell(14, 1).Range.Text = "固定的文字";
    newTable.Cell(14, 2).Range.Text = dtdata.Rows[0]["zyjn"].ToString();
    //合并单元格
    newTable.Cell(14, 2).Merge(newTable.Cell(14, 6));

    newTable.Cell(15, 1).Range.Text = "固定的文字";
    newTable.Cell(15, 2).Range.Text = dtdata.Rows[0]["keshi"].ToString() + "," + dtdata.Rows[0]["renshu"].ToString() + "," + dtdata.Rows[0]["xueshi"].ToString() + "";
    //合并单元格
    newTable.Cell(15, 2).Merge(newTable.Cell(15, 6));

    #region 项目建设进度计划及经费安排
    newTable.Cell(16, 1).Select();//选中行
    newTable.Cell(16, 1).Merge(newTable.Cell(16, 6));

    Microsoft.Office.Interop.Word.Table table_jdjh = WordDoc.Tables.Add(WordApp.Selection.Range, 3, 1, ref Nothing, ref Nothing);
    table_jdjh.Borders.Enable = 0;//默认表格没有边框
    table_jdjh.Cell(1, 1).Range.Text = "五、项目建设进度计划及经费安排(包括分年度建设及相应配套经费):";
    table_jdjh.Cell(1, 1).Range.Bold = 2;
    string jdjh = dtdata.Rows[0]["jdjh"].ToString();
    if (!string.IsNullOrEmpty(jdjh))
    {
    table_jdjh.Cell(2, 1).Range.Text = SafeString.content(jdjh);
    table_jdjh.Cell(3, 1).Select();//选中行
    //图片的地址
    string[] imglist = SafeString.GetHtmlImageUrlList(jdjh);
    foreach (var url in imglist)
    {
    string FileName = "";//本地图片所在路径
    //网页上的地址
    if (url.Contains("http"))
    {
    FileName = url;
    }
    else
    {
    FileName = Server.MapPath(url);//本地图片所在路径
    }
    object LinkToFile = false;
    object SaveWithDocument = true;
    object Anchor = WordDoc.Application.Selection.Range;
    //在显示图片时先判断图片是否存在
    if (!System.IO.File.Exists(FileName) && !FileName.Contains("http"))
    {
    }
    else
    {
    WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
    WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
    //将图片设置为四周环绕型
    Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
    s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
    }
    }
    }
    else
    {
    table_jdjh.Rows.Height = 50f;//表格的最低高度
    }
    #endregion

    #region 大型仪器
    newTable.Cell(17, 1).Select();//选中行
    newTable.Cell(17, 1).Merge(newTable.Cell(17, 6));

    Microsoft.Office.Interop.Word.Table table_sbfx = WordDoc.Tables.Add(WordApp.Selection.Range, 3, 1, ref Nothing, ref Nothing);
    table_sbfx.Borders.Enable = 0;//默认表格没有边框
    table_sbfx.Cell(1, 1).Range.Text = "固定的文字:";
    table_sbfx.Cell(1, 1).Range.Bold = 2;
    string sbfx = dtdata.Rows[0]["sbfx"].ToString();
    if (!string.IsNullOrEmpty(sbfx))
    {
    table_sbfx.Cell(2, 1).Range.Text = SafeString.content(sbfx);
    table_sbfx.Cell(3, 1).Select();//选中行
    //图片的地址
    string[] imglist = SafeString.GetHtmlImageUrlList(sbfx);
    foreach (var url in imglist)
    {
    string FileName = "";//本地图片所在路径
    //网页上的地址
    if (url.Contains("http"))
    {
    FileName = url;
    }
    else
    {
    FileName = Server.MapPath(url);//本地图片所在路径
    }
    object LinkToFile = false;
    object SaveWithDocument = true;
    object Anchor = WordDoc.Application.Selection.Range;
    //在显示图片时先判断图片是否存在
    if (!System.IO.File.Exists(FileName) && !FileName.Contains("http"))
    {
    }
    else
    {
    WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
    WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
    //将图片设置为四周环绕型
    Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
    s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
    }
    }
    }
    else
    {
    table_sbfx.Rows.Height = 50f;//表格的最低高度
    }
    #endregion

    #region 设备清单
    newTable.Cell(18, 1).Select();//选中行
    newTable.Cell(18, 1).Merge(newTable.Cell(18, 6));

    Microsoft.Office.Interop.Word.Table table_sbqd = WordDoc.Tables.Add(WordApp.Selection.Range, 4, 1, ref Nothing, ref Nothing);
    table_sbqd.Borders.Enable = 0;//默认表格没有边框
    table_sbqd.Cell(1, 1).Range.Text = "固定的文字:";
    table_sbqd.Cell(1, 1).Range.Bold = 2;
    table_sbqd.Cell(2, 1).Range.Text = "固定的文字";
    string sbqd = dtdata.Rows[0]["sbqd"].ToString();
    if (!string.IsNullOrEmpty(sbqd))
    {
    table_sbqd.Cell(3, 1).Range.Text = SafeString.content(sbqd);
    table_sbqd.Cell(4, 1).Select();//选中行
    //图片的地址
    string[] imglist = SafeString.GetHtmlImageUrlList(sbqd);
    foreach (var url in imglist)
    {
    string FileName = "";//本地图片所在路径
    //网页上的地址
    if (url.Contains("http"))
    {
    FileName = url;
    }
    else
    {
    FileName = Server.MapPath(url);//本地图片所在路径
    }
    object LinkToFile = false;
    object SaveWithDocument = true;
    object Anchor = WordDoc.Application.Selection.Range;
    //在显示图片时先判断图片是否存在
    if (!System.IO.File.Exists(FileName) && !FileName.Contains("http"))
    {
    }
    else
    {
    WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
    WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
    //将图片设置为四周环绕型
    Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
    s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
    }
    }
    }
    else
    {
    table_sbqd.Rows[3].Height = 190f;//表格的最低高度
    }
    #endregion

    newTable.Cell(19, 1).Select();//选中行
    newTable.Cell(19, 1).Merge(newTable.Cell(19, 6));
    Microsoft.Office.Interop.Word.Table dep = WordDoc.Tables.Add(WordApp.Selection.Range, 3, 3, ref Nothing, ref Nothing);
    dep.Borders.Enable = 0;//默认表格没有边框
    dep.Cell(1, 1).Range.Text = "固定的文字";
    dep.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
    dep.Rows[2].Height = 110f;
    dep.Cell(2, 1).Merge(dep.Cell(2, 3));
    dep.Cell(3, 2).Range.Text = " 固定的文字";
    dep.Cell(3, 3).Range.Text = " 固定的文字";

    newTable.Cell(20, 1).Select();//选中行
    newTable.Cell(20, 1).Merge(newTable.Cell(20, 6));
    Microsoft.Office.Interop.Word.Table zhuanjia = WordDoc.Tables.Add(WordApp.Selection.Range, 3, 3, ref Nothing, ref Nothing);
    zhuanjia.Borders.Enable = 0;//默认表格没有边框
    zhuanjia.Cell(1, 1).Range.Text = "固定的文字:";
    zhuanjia.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
    zhuanjia.Rows[2].Height = 110f;
    zhuanjia.Cell(2, 1).Merge(zhuanjia.Cell(2, 3));
    zhuanjia.Cell(3, 3).Range.Text = " 固定的文字";

    newTable.Cell(21, 1).Select();//选中行
    newTable.Cell(21, 1).Merge(newTable.Cell(21, 6));
    Microsoft.Office.Interop.Word.Table shixun = WordDoc.Tables.Add(WordApp.Selection.Range, 3, 3, ref Nothing, ref Nothing);
    shixun.Borders.Enable = 0;//默认表格没有边框
    shixun.Cell(1, 1).Range.Text = "固定的文字:";
    shixun.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
    shixun.Rows[2].Height = 110f;
    shixun.Cell(2, 1).Merge(shixun.Cell(2, 3));
    shixun.Cell(3, 3).Range.Text = " 年 月 日";

    newTable.Cell(22, 1).Select();//选中行
    newTable.Cell(22, 1).Merge(newTable.Cell(22, 6));
    Microsoft.Office.Interop.Word.Table school = WordDoc.Tables.Add(WordApp.Selection.Range, 3, 3, ref Nothing, ref Nothing);
    school.Borders.Enable = 0;//默认表格没有边框
    school.Cell(1, 1).Range.Text = "固定的文字:";
    school.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
    school.Rows[2].Height = 110f;
    school.Cell(2, 1).Merge(school.Cell(2, 3));
    school.Cell(3, 3).Range.Text = " 年 月 日";

    //文件保存
    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);
    WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
    WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);

    //浏览器上下载文档
    DownLoadFile(filename.ToString());
    }
    return "";
    }
    catch (Exception ex)
    {
    return ex.Message;
    }

    }

    //下载生成后的文档
    public void DownLoadFile(string Url)
    {
    string filePath = Url;
    FileInfo file = new FileInfo(filePath);
    if (file.Exists)
    {
    FileStream fs = new FileStream(filePath, FileMode.Open);
    byte[] bytes = new byte[(int)fs.Length];
    fs.Read(bytes, 0, bytes.Length);
    fs.Close();
    Response.Charset = "UTF-8";
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
    Response.ContentType = "application/octet-stream";
    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(Path.GetFileName(filePath)));
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
    }
    }

    在服务器上部署,若无法导出word时就需查找一下一个问题:

    服务器上需安装office。安装完成需要调用office的组件。

    运行dcomcnfg.exe打开组件服务;

      2、依次展开"组件服务"->"计算机"->"我的电脑"->"DCOM配置"->"Microsoft Word 文档"

        

      3、右键打开“属性”对话框,点击“标识”选项卡,选择交互式用户;点击“安全”选项卡,“启动和激活权限”、“访问权限”选择自定义

        

      4、编辑启动和激活权限权限的自定义选项,将NETWORK SERVICE(也有说法需要IIS_IUSERS和everyone用户)添加进去,给与本地激活远程激活的权限,访问权限相同

        

      5、或许设置完以上代码就可以正常运行,但是我的代码还是抛出错误,这里再配置web.config的<identity impersonate="true" userName="用户名" password="密码"/>,将其放到<system.web>节点下面即可,代码就可以正常运行了。

    若无法找到组件时先看看系统是32位还是64位。经研究得此结论

    1. 这个主要是64位系统的问题,excel是32位的组件,所以在正常的系统组件服务里是看不到的 可以通过在运行里面输入 comexp.msc -32 来打开32位的组件服务,在里就能看到excel组件了

      安装完office后 在组件服务里DCOM配置中找不到
    2. 2
      安装完office后 在组件服务里DCOM配置中找不到
      若在配置完成以上操作之后还是出现远程断开访问报错的情况则将标识选项卡下改为下列用户,输入服务器管理员帐密码
  • 相关阅读:
    2、函数
    二者取其一(初遇)_网络流
    P1879 [USACO06NOV]玉米田Corn Fields
    P2831 愤怒的小鸟
    P2296 寻找道路
    序(不知道是什么时候的模拟题)
    P2243 电路维修
    P1273 有线电视网
    P2613 【模板】有理数取余
    P1373 小a和uim之大逃离
  • 原文地址:https://www.cnblogs.com/luckys/p/12674274.html
Copyright © 2011-2022 走看看