624 lines
18 KiB
C#
624 lines
18 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using CPF.Drawing;
|
|
using CPF.ReoGrid.Drawing;
|
|
using CPF.ReoGrid.IO.OpenXML.Schema;
|
|
using CPF.ReoGrid.Rendering;
|
|
using CPF.ReoGrid.Utility;
|
|
|
|
namespace CPF.ReoGrid.IO.OpenXML
|
|
{
|
|
internal class Document : OpenXMLFile
|
|
{
|
|
public CPF.ReoGrid.IO.OpenXML.Schema.Workbook Workbook { get; private set; }
|
|
|
|
public bool IsReadonly { get; private set; }
|
|
|
|
private Document()
|
|
{
|
|
this.IsReadonly = false;
|
|
}
|
|
|
|
public static Document ReadFromStream(Stream stream)
|
|
{
|
|
IZipArchive zipArchive = MZipArchiveFactory.OpenOnStream(stream);
|
|
bool flag = zipArchive == null;
|
|
Document result;
|
|
if (flag)
|
|
{
|
|
result = null;
|
|
}
|
|
else
|
|
{
|
|
Document document = new Document
|
|
{
|
|
zipArchive = zipArchive,
|
|
_path = string.Empty
|
|
};
|
|
document.LoadRelationShipsFile(document, string.Empty);
|
|
document.Workbook = document.LoadEntryFile<CPF.ReoGrid.IO.OpenXML.Schema.Workbook>("xl/", "workbook.xml");
|
|
result = document;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
internal T LoadEntryFile<T>(string path, string name) where T : class
|
|
{
|
|
T t = this.LoadObjectFromPath<T>(path, name);
|
|
bool flag = t == null;
|
|
if (flag)
|
|
{
|
|
throw new ExcelFormatException("Failed to load specified entry resource: " + name);
|
|
}
|
|
return t;
|
|
}
|
|
|
|
private void LoadRelationShipsFile(OpenXMLFile entryFile, string name)
|
|
{
|
|
string path = entryFile._path + "_rels/" + name + ".rels";
|
|
bool flag = this.zipArchive.IsFileExist(path);
|
|
if (flag)
|
|
{
|
|
entryFile._relationFile = this.LoadObjectFromPath<Relationships>(path, null);
|
|
}
|
|
}
|
|
|
|
internal T LoadRelationResourceById<T>(OpenXMLFile entryFile, string id) where T : class
|
|
{
|
|
Relationship relationship = entryFile._relationFile.relations.FirstOrDefault((Relationship _r) => _r.id == id);
|
|
bool flag = relationship == null;
|
|
if (flag)
|
|
{
|
|
throw new ExcelFormatException("Relation resource cannot be found: " + entryFile._path);
|
|
}
|
|
return this.LoadObjectFromPath<T>(entryFile._path, relationship.target);
|
|
}
|
|
|
|
internal T LoadRelationResourceByType<T>(OpenXMLFile entryFile, string typeNamespace) where T : class
|
|
{
|
|
Relationship relationship = this.Workbook._relationFile.relations.FirstOrDefault((Relationship r) => r.type == typeNamespace);
|
|
return (relationship == null) ? default(T) : this.LoadObjectFromPath<T>(entryFile._path, relationship.target);
|
|
}
|
|
|
|
internal T LoadObjectFromPath<T>(string path, string name) where T : class
|
|
{
|
|
string relativePath = RelativePathUtility.GetRelativePath(path, name);
|
|
IZipEntry file = this.zipArchive.GetFile(relativePath);
|
|
bool flag = file == null;
|
|
if (flag)
|
|
{
|
|
throw new ExcelFormatException("Resource entry cannot be found: " + path);
|
|
}
|
|
T result;
|
|
using (Stream stream = file.GetStream())
|
|
{
|
|
bool flag2 = stream == null;
|
|
if (flag2)
|
|
{
|
|
throw new ExcelFormatException("Resource stream cannot be found: " + path);
|
|
}
|
|
T t = XMLHelper.LoadXML<T>(stream);
|
|
bool flag3 = t is OpenXMLFile;
|
|
if (flag3)
|
|
{
|
|
string pathWithoutFilename = RelativePathUtility.GetPathWithoutFilename(relativePath);
|
|
string fileNameFromPath = RelativePathUtility.GetFileNameFromPath(relativePath);
|
|
OpenXMLFile openXMLFile = t as OpenXMLFile;
|
|
openXMLFile._path = pathWithoutFilename;
|
|
this.LoadRelationShipsFile(openXMLFile, fileNameFromPath);
|
|
}
|
|
result = t;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
internal Stream GetResourceStream(string path)
|
|
{
|
|
IZipEntry file = this.zipArchive.GetFile(path);
|
|
return (file == null) ? null : file.GetStream();
|
|
}
|
|
|
|
public SharedStrings SharedStrings { get; set; }
|
|
|
|
public SharedStrings ReadSharedStringTable()
|
|
{
|
|
bool flag = this.zipArchive.IsFileExist(this.Workbook._path + "sharedStrings.xml");
|
|
SharedStrings result;
|
|
if (flag)
|
|
{
|
|
result = this.LoadEntryFile<SharedStrings>(this.Workbook._path, "sharedStrings.xml");
|
|
}
|
|
else
|
|
{
|
|
result = null;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public Stylesheet Stylesheet { get; set; }
|
|
|
|
public Theme Themesheet
|
|
{
|
|
get
|
|
{
|
|
bool flag = this.themesheet == null && this.zipArchive != null;
|
|
if (flag)
|
|
{
|
|
this.themesheet = this.LoadRelationResourceByType<Theme>(this.Workbook, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme");
|
|
}
|
|
return this.themesheet;
|
|
}
|
|
set
|
|
{
|
|
this.themesheet = value;
|
|
}
|
|
}
|
|
|
|
internal Color ConvertFromCompColor(CompColor compColor, CompColor overrideColor = null)
|
|
{
|
|
bool flag = compColor._solidColor.A > 0;
|
|
Color result;
|
|
if (flag)
|
|
{
|
|
result = compColor._solidColor;
|
|
}
|
|
else
|
|
{
|
|
bool flag2 = compColor.srgbColor != null && !string.IsNullOrEmpty(compColor.srgbColor.val);
|
|
if (flag2)
|
|
{
|
|
int rgb = 0;
|
|
int.TryParse(compColor.srgbColor.val, NumberStyles.AllowHexSpecifier, null, out rgb);
|
|
compColor._solidColor = Color.FromRgb(rgb);
|
|
result = compColor._solidColor;
|
|
}
|
|
else
|
|
{
|
|
bool flag3 = compColor.sysColor != null && !string.IsNullOrEmpty(compColor.sysColor.val);
|
|
if (flag3)
|
|
{
|
|
string val = compColor.sysColor.val;
|
|
string a = val;
|
|
if (a == "windowText")
|
|
{
|
|
compColor._solidColor = StaticResources.SystemColor_WindowText;
|
|
return compColor._solidColor;
|
|
}
|
|
if (a == "window")
|
|
{
|
|
compColor._solidColor = StaticResources.SystemColor_Window;
|
|
return compColor._solidColor;
|
|
}
|
|
}
|
|
bool flag4 = compColor.schemeColor != null;
|
|
if (flag4)
|
|
{
|
|
Color color = Color.Black;
|
|
Theme theme = this.Themesheet;
|
|
string val2 = compColor.schemeColor.val;
|
|
string text = val2;
|
|
uint num = PrivateImplementationDetails.ComputeStringHash(text);
|
|
if (num <= 2873075211U)
|
|
{
|
|
if (num <= 614633880U)
|
|
{
|
|
if (num != 543066875U)
|
|
{
|
|
if (num != 559844494U)
|
|
{
|
|
if (num == 614633880U)
|
|
{
|
|
if (text == "phClr")
|
|
{
|
|
color = this.ConvertFromCompColor(overrideColor, null);
|
|
}
|
|
}
|
|
}
|
|
else if (text == "dk2")
|
|
{
|
|
color = this.ConvertFromCompColor(theme.elements.clrScheme.dk2, null);
|
|
}
|
|
}
|
|
else if (text == "dk1")
|
|
{
|
|
color = this.ConvertFromCompColor(theme.elements.clrScheme.dk1, null);
|
|
}
|
|
}
|
|
else if (num != 2442442836U)
|
|
{
|
|
if (num != 2492775693U)
|
|
{
|
|
if (num == 2873075211U)
|
|
{
|
|
if (text == "hlink")
|
|
{
|
|
color = this.ConvertFromCompColor(theme.elements.clrScheme.hlink, null);
|
|
}
|
|
}
|
|
}
|
|
else if (text == "lt2")
|
|
{
|
|
color = this.ConvertFromCompColor(theme.elements.clrScheme.lt2, null);
|
|
}
|
|
}
|
|
else if (text == "lt1")
|
|
{
|
|
color = this.ConvertFromCompColor(theme.elements.clrScheme.lt1, null);
|
|
}
|
|
}
|
|
else if (num <= 3519584166U)
|
|
{
|
|
if (num != 2891079806U)
|
|
{
|
|
if (num != 3502806547U)
|
|
{
|
|
if (num == 3519584166U)
|
|
{
|
|
if (text == "accent5")
|
|
{
|
|
color = this.ConvertFromCompColor(theme.elements.clrScheme.accent5, null);
|
|
}
|
|
}
|
|
}
|
|
else if (text == "accent6")
|
|
{
|
|
color = this.ConvertFromCompColor(theme.elements.clrScheme.accent6, null);
|
|
}
|
|
}
|
|
else if (text == "folHlink")
|
|
{
|
|
color = this.ConvertFromCompColor(theme.elements.clrScheme.folHlink, null);
|
|
}
|
|
}
|
|
else if (num <= 3553139404U)
|
|
{
|
|
if (num != 3536361785U)
|
|
{
|
|
if (num == 3553139404U)
|
|
{
|
|
if (text == "accent3")
|
|
{
|
|
color = this.ConvertFromCompColor(theme.elements.clrScheme.accent3, null);
|
|
}
|
|
}
|
|
}
|
|
else if (text == "accent4")
|
|
{
|
|
color = this.ConvertFromCompColor(theme.elements.clrScheme.accent4, null);
|
|
}
|
|
}
|
|
else if (num != 3569917023U)
|
|
{
|
|
if (num == 3586694642U)
|
|
{
|
|
if (text == "accent1")
|
|
{
|
|
color = this.ConvertFromCompColor(theme.elements.clrScheme.accent1, null);
|
|
}
|
|
}
|
|
}
|
|
else if (text == "accent2")
|
|
{
|
|
color = this.ConvertFromCompColor(theme.elements.clrScheme.accent2, null);
|
|
}
|
|
bool flag5 = overrideColor != null;
|
|
if (flag5)
|
|
{
|
|
HSLColor hslcolor = ColorUtility.RGBToHSL(color);
|
|
CompColorVar schemeColor = overrideColor.schemeColor;
|
|
bool flag6 = schemeColor.shade != null;
|
|
if (flag6)
|
|
{
|
|
hslcolor.L = ColorUtility.CalculateFinalLumValue(-(float)schemeColor.shade.value / 100000f, hslcolor.L * 255f) / 255f;
|
|
}
|
|
bool flag7 = schemeColor.tint != null;
|
|
if (flag7)
|
|
{
|
|
hslcolor.L = ColorUtility.CalculateFinalLumValue((float)schemeColor.tint.value / 100000f, hslcolor.L * 255f) / 255f;
|
|
}
|
|
bool flag8 = schemeColor.lumMod != null;
|
|
if (flag8)
|
|
{
|
|
hslcolor.L *= (float)schemeColor.lumMod.value / 100000f;
|
|
}
|
|
bool flag9 = schemeColor.lumOff != null;
|
|
if (flag9)
|
|
{
|
|
hslcolor.L += (float)schemeColor.lumOff.value / 100000f;
|
|
}
|
|
bool flag10 = schemeColor.satMod != null;
|
|
if (flag10)
|
|
{
|
|
hslcolor.S *= (float)schemeColor.satMod.value / 100000f;
|
|
}
|
|
color = ColorUtility.HSLToRgb(hslcolor);
|
|
}
|
|
else
|
|
{
|
|
compColor._solidColor = color;
|
|
}
|
|
result = color;
|
|
}
|
|
else
|
|
{
|
|
result = Color.Transparent;
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
internal static Document CreateOnStream(Stream stream)
|
|
{
|
|
Document document = new Document
|
|
{
|
|
zipArchive = MZipArchiveFactory.CreateOnStream(stream),
|
|
_relationFile = new Relationships("_rels/.rels")
|
|
};
|
|
document.contentType = new ContentType
|
|
{
|
|
Defaults = new List<ContentTypeDefaultItem>
|
|
{
|
|
new ContentTypeDefaultItem
|
|
{
|
|
Extension = "rels",
|
|
ContentType = "application/vnd.openxmlformats-package.relationships+xml"
|
|
},
|
|
new ContentTypeDefaultItem
|
|
{
|
|
Extension = "xml",
|
|
ContentType = "application/xml"
|
|
}
|
|
},
|
|
Overrides = new List<ContentTypeOverrideItem>()
|
|
};
|
|
document.CreateWorkbook();
|
|
document.CreateCoreProperties();
|
|
document.CreateAppProperties();
|
|
return document;
|
|
}
|
|
|
|
internal CPF.ReoGrid.IO.OpenXML.Schema.Workbook CreateWorkbook()
|
|
{
|
|
this.Workbook = new CPF.ReoGrid.IO.OpenXML.Schema.Workbook
|
|
{
|
|
_doc = this,
|
|
_xmlTarget = "xl/workbook.xml",
|
|
_relationFile = new Relationships("xl/_rels/workbook.xml.rels"),
|
|
fileVersion = new FileVersion(),
|
|
workbookPr = new WorkbookProperties(),
|
|
bookViews = new BookViews
|
|
{
|
|
workbookView = new WorkbookView
|
|
{
|
|
xWindow = "0",
|
|
yWindow = "0",
|
|
windowWidth = "16384",
|
|
windowHeight = "8192"
|
|
}
|
|
}
|
|
};
|
|
this.Workbook._resId = base.AddRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument", this.Workbook._xmlTarget);
|
|
this.contentType.Overrides.Add(new ContentTypeOverrideItem
|
|
{
|
|
PartName = "/" + this.Workbook._xmlTarget,
|
|
ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"
|
|
});
|
|
return this.Workbook;
|
|
}
|
|
|
|
internal Stylesheet CreateStyles()
|
|
{
|
|
this.Stylesheet = new Stylesheet
|
|
{
|
|
_xmlTarget = "xl/styles.xml",
|
|
borders = new BorderCollection(),
|
|
cellFormats = new CellFormatCollection(),
|
|
colors = new IndexedColors(),
|
|
fills = new FillCollection(),
|
|
fonts = new FontCollection(),
|
|
cellStyles = new CellStyleCollection(),
|
|
cellStyleFormats = new CellFormatCollection()
|
|
};
|
|
this.Workbook.AddRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles", "styles.xml");
|
|
this.contentType.Overrides.Add(new ContentTypeOverrideItem
|
|
{
|
|
PartName = "/" + this.Stylesheet._xmlTarget,
|
|
ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"
|
|
});
|
|
return this.Stylesheet;
|
|
}
|
|
|
|
internal SharedStrings CreateSharedStrings()
|
|
{
|
|
this.SharedStrings = new SharedStrings
|
|
{
|
|
_xmlTarget = "xl/sharedStrings.xml",
|
|
items = new List<SharedStringItem>()
|
|
};
|
|
this.Workbook.AddRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings", "sharedStrings.xml");
|
|
this.contentType.Overrides.Add(new ContentTypeOverrideItem
|
|
{
|
|
PartName = "/" + this.SharedStrings._xmlTarget,
|
|
ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"
|
|
});
|
|
return this.SharedStrings;
|
|
}
|
|
|
|
internal CPF.ReoGrid.IO.OpenXML.Schema.Drawing CreateDrawing(CPF.ReoGrid.IO.OpenXML.Schema.Worksheet sheet)
|
|
{
|
|
string format = "drawing{0}.xml";
|
|
int num = this.drawingFileCount + 1;
|
|
this.drawingFileCount = num;
|
|
string str = string.Format(format, num);
|
|
CPF.ReoGrid.IO.OpenXML.Schema.Drawing drawing = new CPF.ReoGrid.IO.OpenXML.Schema.Drawing
|
|
{
|
|
_xmlTarget = "xl/drawings/" + str,
|
|
_rsTarget = "xl/drawings/_rels/" + str + ".rels",
|
|
_typeObjectCount = new Dictionary<string, int>()
|
|
};
|
|
string id = sheet.AddRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing", "../drawings/" + str);
|
|
this.contentType.Overrides.Add(new ContentTypeOverrideItem
|
|
{
|
|
PartName = "/xl/drawings/" + str,
|
|
ContentType = "application/vnd.openxmlformats-officedocument.drawing+xml"
|
|
});
|
|
sheet.drawing = new CPF.ReoGrid.IO.OpenXML.Schema.SheetDrawing
|
|
{
|
|
id = id,
|
|
_instance = drawing
|
|
};
|
|
return drawing;
|
|
}
|
|
|
|
internal Blip AddMediaImage(Schema.Worksheet sheet, CPF.ReoGrid.IO.OpenXML.Schema.Drawing drawing, ImageObject image)
|
|
{
|
|
string format = "image{0}.png";
|
|
int num = this.mediaImageCount + 1;
|
|
this.mediaImageCount = num;
|
|
string text = string.Format(format, num);
|
|
bool flag = drawing._images == null;
|
|
if (flag)
|
|
{
|
|
drawing._images = new List<Blip>();
|
|
}
|
|
string embedId = drawing.AddRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "../media/" + text);
|
|
Blip blip = new Blip
|
|
{
|
|
_imageObject = image,
|
|
_imageFileName = text,
|
|
embedId = embedId
|
|
};
|
|
drawing._images.Add(blip);
|
|
bool flag2 = !this.contentType.Defaults.Any((ContentTypeDefaultItem ct) => ct.Extension == "png");
|
|
if (flag2)
|
|
{
|
|
this.contentType.Defaults.Add(new ContentTypeDefaultItem
|
|
{
|
|
Extension = "png",
|
|
ContentType = "image/png"
|
|
});
|
|
}
|
|
return blip;
|
|
}
|
|
|
|
internal void CreateCoreProperties()
|
|
{
|
|
this.coreProp = new CoreProperties
|
|
{
|
|
creator = new InnerTextElement(string.Empty),
|
|
lastModifiedBy = new InnerTextElement(string.Empty),
|
|
modified = new OpenXMLDateTime(DateTime.Now),
|
|
created = new OpenXMLDateTime(DateTime.Now),
|
|
_xmlTarget = "docProps/core.xml"
|
|
};
|
|
this.coreProp._resId = base.AddRelationship("http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties", this.coreProp._xmlTarget);
|
|
this.contentType.Overrides.Add(new ContentTypeOverrideItem
|
|
{
|
|
PartName = "/" + this.coreProp._xmlTarget,
|
|
ContentType = "application/vnd.openxmlformats-package.core-properties+xml"
|
|
});
|
|
}
|
|
|
|
internal void CreateAppProperties()
|
|
{
|
|
this.appProp = new AppProperties
|
|
{
|
|
_xmlTarget = "docProps/app.xml"
|
|
};
|
|
this.appProp._resId = base.AddRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties", this.appProp._xmlTarget);
|
|
this.contentType.Overrides.Add(new ContentTypeOverrideItem
|
|
{
|
|
PartName = "/" + this.appProp._xmlTarget,
|
|
ContentType = "application/vnd.openxmlformats-officedocument.extended-properties+xml"
|
|
});
|
|
}
|
|
|
|
private void WriteFile<T>(string path, T obj)
|
|
{
|
|
MemoryStream memoryStream = new MemoryStream();
|
|
XMLHelper.SaveXML<T>(memoryStream, obj);
|
|
memoryStream.Position = 0L;
|
|
IZipEntry zipEntry = this.zipArchive.AddFile(path, memoryStream);
|
|
}
|
|
|
|
private void WriteOpenXMLFile<T>(T obj) where T : OpenXMLFile
|
|
{
|
|
bool flag = obj._relationFile != null;
|
|
if (flag)
|
|
{
|
|
this.WriteFile<Relationships>(obj._relationFile._xmlTarget, obj._relationFile);
|
|
}
|
|
this.WriteFile<T>(obj._xmlTarget, obj);
|
|
}
|
|
|
|
public void Flush()
|
|
{
|
|
this.WriteFile<ContentType>(ContentType._xmlTarget, this.contentType);
|
|
this.WriteFile<Relationships>(this._relationFile._xmlTarget, this._relationFile);
|
|
this.WriteOpenXMLFile<CPF.ReoGrid.IO.OpenXML.Schema.Workbook>(this.Workbook);
|
|
bool flag = this.SharedStrings != null;
|
|
if (flag)
|
|
{
|
|
this.SharedStrings.count = this.SharedStrings.items.Count.ToString();
|
|
this.SharedStrings.uniqueCount = this.SharedStrings.items.Count.ToString();
|
|
this.WriteFile<SharedStrings>(this.SharedStrings._xmlTarget, this.SharedStrings);
|
|
}
|
|
this.WriteFile<Stylesheet>(this.Stylesheet._xmlTarget, this.Stylesheet);
|
|
foreach (WorkbookSheet workbookSheet in this.Workbook.sheets)
|
|
{
|
|
bool flag2 = workbookSheet._instance.drawing != null && workbookSheet._instance.drawing._instance != null;
|
|
if (flag2)
|
|
{
|
|
CPF.ReoGrid.IO.OpenXML.Schema.Drawing instance = workbookSheet._instance.drawing._instance;
|
|
this.WriteOpenXMLFile<CPF.ReoGrid.IO.OpenXML.Schema.Drawing>(instance);
|
|
bool flag3 = instance._images != null;
|
|
if (flag3)
|
|
{
|
|
foreach (Blip blip in instance._images)
|
|
{
|
|
MemoryStream memoryStream = new MemoryStream(4096);
|
|
Image image = blip._imageObject.Image;
|
|
image.SaveToStream(ImageFormat.Png, memoryStream);
|
|
memoryStream.Position = 0L;
|
|
IZipEntry zipEntry = this.zipArchive.AddFile("xl/media/" + blip._imageFileName, memoryStream);
|
|
}
|
|
}
|
|
}
|
|
this.WriteOpenXMLFile<CPF.ReoGrid.IO.OpenXML.Schema.Worksheet>(workbookSheet._instance);
|
|
}
|
|
bool flag4 = this.coreProp != null;
|
|
if (flag4)
|
|
{
|
|
this.WriteFile<CoreProperties>(this.coreProp._xmlTarget, this.coreProp);
|
|
}
|
|
bool flag5 = this.appProp != null;
|
|
if (flag5)
|
|
{
|
|
this.WriteFile<AppProperties>(this.appProp._xmlTarget, this.appProp);
|
|
}
|
|
this.zipArchive.Flush();
|
|
this.zipArchive.Close();
|
|
}
|
|
|
|
private IZipArchive zipArchive;
|
|
|
|
private const string sharedStrings_xml_filename = "sharedStrings.xml";
|
|
|
|
private Theme themesheet;
|
|
|
|
internal ContentType contentType;
|
|
|
|
internal AppProperties appProp;
|
|
|
|
internal CoreProperties coreProp;
|
|
|
|
private int drawingFileCount = 0;
|
|
|
|
private int mediaImageCount = 0;
|
|
}
|
|
}
|