Files
DP44/DataPRO/Modules/Reports/PedestrianAndHeadReports/Classes/ExportBase.cs
2026-04-17 14:55:32 -04:00

96 lines
4.2 KiB
C#

using DocumentFormat.OpenXml.Packaging;
using Ap = DocumentFormat.OpenXml.ExtendedProperties;
using Vt = DocumentFormat.OpenXml.VariantTypes;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Spreadsheet;
using Xdr = DocumentFormat.OpenXml.Drawing.Spreadsheet;
using A = DocumentFormat.OpenXml.Drawing;
using C = DocumentFormat.OpenXml.Drawing.Charts;
using System.Linq;
namespace ExcelExport
{
public class ExportBase
{
private System.Collections.Generic.Dictionary<string, string> _cellLookup = new System.Collections.Generic.Dictionary<string, string>();
protected string GetCell(string key)
{
if (_cellLookup.ContainsKey(key)) { return _cellLookup[key]; }
System.Diagnostics.Trace.WriteLine("missing key " + key);
throw new System.NotImplementedException(key);
}
protected void AddCollectionReference(string key, string reference)
{
if (!_cellLookup.ContainsKey(key)) { _cellLookup.Add(key, reference); }
}
protected CalculationChain _calculationChain1 = new CalculationChain();
protected void InsertCalculation(string cellReference, int sheetid, bool newLevel)
{
_calculationChain1.Append(new CalculationCell()
{
CellReference = cellReference,
SheetId = sheetid,
NewLevel = newLevel
});
}
private System.Collections.Generic.Dictionary<string, int> _items = new System.Collections.Generic.Dictionary<string, int>();
protected SharedStringTablePart _sharedStringTablePart;
protected int InsertSharedStringItem(string text)
{
if (string.IsNullOrEmpty(text)) { text = ""; }
if (_items.ContainsKey(text)) { return _items[text]; }
// The text does not exist in the part. Create the SharedStringItem and return its index.
_sharedStringTablePart.SharedStringTable.AppendChild(new SharedStringItem(new DocumentFormat.OpenXml.Spreadsheet.Text(text)));
_sharedStringTablePart.SharedStringTable.Save();
_items.Add(text, _items.Keys.Count);
return (_items.Keys.Count - 1);
}
protected Cell CreateTextCell(string reference, string value)
{
Cell c = new Cell() { CellReference = reference, DataType = CellValues.SharedString };
CellValue cv = new CellValue() { Text = InsertSharedStringItem(value).ToString() };
c.Append(cv);
return c;
}
protected Cell CreateNumericCell(string column, double[] data, int index)
{
Cell c = new Cell() { CellReference = string.Format("{0}{1}", column, 2 + index) };
if (index < data.Length)
{
CellValue cv = new CellValue() { Text = data[index].ToString() };
c.Append(cv);
}
return c;
}
protected Cell CreateNumericCell(string reference, uint style, double value)
{
Cell c = new Cell() { CellReference = reference, StyleIndex = (UInt32Value)style };
c.Append(new CellValue() { Text = value.ToString() });
return c;
}
protected Cell CreateStylizedTextCell(string reference, UInt32Value style, string text)
{
Cell c = new Cell() { CellReference = reference, StyleIndex = style, DataType = CellValues.SharedString };
CellValue cv = new CellValue() { Text = InsertSharedStringItem(text).ToString() };
c.Append(cv);
return c;
}
protected Cell CreateStylizedTextCell(string reference, UInt32Value style, string text, string key)
{
if (string.IsNullOrEmpty(text)) { text = ""; }
Cell c = new Cell() { CellReference = reference, StyleIndex = style, DataType = CellValues.String };
CellValue cv = new CellValue() { Text = text };
InsertCalculation(reference, 2, true);
c.Append(new CellFormula() { Text = string.Format("\'Top page\'!{0}", GetCell(key)) });
c.Append(cv);
return c;
}
}
}