示例#1
0
 public ExcelDocument(string filename)
 {
     if (File.Exists(filename))
     {
         document = SpreadsheetDocument.Open(filename, true);
         if (document.WorkbookPart.GetPartsOfType<WorkbookStylesPart>().Count() == 0)
         {
             WorkbookStylesPart stylespart = document.WorkbookPart.AddNewPart<WorkbookStylesPart>();
             stylespart.Stylesheet = GetStylesheet();
             DateTypeIndex = 1;
             stylesheet = stylespart.Stylesheet;
         }
         else
         {
             WorkbookStylesPart stylespart = document.WorkbookPart.GetPartsOfType<WorkbookStylesPart>().First();
             IEnumerable<CellFormat> cellformats = stylespart.Stylesheet.CellFormats.Elements<CellFormat>();
             if (cellformats.Where(f => f.NumberFormatId == 14).Count() > 0)
             {
                 uint dateformatindex = 0;
                 foreach (CellFormat format in cellformats)
                     if (format.NumberFormatId == 14) break; else dateformatindex++;
                 DateTypeIndex = dateformatindex;
             }
             else
             {
                 stylespart.Stylesheet.CellFormats.Append(new CellFormat()
                 {
                     BorderId = 0,
                     FillId = 0,
                     FontId = 0,
                     NumberFormatId = 14,
                     FormatId = 0,
                     ApplyNumberFormat = true
                 });
                 stylespart.Stylesheet.CellFormats.Count = (uint)stylespart.Stylesheet.CellFormats.ChildElements.Count;
                 DateTypeIndex = stylespart.Stylesheet.CellFormats.Count - 1;
             }
             stylesheet = stylespart.Stylesheet;
         }
     }
     else
     {
         document = SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook);
         WorkbookPart workbookpart = document.AddWorkbookPart();
         workbookpart.Workbook = new Workbook();
         WorkbookStylesPart stylespart = workbookpart.AddNewPart<WorkbookStylesPart>();
         stylespart.Stylesheet = GetStylesheet();
         DateTypeIndex = 1;
         stylesheet = stylespart.Stylesheet;
     }
     Sheets = new ExcelSheetCollection(this);
 }
示例#2
0
 public ExcelDocument(string filename)
 {
     if (File.Exists(filename))
     {
         document = SpreadsheetDocument.Open(filename, true);
         if (document.WorkbookPart.GetPartsOfType <WorkbookStylesPart>().Count() == 0)
         {
             WorkbookStylesPart stylespart = document.WorkbookPart.AddNewPart <WorkbookStylesPart>();
             stylespart.Stylesheet = GetStylesheet();
             DateTypeIndex         = 1;
             stylesheet            = stylespart.Stylesheet;
         }
         else
         {
             WorkbookStylesPart       stylespart  = document.WorkbookPart.GetPartsOfType <WorkbookStylesPart>().First();
             IEnumerable <CellFormat> cellformats = stylespart.Stylesheet.CellFormats.Elements <CellFormat>();
             if (cellformats.Where(f => f.NumberFormatId == 14).Count() > 0)
             {
                 uint dateformatindex = 0;
                 foreach (CellFormat format in cellformats)
                 {
                     if (format.NumberFormatId == 14)
                     {
                         break;
                     }
                     else
                     {
                         dateformatindex++;
                     }
                 }
                 DateTypeIndex = dateformatindex;
             }
             else
             {
                 stylespart.Stylesheet.CellFormats.Append(new CellFormat()
                 {
                     BorderId          = 0,
                     FillId            = 0,
                     FontId            = 0,
                     NumberFormatId    = 14,
                     FormatId          = 0,
                     ApplyNumberFormat = true
                 });
                 stylespart.Stylesheet.CellFormats.Count = (uint)stylespart.Stylesheet.CellFormats.ChildElements.Count;
                 DateTypeIndex = stylespart.Stylesheet.CellFormats.Count - 1;
             }
             stylesheet = stylespart.Stylesheet;
         }
     }
     else
     {
         document = SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook);
         WorkbookPart workbookpart = document.AddWorkbookPart();
         workbookpart.Workbook = new Workbook();
         WorkbookStylesPart stylespart = workbookpart.AddNewPart <WorkbookStylesPart>();
         stylespart.Stylesheet = GetStylesheet();
         DateTypeIndex         = 1;
         stylesheet            = stylespart.Stylesheet;
     }
     Sheets = new ExcelSheetCollection(this);
 }