Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

Bug resovled: Supporting Font Charset attribute #598

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
31 changes: 26 additions & 5 deletions EPPlus/EPPlus.MultiTarget.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.1;netstandard2.0;net35;net40</TargetFrameworks>
<AssemblyVersion>4.5.3.3</AssemblyVersion>
<FileVersion>4.5.3.3</FileVersion>
<Version>4.5.3.3</Version>
<!--<TargetFrameworks>netcoreapp3.1;netcoreapp2.1;netstandard2.0;net35;net40</TargetFrameworks>-->
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyVersion>4.5.3.8</AssemblyVersion>
<FileVersion>4.5.3.8</FileVersion>
<Version>4.5.3.8</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseUrl></PackageLicenseUrl>
<PackageProjectUrl>https://github.com/JanKallman/EPPlus</PackageProjectUrl>
Expand All @@ -18,7 +19,7 @@
<PackageTags>Excel ooxml</PackageTags>
<Copyright>Jan Källman 2020</Copyright>
<PackageReleaseNotes>
# EPPlus 4.5.3.3
# EPPlus 4.5.3.4 With Font Charset (Core 3.1)

## Announcement: This is the last version of EPPlus under the LGPL License
EPPlus will from version 5 be licensed under the [Polyform Noncommercial 1.0.0]( https://polyformproject.org/licenses/noncommercial/1.0.0/) license.
Expand Down Expand Up @@ -246,6 +247,10 @@ Release Candidate changes
<DefineConstants>Core</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1'">
<DefineConstants>Core</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net35'">
<DefineConstants>NET35;NETFULL</DefineConstants>
</PropertyGroup>
Expand Down Expand Up @@ -320,6 +325,22 @@ Release Candidate changes
<PackageReference Include="System.Xml.XmlDocument" Version="4.3.0" />
<PackageReference Include="System.Xml.XPath.XmlDocument" Version="4.3.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.0" />
<PackageReference Include="System.ComponentModel.TypeConverter" Version="4.3.0" />
<PackageReference Include="System.Data.Common" Version="4.3.0" />
<PackageReference Include="System.Drawing.Common">
<Version>4.7.0</Version>
</PackageReference>
<PackageReference Include="System.Reflection" Version="4.3.0" />
<PackageReference Include="System.Security.Claims" Version="4.3.0" />
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="4.7.0" />
<PackageReference Include="System.Security.Cryptography.X509Certificates" Version="4.3.2" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.7.0" />
<PackageReference Include="System.Xml.XmlDocument" Version="4.3.0" />
<PackageReference Include="System.Xml.XPath.XmlDocument" Version="4.3.0" />
</ItemGroup>

<ItemGroup>
<None Update="readme.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
2 changes: 1 addition & 1 deletion EPPlus/ExcelAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ internal enum AddressType
internal static AddressType IsValid(string Address, bool r1c1=false)
{
double d;
if (Address == "#REF!")
if (Address == "#REF!" || Address == "'#REF'!#REF!" || Address.StartsWith("#REF!"))
{
return AddressType.Invalid;
}
Expand Down
4 changes: 2 additions & 2 deletions EPPlus/ExcelWorkbook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public ExcelNamedRangeCollection Names
#region Workbook Properties
decimal _standardFontWidth = decimal.MinValue;
string _fontID = "";
internal FormulaParser FormulaParser
public FormulaParser FormulaParser
{
get
{
Expand Down Expand Up @@ -1111,7 +1111,7 @@ internal void GetExternalReferences()
XmlElement book=xmlExtRef.SelectSingleNode("//d:externalBook", NameSpaceManager) as XmlElement;
if(book!=null)
{
string rId_ExtRef = book.GetAttribute("r:id");
string rId_ExtRef = book.GetAttribute("id", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
var rel_extRef = part.GetRelationship(rId_ExtRef);
if (rel_extRef != null)
{
Expand Down
6 changes: 3 additions & 3 deletions EPPlus/FormulaParsing/ExcelValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ public static eErrorType ToErrorType(string val)
}
}

internal static ExcelErrorValue Create(eErrorType errorType)
public static ExcelErrorValue Create(eErrorType errorType)
{
return new ExcelErrorValue(errorType);
}

internal static ExcelErrorValue Parse(string val)
public static ExcelErrorValue Parse(string val)
{
if (Values.StringIsErrorValue(val))
{
Expand All @@ -143,7 +143,7 @@ internal static ExcelErrorValue Parse(string val)
throw new ArgumentException("Not a valid error value: " + val);
}

private ExcelErrorValue(eErrorType type)
public ExcelErrorValue(eErrorType type)
{
Type=type;
}
Expand Down
17 changes: 16 additions & 1 deletion EPPlus/Style/ExcelFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ public int Family
}
}
/// <summary>
/// Font charset
/// </summary>
public int Charset
{
get
{
return _styles.Fonts[Index].Charset;
}
set
{
_ChangedEvent(this, new StyleChangeEventArgs(eStyleClass.Font, eStyleProperty.Charset, value, _positionID, _address));
}
}
/// <summary>
/// Cell color
/// </summary>
public ExcelColor Color
Expand Down Expand Up @@ -222,13 +236,14 @@ public void SetFromFont(Font Font)
Bold = Font.Bold;
UnderLine = Font.Underline;
Italic = Font.Italic;
Charset = Font.GdiCharSet;
}

internal override string Id
{
get
{
return Name + Size.ToString() + Family.ToString() + Scheme.ToString() + Bold.ToString()[0] + Italic.ToString()[0] + Strike.ToString()[0] + UnderLine.ToString()[0] + VerticalAlign;
return Name + Size.ToString() + Family.ToString() + Charset.ToString() + Scheme.ToString() + Bold.ToString()[0] + Italic.ToString()[0] + Strike.ToString()[0] + UnderLine.ToString()[0] + VerticalAlign;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion EPPlus/Style/StyleChangeEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ internal enum eStyleProperty
GradientRight,
XfId,
Indent,
QuotePrefix
QuotePrefix,
Charset
}
internal class StyleChangeEventArgs : EventArgs
{
Expand Down
21 changes: 21 additions & 0 deletions EPPlus/Style/XmlAccess/ExcelFontXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ internal ExcelFontXml(XmlNamespaceManager nameSpaceManager)
_name = "";
_size = 0;
_family = int.MinValue;
_charset = int.MinValue;
_scheme = "";
_color = _color = new ExcelColorXml(NameSpaceManager);
_bold = false;
Expand All @@ -62,6 +63,7 @@ internal ExcelFontXml(XmlNamespaceManager nsm, XmlNode topNode) :
_name = GetXmlNodeString(namePath);
_size = (float)GetXmlNodeDecimal(sizePath);
_family = GetXmlNodeIntNull(familyPath)??int.MinValue;
_charset = GetXmlNodeIntNull(charsetPath)??int.MinValue;
_scheme = GetXmlNodeString(schemePath);
_color = new ExcelColorXml(nsm, topNode.SelectSingleNode(_colorPath, nsm));
_bold = GetBoolValue(topNode, boldPath);
Expand Down Expand Up @@ -142,6 +144,22 @@ public int Family
_family=value;
}
}
const string charsetPath = "d:charset/@val";
int _charset;
/// <summary>
/// Font Charset
/// </summary>
public int Charset
{
get
{
return (_charset == int.MinValue ? 1 : _charset); // DEFAULT Charset
}
set
{
_charset = value;
}
}
ExcelColorXml _color = null;
const string _colorPath = "d:color";
/// <summary>
Expand Down Expand Up @@ -278,6 +296,7 @@ public void SetFromFont(System.Drawing.Font Font)
Bold = Font.Bold;
UnderLine=Font.Underline;
Italic=Font.Italic;
Charset = Font.GdiCharSet; // default is 1
}
public static float GetFontHeight(string name, float size)
{
Expand Down Expand Up @@ -332,6 +351,7 @@ internal ExcelFontXml Copy()
newFont.Name = _name;
newFont.Size = _size;
newFont.Family = _family;
newFont.Charset = _charset;
newFont.Scheme = _scheme;
newFont.Bold = _bold;
newFont.Italic = _italic;
Expand Down Expand Up @@ -372,6 +392,7 @@ internal override XmlNode CreateXmlNode(XmlNode topElement)
}
if(!string.IsNullOrEmpty(_name)) SetXmlNodeString(namePath, _name);
if(_family>int.MinValue) SetXmlNodeString(familyPath, _family.ToString());
if(_charset>int.MinValue) SetXmlNodeString(charsetPath, _charset.ToString());
if (_scheme != "") SetXmlNodeString(schemePath, _scheme.ToString());

return TopNode;
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# EPPlus
# EPPlus With Font Charset Support
**Announcement: This repository will move to https://github.com/EPPlusSoftware/EPPlus.**
EPPlus will from version 5 switch license from **LGPL** to [Polyform Noncommercial 1.0.0]( https://polyformproject.org/licenses/noncommercial/1.0.0/) license.
With the new license EPPlus is still free to use in some cases, but will require a commercial license to be used in a commercial business.
Expand Down
129 changes: 129 additions & 0 deletions SampleApp.Core/Sample17.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*******************************************************************************
* You may amend and distribute as you like, but don't remove this header!
*
* All rights reserved.
*
* EPPlus is an Open Source project provided under the
* GNU General Public License (GPL) as published by the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* EPPlus provides server-side generation of Excel 2007 spreadsheets.
* See https://github.com/JanKallman/EPPlus for details.
*
*
* The GNU General Public License can be viewed at http://www.opensource.org/licenses/gpl-license.php
* If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
*
* The code for this project may be used and redistributed by any means PROVIDING it is
* not sold for profit without the author's written consent, and providing that this notice
* and the author's name and all copyright notices remain intact.
*
* All code and executables are provided "as is" with no warranty either express or implied.
* The author accepts no liability for any damage or loss of business that this product may cause.
*
*
* Code change notes:
*
* Author Change Date
*******************************************************************************
* Jan Källman Added 10-SEP-2009
*******************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using OfficeOpenXml;
using System.IO;
using System.Data.SqlClient;
using OfficeOpenXml.Drawing;
using OfficeOpenXml.Drawing.Chart;
using OfficeOpenXml.Style;
using System.Drawing;
using OfficeOpenXml.FormulaParsing.Excel.Functions;
using OfficeOpenXml.FormulaParsing.ExpressionGraph;
using OfficeOpenXml.FormulaParsing;
using System.Linq;

namespace EPPlusSamples
{
class EvaluateFunction : ExcelFunction
{
public override CompileResult Execute(IEnumerable<FunctionArgument> arguments, ParsingContext context)
{
ValidateArguments(arguments, 1);
var args = arguments.ToArray();
// renderer.customFunctionEvaluated = true;

double? hasDefaultVal = null;
if (args.Length > 1 && args[1].Value is double)
{
hasDefaultVal = (double)args[1].Value;
}
return new CompileResult(args[0].Value.ToString(), DataType.String);
}
}
class Sample17
{
/// <summary>
/// This sample creates a new workbook from a template file containing a chart and populates it with Exchangrates from
/// the Adventureworks database and set the three series on the chart.
/// </summary>
/// <param name="connectionString">Connectionstring to the Adventureworks db</param>
/// <param name="template">the template</param>
/// <param name="outputdir">output dir</param>
/// <returns></returns>
public static string RunSample17(FileInfo template)
{
using (var stream = File.Open(template.FullName, FileMode.Open))
using (ExcelPackage p = new ExcelPackage(stream))
{
p.Workbook.FormulaParserManager.AddOrReplaceFunction("e", new EvaluateFunction());
var sheet = p.Workbook.Worksheets.FirstOrDefault();
var resultFormula = sheet.Calculate("J23+J21+J13+J14+J22");
//Set up the headers
ExcelWorksheet ws = p.Workbook.Worksheets[0];
for (int i=1; i<= ws.Dimension.Rows; i++)
{
for (int j=1; j<=ws.Dimension.Columns; j++)
{
var cell = ws.Cells[i, j];
if (cell.Merge)
continue;
var val = ws.Cells[i, j].Value;
var formual = ws.Cells[i, j].Formula;
if (!string.IsNullOrEmpty(formual))
{
var result = p.Workbook.FormulaParserManager.Parse(formual);
var val2 = ws.Calculate(formual);
ws.Cells[i, j].Formula = string.Empty;
ws.Cells[i, j].Value = val2;
Console.WriteLine($"{i},{j}: {val} and f={formual} and eval={val2}");
}
}
}
p.Workbook.Worksheets[0].Select();

foreach (var a in ws.MergedCells)
{
var cell = ws.Cells[a];
var formual = cell.Formula;
if (!string.IsNullOrEmpty(formual))
{
var tokens = p.Workbook.FormulaParser.Lexer.Tokenize(formual);
//var result = p.Workbook.FormulaParserManager.Parse();
var val2 = ws.Calculate(formual);
cell.Formula = string.Empty;
cell.Value = val2;
Console.WriteLine($"{cell.Value} and f={formual} and eval={val2}");
}
}
Byte[] bin = p.GetAsByteArray();

FileInfo file = Utils.GetFileInfo("sample17.xlsx");
File.WriteAllBytes(file.FullName, bin);
return file.FullName;
}
}
}
}
2 changes: 1 addition & 1 deletion SampleApp.Core/SampleApp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
13 changes: 12 additions & 1 deletion SampleApp.Core/Sample_Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,21 @@ namespace EPPlusSamples
{
class Sample_Main
{
static void Test()
{
Console.WriteLine("Running sample 4");
var sample17Path = Sample17.RunSample17(new FileInfo(@"C:\Users\mohammad\Documents\Projects\DMS2\codes\frontend\DMS\Api Tests\Planning\Exports\Files\Export2.xlsx")); //Template path from /bin/debug or /bin/release
Console.WriteLine("Sample 17 created: {0}", sample17Path);
Console.WriteLine();
}
static void Main(string[] args)
{

try
{
Utils.OutputDir = new DirectoryInfo($"{AppDomain.CurrentDomain.BaseDirectory}SampleApp");
Test();
return;
//Sample 3, 4 and 12 uses the Adventureworks database. Enter the connectionstring to the Adventureworks database(2016 CTP3) into the variable below...
//Leave this blank if you don't have access to the Adventureworks database
string connectionStr = ""; //for example "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=AdventureWorks2016CTP3;Data Source=MySqlServer"
Expand Down Expand Up @@ -177,7 +188,7 @@ static void Main(string[] args)
}
var prevColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Genereted sample workbooks can be found in {Utils.OutputDir.FullName}");
Console.WriteLine($"Genereted sample workbooks can be found in {Utils.OutputDir?.FullName}");
Console.ForegroundColor = prevColor;

Console.WriteLine();
Expand Down