c# - 想要从数据表中的 doc/docx 导入图像,然后使用 C# 在 gridview 中导入图像
问题描述
示例文档我有这样的代码
internal void GetShapesInParagraph(int paragraphIndex)
{
Application word = new Application();
object miss = System.Reflection.Missing.Value;
object path = @"A:\format1.docx";
object readOnly = true;
Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref
miss, ref miss, ref miss, ref miss);
// get the paragraph we want to check
Paragraph para = docs.Paragraphs[paragraphIndex];
// validate the object
if (para != null)
{
// the InlineShapes collection exists in the Range of that paragraph
foreach (InlineShape ils in para.Range.InlineShapes)
{
// validate the object
if (ils != null)
{
// validate this is a picture
if (ils.Type == WdInlineShapeType.wdInlineShapePicture)
{
DataRow dr = dt.NewRow();
// now we have a shape in that paragraph, we can do what we want to it
int ShapeWidth = Convert.ToInt32(ils.Width);
int ShapeHeight = Convert.ToInt32(ils.Height);
dr[0] = ils.PictureFormat;
dt.Rows.Add(dr);
}
}
}
}
}
我可以从 doc/docx 文件中导入文本,但我被困在先将图像导入数据表中,然后再导入到 gridview 中。
解决方案
您需要将图像保存到磁盘,然后将其读回以获取可以存储在数据库中的 base64 字符串。
如果您只处理开放的 xml 文档,请考虑使用Open XML SDK 。在这种情况下,您可以从磁盘读取图像内容并获取可以存储在 Db 中的 base64 字符串,有关详细信息,请参阅使用 CustomXMLPart 中存储的 base64 数据作为 Office 中的图像。