using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;
namespace ExcelZipLib
{
public class Workbook
{
///
/// The shared strings
///
public static sst SharedStrings;
///
/// All worksheets in the Excel workbook deserialized
///
/// Full path and filename of the Excel xlsx-file
///
public static List Worksheets(string ExcelFileName)
{
worksheet ws;
List allWorkSheets = new List();
using (ZipArchive zipArchive = ZipFile.Open(ExcelFileName, ZipArchiveMode.Read))
{
SharedStrings = DeserializedZipEntry(GetZipArchiveEntry(zipArchive, @"xl/sharedStrings.xml"));
foreach (var worksheetEntry in (WorkSheetFileNames(zipArchive)).OrderBy(x => x.FullName))
{
ws = DeserializedZipEntry(worksheetEntry);
ws.NumberOfColumns = worksheet.MaxColumnIndex + 1;
ws.ExpandRows();
allWorkSheets.Add(ws);
}
return allWorkSheets;
}
}
///
/// Method converting an Excel cell value to a date
///
///
///
public static DateTime DateFromExcelFormat(string ExcelCellValue)
{
return DateTime.FromOADate(Convert.ToDouble(ExcelCellValue));
}
///
/// Gets the zip archive entry.
///
/// The zip archive.
/// Name of the zip entry.
///
private static ZipArchiveEntry GetZipArchiveEntry(ZipArchive ZipArchive, string ZipEntryName)
{
return ZipArchive.Entries.First(n => n.FullName.Equals(ZipEntryName));
}
///
/// Works the sheet file names.
///
/// The zip archive.
///
private static IEnumerable WorkSheetFileNames(ZipArchive ZipArchive)
{
foreach (var zipEntry in ZipArchive.Entries)
if (zipEntry.FullName.StartsWith("xl/worksheets/sheet"))
yield return zipEntry;
}
///
/// Deserializeds the zip entry.
///
///
/// The zip archive entry.
///
private static T DeserializedZipEntry(ZipArchiveEntry ZipArchiveEntry)
{
using (Stream stream = ZipArchiveEntry.Open())
return (T)new XmlSerializer(typeof(T)).Deserialize(XmlReader.Create(stream));
}
}
}