Big changes

This commit is contained in:
Duc
2025-03-13 12:04:22 -07:00
parent c689fcb7f9
commit ffa9905494
748 changed files with 199255 additions and 3743 deletions

View File

@@ -0,0 +1,118 @@
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
namespace ExcelZipLib
{
public class Cell
{
/// <summary>
/// Used for converting from Excel column/row to column index starting at 0
/// </summary>
[XmlAttribute("r")]
public string CellReference
{
get
{
return ColumnIndex.ToString();
}
set
{
ColumnIndex = GetColumnIndex(value);
if (ColumnIndex > worksheet.MaxColumnIndex)
worksheet.MaxColumnIndex = ColumnIndex;
}
}
/// <summary>
/// The t type
/// </summary>
[XmlAttribute("t")]
public string tType = "";
/// <summary>
/// Original value of the Excel cell
/// </summary>
[XmlElement("v")]
public string Value
{
get
{
return _value;
}
set
{
_value = value;
if (tType.Equals("s"))
{
Text = Workbook.SharedStrings.si[Convert.ToInt32(_value)].t;
return;
}
if (tType.Equals("str"))
{
Text = _value;
return;
}
try
{
Amount = Convert.ToDouble(_value, CultureInfo.InvariantCulture);
Text = Amount.ToString("#,##0.##");
IsAmount = true;
}
catch (Exception ex)
{
Amount = 0;
Text = String.Format("Cell Value '{0}': {1}", _value, ex.Message);
}
}
}
/// <summary>
/// Index of the orignal Excel cell column starting at 0
/// </summary>
[XmlIgnore]
public int ColumnIndex;
/// <summary>
/// Text of the Excel cell (if it was a string)
/// </summary>
[XmlIgnore]
public string Text = "";
/// <summary>
/// Amount of the Excel cell (if it was a number)
/// </summary>
[XmlIgnore]
public double Amount;
/// <summary>
/// The is amount
/// </summary>
[XmlIgnore]
public bool IsAmount;
/// <summary>
/// The _value.
/// </summary>
private string _value = "";
/// <summary>
/// Gets the index of the column.
/// </summary>
/// <param name="CellReference">The cell reference.</param>
/// <returns></returns>
private int GetColumnIndex(string CellReference)
{
string colLetter = new Regex("[A-Za-z]+").Match(CellReference).Value.ToUpper();
int colIndex = 0;
for (int i = 0; i < colLetter.Length; i++)
{
colIndex *= 26;
colIndex += (colLetter[i] - 'A' + 1);
}
return colIndex - 1;
}
}
}

View File

@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$(SolutionDir)Solution.props" />
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<AssemblyName>ExcelZip</AssemblyName>
<Product>Composable Test Software Library</Product>
<Description>Microsoft Excel Zip Library</Description>
<OutputType>Library</OutputType>
<!-- Static versioning (Suitable for Development) -->
<!-- Disable the line below for dynamic versioning -->
<Version>1.0.0</Version>
</PropertyGroup>
<ItemGroup>
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,35 @@
using System.Xml.Serialization;
namespace ExcelZipLib
{
public class Row
{
/// <summary>
/// The filled cells.
/// </summary>
[XmlElement("c")]
public Cell[] FilledCells;
/// <summary>
/// The cells.
/// </summary>
[XmlIgnore]
public Cell[] Cells;
/// <summary>
/// Expands the cells.
/// </summary>
/// <param name="NumberOfColumns">The number of columns.</param>
public void ExpandCells(int NumberOfColumns)
{
Cells = new Cell[NumberOfColumns];
foreach (var cell in FilledCells)
{
Cells[cell.ColumnIndex] = cell;
}
FilledCells = null;
}
}
}

View File

@@ -0,0 +1,49 @@
using System;
using System.Xml;
using System.Xml.Serialization;
namespace ExcelZipLib
{
[Serializable()]
[XmlType(Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main")]
[XmlRoot("sst", Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main")]
public class sst
{
/// <summary>
/// The unique count
/// </summary>
[XmlAttribute]
public string uniqueCount;
/// <summary>
/// The count
/// </summary>
[XmlAttribute]
public string count;
/// <summary>
/// The si
/// </summary>
[XmlElement("si")]
[NonSerialized]
public SharedString[] si;
/// <summary>
/// Initializes a new instance of the <see cref="sst"/> class.
/// </summary>
public sst()
{
}
}
/// <summary>
/// The shared string class.
/// </summary>
public class SharedString
{
/// <summary>
/// The t.
/// </summary>
public string t;
}
}

View File

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

View File

@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
namespace ExcelZipLib
{
[Serializable()]
[XmlType(Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main")]
[XmlRoot("worksheet", Namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main")]
public class worksheet
{
/// <summary>
/// The rows
/// </summary>
[XmlArray("sheetData")]
[XmlArrayItem("row")]
[NonSerialized]
public List<Row> Rows;
/// <summary>
/// The number of columns
/// </summary>
[XmlIgnore]
public int NumberOfColumns; // Total number of columns in this worksheet
/// <summary>
/// The maximum column index
/// </summary>
public static int MaxColumnIndex = 0; // Temporary variable for import
/// <summary>
/// Initializes a new instance of the <see cref="worksheet"/> class.
/// </summary>
public worksheet()
{
}
/// <summary>
/// Expands the rows.
/// </summary>
public void ExpandRows()
{
foreach (var row in Rows)
row.ExpandCells(NumberOfColumns);
}
}
}