public ExcelOpenXmlSheetWriter(Stream stream, object value, string sheetName, IConfiguration configuration, bool printHeader)
 {
     this._stream        = stream;
     this._archive       = new MiniExcelZipArchive(_stream, ZipArchiveMode.Create, true, _utf8WithBom);
     this._configuration = configuration as OpenXmlConfiguration ?? OpenXmlConfiguration.DefaultConfig;
     this._printHeader   = printHeader;
     this._value         = value;
     _sheets.Add(new SheetDto {
         Name = sheetName, SheetIdx = 1
     });                                                           //TODO:remove
 }
 public void SaveAs(object value, string sheetName, bool printHeader, IConfiguration configuration)
 {
     OpenXmlConfiguration config = configuration as OpenXmlConfiguration ?? OpenXmlConfiguration.DefaultConfig;
     using (var archive = new MiniExcelZipArchive(_stream, ZipArchiveMode.Create, true, _utf8WithBom))
     {
         if (value is IDictionary<string, object>)
         {
             var sheetId = 0;
             var sheets = value as IDictionary<string, object>;
             var packages = DefualtOpenXml.GenerateDefaultOpenXml(archive, sheets.Keys, config);
             foreach (var sheet in sheets)
             {
                 sheetId++;
                 var sheetPath = $"xl/worksheets/sheet{sheetId}.xml";
                 CreateSheetXml(sheet.Value, printHeader, archive, packages, sheetPath);
             }
             GenerateContentTypesXml(archive, packages);
         }
         else if (value is DataSet)
         {
             var sheetId = 0;
             var sheets = value as DataSet;
             var keys = new List<string>();
             foreach (DataTable dt in sheets.Tables)
             {
                 keys.Add(dt.TableName);
             }
             var packages = DefualtOpenXml.GenerateDefaultOpenXml(archive, keys, config);
             foreach (DataTable dt in sheets.Tables)
             {
                 sheetId++;
                 var sheetPath = $"xl/worksheets/sheet{sheetId}.xml";
                 CreateSheetXml(dt, printHeader, archive, packages, sheetPath);
             }
             GenerateContentTypesXml(archive, packages);
         }
         else
         {
             var packages = DefualtOpenXml.GenerateDefaultOpenXml(archive, new[] { sheetName }, config);
             var sheetPath = "xl/worksheets/sheet1.xml";
             CreateSheetXml(value, printHeader, archive, packages, sheetPath);
             GenerateContentTypesXml(archive, packages);
         }
     }
 }
Example #3
0
        //TODO:read from static generated file looks like more better?
        internal static Dictionary <string, ZipPackageInfo> GenerateDefaultOpenXml(ZipArchive archive, IEnumerable <string> sheetNames, OpenXmlConfiguration configuration)
        {
            var defaults = new Dictionary <string, Tuple <string, string> >()
            {
                { @"_rels/.rels", new Tuple <string, string>(DefualtOpenXml.DefaultRels, "application/vnd.openxmlformats-package.relationships+xml") },
            };

            // styles.xml
            {
                var styleXml = string.Empty;

                if (configuration.TableStyles == TableStyles.None)
                {
                    styleXml = NoneStylesXml;
                }
                else if (configuration.TableStyles == TableStyles.Default)
                {
                    styleXml = DefaultStylesXml;
                }

                defaults.Add(@"xl/styles.xml", new Tuple <string, string>(styleXml, "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"));
            }

            // workbook.xml 、 workbookRelsXml
            {
                var workbookXml     = new StringBuilder();
                var workbookRelsXml = new StringBuilder();

                var sheetId = 0;
                foreach (var sheetName in sheetNames)
                {
                    sheetId++;
                    var id = $"R{Guid.NewGuid().ToString("N")}";
                    workbookXml.AppendLine($@"<x:sheet xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships"" name=""{sheetName}"" sheetId=""{sheetId}"" r:id=""{id}"" />");
                    workbookRelsXml.AppendLine($@"<Relationship Type=""http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"" Target=""/xl/worksheets/sheet{sheetId}.xml"" Id=""{id}"" />");
                }
                defaults.Add(@"xl/workbook.xml", new Tuple <string, string>(
                                 DefualtOpenXml.DefaultWorkbookXml.Replace("{{sheets}}", workbookXml.ToString())
                                 , "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml")
                             );
                defaults.Add(@"xl/_rels/workbook.xml.rels", new Tuple <string, string>(
                                 DefualtOpenXml.DefaultWorkbookXmlRels.Replace("{{sheets}}", workbookRelsXml.ToString())
                                 , "application/vnd.openxmlformats-package.relationships+xml")
                             );
            }

            var zps = new Dictionary <string, ZipPackageInfo>();

            foreach (var p in defaults)
            {
                ZipArchiveEntry entry = archive.CreateEntry(p.Key);
                using (var zipStream = entry.Open())
                    using (StreamWriter writer = new StreamWriter(zipStream, Utf8WithBom))
                        writer.Write(p.Value.Item1.ToString());

                zps.Add(p.Key, new ZipPackageInfo(entry, p.Value.Item2));
            }
            return(zps);
        }
Example #4
0
 public ExcelOpenXmlSheetReader(Stream stream, IConfiguration configuration)
 {
     _archive = new ExcelOpenXmlZip(stream);
     _config  = (OpenXmlConfiguration)configuration ?? OpenXmlConfiguration.DefaultConfig;
     SetSharedStrings();
 }
Example #5
0
 public ExcelOpenXmlTemplate(Stream strem, IConfiguration configuration)
 {
     _stream        = strem;
     _configuration = (OpenXmlConfiguration)configuration ?? OpenXmlConfiguration.DefaultConfig;
 }