如果在word中的同一行中既有图片又有文字,提取出来后显示在DataGridView的一列中,下面代码是显示在不同列。
            DataTable dt = new DataTable();
            dt.Columns.Add("id", typeof(System.Int32));
            dt.Columns.Add("text", typeof(System.String));
            dt.Columns.Add("img", typeof(System.Drawing.Bitmap));            IWord word = new Word2003Factory().CreateWord();            Word.Document doc = word.OpenDocument(fileName);
            StringBuilder sb = new StringBuilder();
            object objLine;
            IList<int> picLine  = new List<int>();            foreach (Word.InlineShape shape in doc.InlineShapes)
            {
                if (shape.Type == Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapePicture)
                {
                    objLine = shape.Range.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdFirstCharacterLineNumber);
                    picLine.Add(Convert.ToInt32(objLine));
                }
            }            for(int i =1 ;i<doc.Paragraphs.Count;i++)
            {
                DataRow dr = dt.NewRow();
                int count = doc.Paragraphs[i].Range.Characters.Count;
                sb = new StringBuilder();
                if (picLine.Contains(i))
                {
                    for (int j = 1; j < count; j++)
                    {
                        doc.Paragraphs[i].Range.Characters[j].Select();
                        if (doc.Application.Selection.Type == Microsoft.Office.Interop.Word.WdSelectionType.wdSelectionInlineShape)
                        {
                            doc.Application.Selection.Copy();
                            Image img = Clipboard.GetImage();
                            Bitmap pic = new Bitmap(img);
                            dr["img"] = pic;
                        }
                        else if (doc.Application.Selection.Type == Microsoft.Office.Interop.Word.WdSelectionType.wdSelectionNormal)
                        {
                            sb.Append(doc.Application.Selection.Text);
                        }
                    }
                }
                else
                {
                    doc.Paragraphs[i].Range.Select();
                    if (doc.Application.Selection.Type == Microsoft.Office.Interop.Word.WdSelectionType.wdSelectionNormal)
                    {
                        sb.Append(doc.Application.Selection.Text);
                    }
                }        
                
                dr["id"] = i;
                dr["text"] = sb.ToString();
                dt.Rows.Add(dr);
            }            dataGridView1.DataSource = dt;
            /*
            string value = string.Empty;            object docProperty = doc.BuiltInDocumentProperties;
            Type docPropertyType = docProperty.GetType();            object item = docPropertyType.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, docProperty, new object[] { "Number of pages" });
            Type itemTyp = item.GetType();
            object itemValue = itemTyp.InvokeMember("Value", BindingFlags.Default | BindingFlags.GetProperty, null, item, new object[] { });
            return itemValue.ToString();
             */

解决方案 »

  1.   

    找到个,未测试效果
    public class TextAndImageColumn:DataGridViewTextBoxColumn{private Image imageValue;private Size imageSize; public TextAndImageColumn(){this.CellTemplate = new TextAndImageCell();} public override object Clone(){TextAndImageColumn c = base.Clone() as TextAndImageColumn;c.imageValue = this.imageValue;c.imageSize = this.imageSize;return c;} public Image Image{get { return this.imageValue; }set{if (this.Image != value) {this.imageValue = value;this.imageSize = value.Size; if (this.InheritedStyle != null) {Padding inheritedPadding = this.InheritedStyle.Padding;this.DefaultCellStyle.Padding = new Padding(imageSize.Width,
    inheritedPadding.Top, inheritedPadding.Right, 
    inheritedPadding.Bottom);}}}}private TextAndImageCell TextAndImageCellTemplate{get { return this.CellTemplate as TextAndImageCell; }}internal Size ImageSize{get { return imageSize; }}} public class TextAndImageCell : DataGridViewTextBoxCell{private Image imageValue;private Size imageSize;
    public override object Clone(){TextAndImageCell c = base.Clone() as TextAndImageCell;c.imageValue= this.imageValue;c.imageSize = this.imageSize;return c;} public Image Image{get {if (this.OwningColumn == null || 
    this.OwningTextAndImageColumn == null) { return imageValue;}else if (this.imageValue != null) {return this.imageValue;}else {return this.OwningTextAndImageColumn.Image;}}set {if (this.imageValue != value) {this.imageValue = value;this.imageSize = value.Size; Padding inheritedPadding = this.InheritedStyle.Padding;this.Style.Padding = new Padding(imageSize.Width, 
    inheritedPadding.Top, inheritedPadding.Right, 
    inheritedPadding.Bottom);}}}protected override void Paint(Graphics graphics, Rectangle clipBounds,
    Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState,
    object value, object formattedValue, string errorText, 
    DataGridViewCellStyle cellStyle, 
    DataGridViewAdvancedBorderStyle advancedBorderStyle,
    DataGridViewPaintParts paintParts){// Paint the base contentbase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, 
    value, formattedValue, errorText, cellStyle,
    advancedBorderStyle, paintParts); if (this.Image != null) {// Draw the image clipped to the cell.System.Drawing.Drawing2D.GraphicsContainer container = 
    graphics.BeginContainer();
    graphics.SetClip(cellBounds);graphics.DrawImageUnscaled(this.Image, cellBounds.Location); graphics.EndContainer(container);}} private TextAndImageColumn OwningTextAndImageColumn{get { return this.OwningColumn as TextAndImageColumn; }}}