Нужна помощь по встраиванию файлов (.pdf, .msg, .docx, .zip и т. Д.) В Excel с использованием openxml от c # .net.У меня есть код для вставки файла Excel в док.но не в состоянии полностью изменить процесс.Пожалуйста, посмотрите код.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EmbedExcelIntoWordDoc
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Embed Excel sheet into Word Document by using Open XML SDK \n");
Console.WriteLine("===========================================================\n");
string appPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string containingDocumentPath = appPath + "\\ContainingDocument.docx";
string embeddedExcelPath = appPath + "\\ExbededExcel.xlsx";
Utility.CreatePackage(containingDocumentPath, embeddedExcelPath);
Console.WriteLine("Excel file '" + embeddedExcelPath + "' embeded into the word document '" + containingDocumentPath + "'");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Вот файл утилиты.
using System;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
using v = DocumentFormat.OpenXml.Vml;
using ovml = DocumentFormat.OpenXml.Vml.Office;
using System.IO;
using System.Drawing;
class Utility
{
public static void CreatePackage(string containingDocumentPath, string embeddedDocumentPath)
{
using (WordprocessingDocument package =
WordprocessingDocument.Create(containingDocumentPath,
WordprocessingDocumentType.Document))
{
AddParts(package, embeddedDocumentPath);
}
}
private static void AddParts(WordprocessingDocument parent,
string embeddedDocumentPath)
{
var mainDocumentPart = parent.AddMainDocumentPart();
GenerateMainDocumentPart().Save(mainDocumentPart);
var embeddedPackagePart =
mainDocumentPart.AddNewPart<EmbeddedPackagePart>(
"application/vnd.openxmlformats-" +
"officedocument.spreadsheetml.sheet",
"rId1");
GenerateEmbeddedPackagePart(embeddedPackagePart,
embeddedDocumentPath);
var imagePart =
mainDocumentPart.AddNewPart<ImagePart>(
"image/x-emf", "rId2");
GenerateImagePart(imagePart);
}
private static Document GenerateMainDocumentPart()
{
var element =
new Document(
new Body(
new Paragraph(
new Run(
new Text(
"This is the containing document."))),
new Paragraph(
new Run(
new Text(
"This is the embedded document: "))),
new Paragraph(
new Run(
new EmbeddedObject(
new v.Shape(
new v.ImageData()
{
Title = "",
RelationshipId = "rId2"
}
)
{
Id = "_x0000_i1025",
Style = "width:76.5pt;height:49.5pt",
},
new ovml.OleObject()
{
Type = ovml.OleValues.Embed,
ProgId = "Excel.Sheet.12",
ShapeId = "_x0000_i1025",
DrawAspect = ovml.OleDrawAspectValues.Icon,
ObjectId = "_1299573545",
Id = "rId1"
}
)
)
)
)
);
return element;
}
public static void GenerateEmbeddedPackagePart(OpenXmlPart part,
string embeddedDocumentPath)
{
byte[] embeddedDocumentBytes;
// The following code will generate an exception if an invalid
// filename is passed.
using (FileStream fsEmbeddedDocument =
File.OpenRead(embeddedDocumentPath))
{
embeddedDocumentBytes =
new byte[fsEmbeddedDocument.Length];
fsEmbeddedDocument.Read(embeddedDocumentBytes, 0,
embeddedDocumentBytes.Length);
}
using (BinaryWriter writer =
new BinaryWriter(part.GetStream()))
{
writer.Write(embeddedDocumentBytes);
writer.Flush();
}
}
public static void GenerateImagePart(OpenXmlPart part)
{
using (BinaryWriter writer = new BinaryWriter(part.GetStream()))
{
writer.Write(System.Convert.FromBase64String(EmbedExcelIntoWordDoc.Properties.Resource1.BASE64_STRING_EXCEL_ICON));
writer.Flush();
}
}
}
в приведенном выше коде мы встраиваем Excel в файл doc.Мне нужно встроить файл (.pdf, .msg, .docx, .zip) в Excel, используя openxml от c # .net
Пожалуйста, проверьте ожидаемый результат.
Вывод
Пожалуйста, помогите.
Спасибо