Reputation: 2906
I'm trying to create a 2007+ Excel document (.xlsx), using nothing but .net 4.0 built-in classes. So I can't use any third party libraries.
Actually it's done for the most part, the only problem I'm facing now is that, the package I'm creating seems to fail on the creation of the [Content_Types].xml file, I mean it lacks all the <Override> elements, and only the <Default> elements are created.
I'm pretty sure the problem is at the point where I create the relationships between the packages, but I just don't know how to make it work, and documentation and examples on Package and ZipPackage classes are surprisingly scarce.
Maybe I'm missing another step...does some one has any clue? please
This is a similar code which produces excactly the same problem
public void CreatePackage()
{
string Dir = @"F:\Proyectos\Excel_StackOverflow\WindowsFormsApplication1\WindowsFormsApplication1\Resources";
Uri File1_abs = new Uri(String.Format(@"{0}\1.xml", Dir), UriKind.Absolute);
Uri File1_rel = new Uri(@"/OddContent/File1.xml", UriKind.Relative);
using (ZipPackage exPkg = (ZipPackage)Package.Open(String.Format(@"{0}\Temp.zip", Dir), FileMode.Create))
{
ZipPackagePart File1Part = (ZipPackagePart)exPkg.CreatePart(File1_rel, System.Net.Mime.MediaTypeNames.Text.Xml);
using (FileStream fs1 = new FileStream(File1_abs.LocalPath, FileMode.Open, FileAccess.Read))
{
Form1.CopyStream(fs1, File1Part.GetStream());
}
exPkg.CreateRelationship(File1_rel, TargetMode.Internal, "SomeType");
exPkg.Flush();
exPkg.Close();
}
}
private static void CopyStream(Stream source, Stream target)
{
const int bufSize = 0x1000;
byte[] buf = new byte[bufSize];
int bytesRead = 0;
while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
target.Write(buf, 0, bytesRead);
}
and the [Content_Types].xml file it generates for the given code is:
<?xml version="1.0" encoding="UTF-8"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default ContentType="text/xml" Extension="xml"/>
</Types>
But actually I'm expecting something like this to be the [Content_Types].xml file content:
<?xml version="1.0" encoding="UTF-8"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default ContentType="text/xml" Extension="xml"/>
<Override PartName="...................."/>
</Types>
Upvotes: 1
Views: 2120
Reputation: 1411
The "Override" element will be put into the types list, once there is a part that is stored with the default extension but a different Content Type.
Here's an updated version that will create the "Override" element for the File1Part:
Uri File1_rel = new Uri(@"/OddContent/File1.xml", UriKind.Relative);
Uri File2_rel = new Uri(@"/OddContent/File2.xml", UriKind.Relative);
using (ZipPackage exPkg = (ZipPackage)Package.Open(String.Format(@"{0}\Temp.zip", Dir), FileMode.Create))
{
ZipPackagePart p2 = (ZipPackagePart)exPkg.CreatePart(File2_rel, System.Net.Mime.MediaTypeNames.Text.Xml);
ZipPackagePart File1Part = (ZipPackagePart)exPkg.CreatePart(File1_rel, "application/vnd.openxmlformats-officedocument.wordprocessingml.documents.main+xml");
NOTE: I type-copied the mime type for the vnd... from an image, it might have typos, feel free to fix.
The created [Content_Types].xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default Extension="xml" ContentType="text/xml" />
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" />
<Override PartName="/OddContent/File1.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.documents.main+xml" />
</Types>
Upvotes: 2