Source

main / Source / WebClient / Web.BizLogic / Extensions / SpreadsheetDocumentExtension.cs

Full commit
Naethra D 3ee1b94 






































































































































































































































































































































































































































































































































































































































































































  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml;
using DataUp.Web.Common;
using System.Collections;
using System.IO;
using System.Web;
using System.Xml;
using DocumentFormat.OpenXml.Bibliography;

namespace DataUp.Web.BizLogic
{
    /// <summary>
    /// Extension methods for a Excel SpreadSheet.
    /// </summary>
    public static class SpreadsheetDocumentExtension
    {
        // Given a workbook document, and a sheet name, return the Worksheet
        // corresponding to the supplied name. 
        public static Sheet GetSheet(this SpreadsheetDocument document, string sheetName)
        {
            WorkbookPart wbPart = document.WorkbookPart;

            // Find the sheet with the supplied name, and then use that Sheet object
            // to retrieve a reference to the appropriate worksheet.
            Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().
              Where(s => string.Compare(s.Name, sheetName, true) == 0).FirstOrDefault();

            return theSheet;
        }

        public static Sheet InsertWorksheet(this SpreadsheetDocument document, string sheetName)
        {
            // Add a blank WorksheetPart.
            WorksheetPart newWorksheetPart = document.WorkbookPart.AddNewPart<WorksheetPart>();
            newWorksheetPart.Worksheet = new Worksheet(new SheetData());

            Sheets sheets = document.WorkbookPart.Workbook.GetFirstChild<Sheets>();
            string relationshipId = document.WorkbookPart.GetIdOfPart(newWorksheetPart);

            // Get a unique ID for the new worksheet.
            uint sheetId = 1;
            if (sheets.Elements<Sheet>().Count() > 0)
            {
                sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
            }

            // Append the new worksheet and associate it with the workbook.
            Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
            sheets.Append(sheet);

            return sheet;
        }

        public static Sheet InsertMetadataWorksheet(this SpreadsheetDocument document, bool hasParameterMetaData)
        {
            // Add a blank WorksheetPart.
            WorksheetPart newWorksheetPart = document.WorkbookPart.AddNewPart<WorksheetPart>();
            newWorksheetPart.Worksheet = new Worksheet(new SheetData());

            TableDefinitionPart tableDefinitionPart = newWorksheetPart.AddNewPart<TableDefinitionPart>();
            GenerateTableDefinitionPartContent(tableDefinitionPart);

            string paramTableId = String.Empty;
            if (hasParameterMetaData)
            {
                //Generate ParameterMetaData.
                TableDefinitionPart tableParameterPart = newWorksheetPart.AddNewPart<TableDefinitionPart>();
                GenerateParameterTablePartContent(tableParameterPart);
                paramTableId = newWorksheetPart.GetIdOfPart(tableParameterPart);
            }
            
            //Generate Content.
            GeneratePartContent(newWorksheetPart, new string[]{newWorksheetPart.GetIdOfPart(tableDefinitionPart), 
                                                                       paramTableId });

            Sheets sheets = document.WorkbookPart.Workbook.GetFirstChild<Sheets>();
            string relationshipId = document.WorkbookPart.GetIdOfPart(newWorksheetPart);

            // Get a unique ID for the new worksheet.
            uint sheetId = 1;
            if (sheets.Elements<Sheet>().Count() > 0)
            {
                sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
            }

            // Append the new worksheet and associate it with the workbook.
            Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = Constants.MetadataSheetName };
            sheets.Append(sheet);

            return sheet;
        }

        // Generates content of tableDefinitionPart1.
        private static void GenerateTableDefinitionPartContent(TableDefinitionPart tableDefinitionPart)
        {
            Table table1 = new Table() { Id = (UInt32Value)5U, Name = Constants.MetadataRangeName, DisplayName = Constants.MetadataRangeName, Reference = "A1:B44", TotalsRowShown = false };
            AutoFilter autoFilter1 = new AutoFilter() { Reference = "A1:B44" };

            TableColumns tableColumns1 = new TableColumns() { Count = (UInt32Value)2U };
            TableColumn tableColumn1 = new TableColumn() { Id = (UInt32Value)1U, Name = "Name" };
            TableColumn tableColumn2 = new TableColumn() { Id = (UInt32Value)2U, Name = "Value" };

            tableColumns1.Append(tableColumn1);
            tableColumns1.Append(tableColumn2);
            TableStyleInfo tableStyleInfo1 = new TableStyleInfo() { Name = Constants.MetadataTableStyleName, ShowFirstColumn = false, ShowLastColumn = false, ShowRowStripes = true, ShowColumnStripes = false };

            table1.Append(autoFilter1);
            table1.Append(tableColumns1);
            table1.Append(tableStyleInfo1);

            tableDefinitionPart.Table = table1;
        }

        // Generates content of tableDefinitionPart1.
        private static void GenerateParameterTablePartContent(TableDefinitionPart tableDefinitionPart)
        {
            Table table1 = new Table() { Id = (UInt32Value)6U, Name = Constants.ParaMetadataRangeName, DisplayName = Constants.ParaMetadataRangeName, Reference = "D1:I44", TotalsRowShown = false };
            AutoFilter autoFilter1 = new AutoFilter() { Reference = "D1:I44" };

            TableColumns tableColumns1 = new TableColumns() { Count = (UInt32Value)6U };

            tableColumns1.Append(new TableColumn() { Id = (UInt32Value)1U, Name = "EntityName" });
            tableColumns1.Append(new TableColumn() { Id = (UInt32Value)2U, Name = "EntityDescription" });
            tableColumns1.Append(new TableColumn() { Id = (UInt32Value)3U, Name = "Name" });
            tableColumns1.Append(new TableColumn() { Id = (UInt32Value)4U, Name = "Description" });
            tableColumns1.Append(new TableColumn() { Id = (UInt32Value)5U, Name = "Type" });
            tableColumns1.Append(new TableColumn() { Id = (UInt32Value)6U, Name = "Units" });

            TableStyleInfo tableStyleInfo1 = new TableStyleInfo() { Name = Constants.MetadataTableStyleName, ShowFirstColumn = false, ShowLastColumn = false, ShowRowStripes = true, ShowColumnStripes = false };

            table1.Append(autoFilter1);
            table1.Append(tableColumns1);
            table1.Append(tableStyleInfo1);

            tableDefinitionPart.Table = table1;
        }

        /// <summary>
        /// Generates the content of the WorkSheet part.
        /// </summary>
        /// <param name="part">The part.</param>
        private static void GeneratePartContent(WorksheetPart part, string[] partIds)
        {
            Worksheet worksheet1 = new Worksheet() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "x14ac" } };
            worksheet1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
            worksheet1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
            worksheet1.AddNamespaceDeclaration("x14ac", "http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac");
            SheetDimension sheetDimension1 = new SheetDimension() { Reference = "A1:I44" };

            SheetViews sheetViews1 = new SheetViews();

            SheetView sheetView1 = new SheetView() { TabSelected = true, WorkbookViewId = (UInt32Value)0U };
            Selection selection1 = new Selection() { ActiveCell = "K9", SequenceOfReferences = new ListValue<StringValue>() { InnerText = "K9" } };

            sheetView1.Append(selection1);

            sheetViews1.Append(sheetView1);
            SheetFormatProperties sheetFormatProperties1 = new SheetFormatProperties() { DefaultRowHeight = 15D, DyDescent = 0.25D };

            Columns columns1 = new Columns();
            Column column1 = new Column() { Min = (UInt32Value)1U, Max = (UInt32Value)2U, Width = 11D, CustomWidth = true };

            columns1.Append(column1);

            SheetData sheetData1 = new SheetData();

            Row row1 = new Row() { RowIndex = (UInt32Value)1U, Spans = new ListValue<StringValue>() { InnerText = (String.IsNullOrEmpty(partIds[1]))?"1:2" : "1:9" }, DyDescent = 0.25D };

            Cell cell1 = new Cell() { CellReference = "A1", DataType = CellValues.String };
            CellValue cellValue1 = new CellValue(){Text = "Name"};
            cell1.Append(cellValue1);
            row1.Append(cell1);

            Cell cell2 = new Cell() { CellReference = "B1", DataType = CellValues.String };
            CellValue cellValue2 = new CellValue(){Text = "Value"};
            cell2.Append(cellValue2);
            row1.Append(cell2);

            if (!String.IsNullOrEmpty(partIds[1]))
            {
                Cell cell3 = new Cell() { CellReference = "D1", DataType = CellValues.String };
                CellValue cellValue3 = new CellValue() { Text = "EntityName" };

                cell3.Append(cellValue3);
                row1.Append(cell3);

                Cell cell4 = new Cell() { CellReference = "E1", DataType = CellValues.String };
                CellValue cellValue4 = new CellValue() { Text = "EntityDescription" };
                cell4.Append(cellValue4);
                row1.Append(cell4);

                Cell cell5 = new Cell() { CellReference = "F1", DataType = CellValues.String };
                CellValue cellValue5 = new CellValue(){Text = "Name"};
                cell5.Append(cellValue5);
                row1.Append(cell5);

                Cell cell6 = new Cell() { CellReference = "G1", DataType = CellValues.String };
                CellValue cellValue6 = new CellValue(){Text = "Description"};
                cell6.Append(cellValue6);
                row1.Append(cell6);

                Cell cell7 = new Cell() { CellReference = "H1", DataType = CellValues.String };
                CellValue cellValue7 = new CellValue(){Text = "Type"};
                cell7.Append(cellValue7);
                row1.Append(cell7);

                Cell cell8 = new Cell() { CellReference = "I1", DataType = CellValues.String };
                CellValue cellValue8 = new CellValue(){Text = "Units"};
                cell8.Append(cellValue8);
                row1.Append(cell8);
            }

            sheetData1.Append(row1);
            PageMargins pageMargins1 = new PageMargins() { Left = 0.7D, Right = 0.7D, Top = 0.75D, Bottom = 0.75D, Header = 0.3D, Footer = 0.3D };

            TableParts tableParts1 = new TableParts() { Count =  (String.IsNullOrEmpty(partIds[1]))?(UInt32Value)1U : (UInt32Value)2U };
            tableParts1.Append(new TablePart() { Id = partIds[0] });

            if (!String.IsNullOrEmpty(partIds[1]))
            {
                tableParts1.Append(new TablePart() { Id = partIds[1] });   
            }

            worksheet1.Append(sheetDimension1);
            worksheet1.Append(sheetViews1);
            worksheet1.Append(sheetFormatProperties1);
            worksheet1.Append(columns1);
            worksheet1.Append(sheetData1);
            worksheet1.Append(pageMargins1);
            worksheet1.Append(tableParts1);

            part.Worksheet = worksheet1;
        }

        public static bool SetCellValue(this SpreadsheetDocument document, Sheet metadataSheet, string addressName, string value)
        {
            // If the string exists in the shared string table, get its index.
            // If the string doesn't exist in the shared string table, add it and get the next index.

            // Assume failure.
            bool returnValue = false;

            // Open the document for editing.
            WorkbookPart wbPart = document.WorkbookPart;

            if (metadataSheet != null)
            {
                Worksheet ws = ((WorksheetPart)(wbPart.GetPartById(metadataSheet.Id))).Worksheet;

                Cell theCell = ws.InsertCellInWorksheet(addressName);

                // Either retrieve the index of an existing string,
                // or insert the string into the shared string table
                // and get the index of the new item.
                int stringIndex = wbPart.InsertSharedStringItem(value);

                theCell.CellValue = new CellValue(stringIndex.ToString());
                theCell.DataType = new EnumValue<CellValues>(CellValues.SharedString);

                // Save the worksheet.
                ws.Save();
                returnValue = true;
            }

            return returnValue;
        }

        // Get the value of a cell, given a file name, sheet name, and address name.
        public static string GetCellValue(this SpreadsheetDocument document, Sheet metadataSheet, string addressName)
        {
            string value = string.Empty;

            WorkbookPart wbPart = document.WorkbookPart;

            // Retrieve a reference to the worksheet part, and then use its Worksheet property to get 
            // a reference to the cell whose address matches the address you've supplied:
            WorksheetPart wsPart = (WorksheetPart)(wbPart.GetPartById(metadataSheet.Id));
            Cell theCell = wsPart.Worksheet.Descendants<Cell>().
              Where(c => c.CellReference == addressName).FirstOrDefault();

            // If the cell doesn't exist, return an empty string:
            if (theCell != null)
            {
                value = theCell.InnerText;

                // If the cell represents an integer number, you're done. 
                // For dates, this code returns the serialized value that 
                // represents the date. The code handles strings and booleans
                // individually. For shared strings, the code looks up the corresponding
                // value in the shared string table. For booleans, the code converts 
                // the value into the words TRUE or FALSE.
                if (theCell.DataType != null)
                {
                    switch (theCell.DataType.Value)
                    {
                        case CellValues.SharedString:
                            // For shared strings, look up the value in the shared strings table.
                            var stringTable = wbPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
                            // If the shared string table is missing, something's wrong.
                            // Just return the index that you found in the cell.
                            // Otherwise, look up the correct text in the table.
                            if (stringTable != null)
                            {
                                value = stringTable.SharedStringTable.ElementAt(int.Parse(value)).InnerText;
                            }
                            break;

                        case CellValues.Boolean:
                            switch (value)
                            {
                                case "0":
                                    value = "FALSE";
                                    break;
                                default:
                                    value = "TRUE";
                                    break;
                            }
                            break;
                    }
                }
            }
            return value;
        }

        /// <summary>
        /// Gets the metadata details.
        /// </summary>
        /// <param name="excelDocument">The excel document.</param>
        /// <returns>List of Metadata</returns>
        public static MetadataDetailList GetMetadataDetails(this SpreadsheetDocument excelDocument)
        {

            MetadataDetailList metadataList = null;
            var metadataSheet = excelDocument.GetSheet(Constants.MetadataSheetName);
            if (metadataSheet == null)
            {
                var defaultMetadata = MetadataHelper.GetDefaultMetadataList();

                UpdateDefaultProperties(excelDocument, defaultMetadata);

                metadataList = new MetadataDetailList()
                {
                    Metadata = defaultMetadata
                };
            }
            else
            {
                Hashtable actualMetadata = new Hashtable();
                Hashtable actualParameterData = new Hashtable();
                Collection<ParameterMetadataDetail> paramterMetadata = new Collection<ParameterMetadataDetail>();

                // Get Value from Metadata Sheet.
                for (int i = 1; i < 45; i++)
                {
                    string key = excelDocument.GetCellValue(metadataSheet, "A" + i.ToString());
                    string value = excelDocument.GetCellValue(metadataSheet, "B" + i.ToString());

                    if (!string.IsNullOrWhiteSpace(key) && !actualMetadata.ContainsKey(key))
                    {
                        actualMetadata.Add(key, value);
                    }
                }

                int current = 2;
                string name = excelDocument.GetCellValue(metadataSheet, "D" + current);
                string prevName = String.Empty;
                ParameterMetadataDetail paramData = default(ParameterMetadataDetail);
                while (!string.IsNullOrWhiteSpace(name))
                {
                    if (prevName != name)
                    {
                        paramData = new ParameterMetadataDetail()
                                            {
                                                EntityName = excelDocument.GetCellValue(metadataSheet, "D" + current.ToString()),
                                                EntityDescription = excelDocument.GetCellValue(metadataSheet, "E" + current.ToString()),
                                            };
                    }

                    paramData.AttributeList.Add(
                        new ParameterAttribute
                        {
                            Name = excelDocument.GetCellValue(metadataSheet, "F" + current.ToString()),
                            Description = excelDocument.GetCellValue(metadataSheet, "G" + current.ToString()),
                            Type = excelDocument.GetCellValue(metadataSheet, "H" + current.ToString()),
                            Units = excelDocument.GetCellValue(metadataSheet, "I" + current.ToString())
                        });

                    current++;
                    prevName = name;
                    name = excelDocument.GetCellValue(metadataSheet, "D" + current);
                    if (prevName != name)
                    {
                        paramterMetadata.Add(paramData);
                    }
                }

                var defaultMetadata = MetadataHelper.GetDefaultMetadataList();
                foreach (var item in defaultMetadata)
                {
                    if (!string.IsNullOrWhiteSpace(item.Type) && item.Type.Trim() == "DateTime")
                    {

                        DateTime result;

                        if (!string.IsNullOrWhiteSpace(actualMetadata[item.Name].ToString()) && !DateTime.TryParse(actualMetadata[item.Name].ToString(), out result))
                        {
                            double d = double.Parse(actualMetadata[item.Name].ToString());
                            DateTime convDateTime = DateTime.FromOADate(d);
                            item.Value = convDateTime.ToString("yyyy-MM-dd");
                        }
                        else
                        {
                            item.Value = actualMetadata.ContainsKey(item.Name) ? actualMetadata[item.Name].ToString() : string.Empty;
                        }

                    }

                    else
                    {
                        item.Value = actualMetadata.ContainsKey(item.Name) ? actualMetadata[item.Name].ToString() : string.Empty;
                    }
                }

                metadataList = new MetadataDetailList()
                {
                    Metadata = defaultMetadata,
                    ParamterMetadata = paramterMetadata
                };
            }

            return metadataList;
        }

        /// <summary>
        /// Updates the default properties.
        /// </summary>
        /// <param name="excelDocument">The excel document.</param>
        /// <param name="defaultMetadata">The default metadata.</param>
        private static void UpdateDefaultProperties(SpreadsheetDocument excelDocument, Collection<MetadataDetail> defaultMetadata)
        {

            // Get Values from default properties.
            var corePart = excelDocument.CoreFilePropertiesPart;
            XmlDocument xmlProperties = new XmlDocument();
            if (corePart == null)
            {
                return;
            }
            xmlProperties.Load(corePart.GetStream());
            try
            {
                var date = xmlProperties.GetElementsByTagName("created", Constants.DCTerms).Item(0);
                var dateNode = defaultMetadata.Where(m => m.Name.EndsWith("Today's date", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                if (dateNode != null && (date != null))
                {
                    //dateNode.Value = DateTime.Now.ToString(Constants.Dateformate, System.Globalization.CultureInfo.GetCultureInfo("en-US"));
                    dateNode.Value = DateTime.Parse(date.InnerText).ToString(Constants.Dateformate);
                }
            }
            catch (Exception)
            {

            }

            // Access Title property
            try
            {
                var title = xmlProperties.GetElementsByTagName("title", Constants.DC).Item(0);

                // title
                var titleNode = defaultMetadata.Where(m => m.Name.EndsWith("Title of dataset", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                if (titleNode != null)
                {
                    titleNode.Value = (title != null) ? title.InnerText : string.Empty;
                }
            }
            catch (Exception)
            {

            }
        }

        public static string GetSheetName(this SpreadsheetDocument document, WorksheetPart sheet)
        {
            string sheetID = document.WorkbookPart.GetIdOfPart(sheet);
            WorkbookPart wbPart = document.WorkbookPart;

            // Find the sheet with the supplied name, and then use that Sheet object
            // to retrieve a reference to the appropriate worksheet.
            Sheet theSheet = wbPart.Workbook.Descendants<Sheet>()
                .Where(s => s.Id == sheetID).FirstOrDefault();
            return theSheet.Name;
        }

        public static string GetSheetId(this SpreadsheetDocument document, WorksheetPart sheet)
        {
            string sheetID = document.WorkbookPart.GetIdOfPart(sheet);
            WorkbookPart wbPart = document.WorkbookPart;

            // Find the sheet with the supplied name, and then use that Sheet object
            // to retrieve a reference to the appropriate worksheet.
            Sheet theSheet = wbPart.Workbook.Descendants<Sheet>()
                .Where(s => s.Id == sheetID).FirstOrDefault();
            return theSheet.SheetId;
        }

        public static bool IsSheetHidden(this SpreadsheetDocument document, WorksheetPart sheet)
        {
            bool isHidden = false;
            string sheetID = document.WorkbookPart.GetIdOfPart(sheet);
            WorkbookPart wbPart = document.WorkbookPart;
            Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().Where(s => s.Id == sheetID).First();
            // string name = sheet.
            if (theSheet.State == "Hidden" || theSheet.State == "veryHidden")
            {
                isHidden = true;
            }
            return isHidden;
        }

        /// <summary>
        /// Gets the zip file stream.
        /// </summary>
        /// <param name="document">The SpreadSheet document.</param>
        /// <returns></returns>
        public static Stream GetZipFileStream(this SpreadsheetDocument document, string name)
        {
            SharedStringTable sharedstringtable = new SharedStringTable();

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            WorkbookPart wbPart = document.WorkbookPart;
            var sheets = wbPart.GetPartsOfType<WorksheetPart>();

            WorkbookStylesPart workbookStylesPart = document.WorkbookPart.GetPartsOfType<WorkbookStylesPart>().FirstOrDefault();
            var sharedStringPart = wbPart.SharedStringTablePart;
            CellFormats cellFormats = null;
            if (workbookStylesPart != null)
            {
                cellFormats = (CellFormats)workbookStylesPart.Stylesheet.CellFormats;
            }

            if (sharedStringPart != null)
            {
                sharedstringtable = sharedStringPart.SharedStringTable;
            }

            string fileName = string.Format("{0}\\{1}\\", Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));

            DirectoryInfo dir = Directory.CreateDirectory(fileName);
            SheetData sheetdata;
            string sheetName = String.Empty;

            foreach (var sheet in sheets)
            {
                sheetdata = sheet.Worksheet.Elements<SheetData>().FirstOrDefault();
                sheetName = document.GetSheetName(sheet);
                if (sheetdata.Descendants<Row>().Count() > 0 && sheetName != Constants.MetadataSheetName)
                {
                    try
                    {
                        string filename = string.Format("{0}{1}-{2}.csv", dir.FullName, name, sheetName);
                        using (StreamWriter file = new StreamWriter(filename))
                        {
                            foreach (var row in sheetdata.Descendants<Row>())
                            {
                                foreach (Cell cell in row.Descendants<Cell>())
                                {
                                    if (cell.CellValue != null)
                                    {
                                        string val = GetCellValue(sharedstringtable, cellFormats, cell);
                                        sb.AppendFormat("{0},", CsvHelper.Escape(val));
                                    }
                                    else
                                    {
                                        sb.Append(",");
                                    }
                                }
                                file.WriteLine(sb.ToString());
                                sb.Clear();
                            }
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
            sb = null;
            return ZipFileHelper.ZipFiles(dir.FullName);
        }

        private static string GetCellValue(SharedStringTable sharedstringtable, CellFormats cellFormats, Cell cell)
        {
            string str = string.Empty;
            if (cell.DataType == null)
            {
                if (cell.StyleIndex != null)
                {
                    CellFormat cf = cellFormats.Descendants<CellFormat>().ElementAt<CellFormat>(Convert.ToInt32(cell.StyleIndex.Value));
                    //this is for number
                    if (cf.NumberFormatId != null && cf.NumberFormatId >= 0 && cf.NumberFormatId <= 13)
                    {
                        decimal decimalValue = 0.0M;
                        decimal.TryParse(cell.CellValue.Text, out decimalValue);
                        var valtxt = decimalValue;
                        str = valtxt.ToString();

                    }
                    //this is for date
                    else if (cf.NumberFormatId != null && cf.NumberFormatId >= 14 && cf.NumberFormatId <= 22)
                    {
                        var valdate = DateTime.FromOADate(Convert.ToDouble(cell.CellValue.Text));
                        str = valdate.ToString();
                    }
                    else
                    {
                        //this is for some text
                        var valtxt = cell.CellValue.Text;
                        str = valtxt.ToString();
                    }
                }
                else
                {
                    decimal decimalValue = 0.0M;
                    decimal.TryParse(cell.CellValue.Text, out decimalValue);
                    var valtxt = decimalValue;
                    str = valtxt.ToString();
                }
            }
            else
            {
                switch (cell.DataType.Value)
                {
                    case CellValues.SharedString:
                        if (sharedstringtable != null)
                        {
                            var sharedstring = sharedstringtable.ChildElements[Convert.ToInt32(cell.CellValue.Text)].InnerText;
                            if (sharedstring != null)
                            {
                                str = sharedstring.ToString();
                            }
                        }
                        break;
                    case CellValues.Boolean:
                        str = cell.CellValue.Text == "1" ? "true" : "false";
                        break;
                    case CellValues.Date:
                        var dateval = DateTime.FromOADate(Convert.ToDouble(cell.CellValue.Text));
                        str = dateval.ToString();
                        break;
                    case CellValues.Number:
                        decimal decimalValue = 0.0M;
                        decimal.TryParse(cell.CellValue.Text, out decimalValue);
                        var numval = decimalValue;
                        str = numval.ToString();
                        break;
                    default:
                        if (cell.CellValue != null)
                        {
                            var somtxt = cell.CellValue.Text;
                            if (somtxt != null)
                            {
                                str = somtxt.ToString();
                            }
                        }
                        break;
                }
            }
            return str;
        }
    }
}