public static void LoadExcelSheet(string name, byte[] resource, DataSet data, bool isXlMinimized) { //for faster processing, do not do screen updates yet Globals.ThisAddIn.Application.ScreenUpdating = false; _fileCounter++; string workbook = Path.Combine( Path.GetTempPath(), string.Format("{0}_{1}.xlsx", name, _fileCounter)); File.WriteAllBytes(workbook, resource); wb = Globals.ThisAddIn.Application.Workbooks.Open( workbook, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); if (isXlMinimized) { Globals.ThisAddIn.Application.WindowState = XlWindowState.xlMinimized; } IEnumerator xmlMaps = wb.XmlMaps.GetEnumerator(); while (xmlMaps.MoveNext()) { XmlMap map = xmlMaps.Current as XmlMap; if (map != null) { map.ImportXml(data.GetXml(), true); break; } } Globals.ThisAddIn.Application.ScreenUpdating = true; }
//extract the xml data file from excel archive file and import it into excel public bool mergeData(Workbook workbook) { String xmlcontent = null; XmlMap impMap = null; foreach (XmlMap xmlmap in workbook.XmlMaps) { if (xmlmap.Name.Equals(GenXsd.PDE_XSD_MAP_NAME)) { impMap = xmlmap; break; } } if (impMap == null)//cannot found import xsd, meaning this has been merged. { isFirstOpen = false; return(true); } foreach (CustomXMLPart curXmlPart in workbook.CustomXMLParts) { if (curXmlPart.BuiltIn) { continue; } XmlObject customObject = ObjectSerializeHelper.Deserialize <XmlObject>(curXmlPart.XML); if (customObject.ContentType == ContentType.PderXml) { xmlcontent = customObject.Content; break; } } if (xmlcontent == null || xmlcontent.Length < 1) { MessageBox.Show("The file format has been broken!"); workbook.Close(); return(false); } impMap.ImportXml(xmlcontent); //delete the xsd for import data impMap.Delete(); workbook.Save(); return(true); }