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>