WinForm 的DataGridView中能否将文本和图片在同一列显示
如果在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,