当前位置:编程学习 > C#/ASP.NET >>

小弟最近用C#做一个WORD文档,先文字,后图片,再表格,但是图片无法显示,求救!!!

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using MSO = Microsoft.Office.Core;

using Word = Microsoft.Office.Interop.Word;

using System.Text.RegularExpressions;

using System.Reflection;


namespace WindowsFormsApplication2

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            Word.Application WordApp = new Word.Application();

            WordApp.Visible = true;

            Word.Document WordDoc = WordApp.Documents.Add();

            object oMissing = System.Reflection.Missing.Value;

           

          

            string strContent;

   

            strContent = "标题\n";

            WordDoc.Paragraphs.Last.Range.Font.Name = "黑体";

            WordDoc.Paragraphs.Last.Range.Font.Size = 16;

            WordDoc.Paragraphs.Last.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;

            WordDoc.Paragraphs.Last.Range.Text = strContent;

           

            //写u入黑体文本

            

            strContent = "正文:在VS2008平台下\n";

            WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;

            WordDoc.Paragraphs.Last.Range.Font.Name ="宋体";

            WordDoc.Paragraphs.Last.Range.Font.Size = 14;

            WordDoc.Paragraphs.Last.Range.ParagraphFormat.FirstLineIndent = 22;

            WordDoc.Paragraphs.Last.Range.Text = strContent;

            

            Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordDoc.Paragraphs.Last.Range, 12, 3, ref oMissing, ref oMissing);

            newTable.Cell(1, 1).Range.Text = "产品名称";

            newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体

            newTable.Cell(1, 2).Range.Text = "产品数量";

            newTable.Cell(1, 2).Range.Bold = 2;//设置单元格中字体为粗体

            WordDoc.Paragraphs.Last.Range.InlineShapes.AddPicture(@"C:\Users\Administrator\Desktop\2.jpg");

            WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 50f;//图片宽度

            WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 50f;//图片高度

 WordDoc.SaveAs2("C:\\1.doc");

            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WordApp);

            WordDoc.Close();

             WordApp.Quit();



        }

        private void Form1_Load(object sender, EventArgs e)        {

        }

    }

} --------------------编程问答-------------------- 不会 顶哈  --------------------编程问答--------------------
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,