diff --git a/ClosedXML/Excel/Tables/XLTable.cs b/ClosedXML/Excel/Tables/XLTable.cs index a090dfc..5e8f3a6 100644 --- a/ClosedXML/Excel/Tables/XLTable.cs +++ b/ClosedXML/Excel/Tables/XLTable.cs @@ -65,7 +65,7 @@ foreach (var cell in headersRow.Cells()) { var name = cell.GetString(); - if (XLHelper.IsNullOrWhiteSpace(name)) + if (XLHelper.IsNullOrWhiteSpace(name)) { name = "Column" + (cellPos + 1); cell.SetValue(name); @@ -73,7 +73,7 @@ if (_fieldNames.ContainsKey(name)) throw new ArgumentException("The header row contains more than one field name '" + name + "'."); - _fieldNames.Add(name, new XLTableField(this) {Index = cellPos++ }); + _fieldNames.Add(name, new XLTableField(this, name) {Index = cellPos++ }); } } else @@ -87,7 +87,7 @@ { var name = "Column" + i; - _fieldNames.Add(name, new XLTableField(this) {Index = i - 1 }); + _fieldNames.Add(name, new XLTableField(this, name) {Index = i - 1 }); } } } @@ -102,7 +102,7 @@ Int32 cellPos = 0; foreach(var name in fieldNames) { - _fieldNames.Add(name, new XLTableField(this) { Index = cellPos++, Name = name }); + _fieldNames.Add(name, new XLTableField(this, name) { Index = cellPos++ }); } } @@ -164,8 +164,8 @@ public Boolean ShowColumnStripes { get; set; } private Boolean _showAutoFilter; - public Boolean ShowAutoFilter { - get { return _showHeaderRow && _showAutoFilter; } + public Boolean ShowAutoFilter { + get { return _showHeaderRow && _showAutoFilter; } set { _showAutoFilter = value; } } public XLTableTheme Theme { get; set; } @@ -450,7 +450,7 @@ _uniqueNames.Add(c.GetString()); co++; } - + headersRow.Clear(); RangeAddress.FirstAddress = new XLAddress(Worksheet, RangeAddress.FirstAddress.RowNumber + 1, RangeAddress.FirstAddress.ColumnNumber, @@ -462,7 +462,7 @@ else { using(var asRange = Worksheet.Range( - RangeAddress.FirstAddress.RowNumber - 1 , + RangeAddress.FirstAddress.RowNumber - 1 , RangeAddress.FirstAddress.ColumnNumber, RangeAddress.LastAddress.RowNumber, RangeAddress.LastAddress.ColumnNumber @@ -473,7 +473,7 @@ if (firstRow.IsEmpty(true)) { rangeRow = firstRow; - RangeAddress.FirstAddress = new XLAddress(Worksheet, + RangeAddress.FirstAddress = new XLAddress(Worksheet, RangeAddress.FirstAddress.RowNumber - 1, RangeAddress.FirstAddress.ColumnNumber, RangeAddress.FirstAddress.FixedRow, @@ -499,7 +499,7 @@ } Int32 co = 1; - foreach (var name in FieldNames.Keys) + foreach (var name in FieldNames.Values.Select(f => f.Name)) { rangeRow.Cell(co).SetValue(name); co++; @@ -513,7 +513,7 @@ public IXLTable SetShowHeaderRow() { return SetShowHeaderRow(true); - } + } public IXLTable SetShowHeaderRow(Boolean value) { ShowHeaderRow = value; @@ -529,4 +529,4 @@ } } -} \ No newline at end of file +} diff --git a/ClosedXML/Excel/Tables/XLTableField.cs b/ClosedXML/Excel/Tables/XLTableField.cs index 0f98646..19e9655 100644 --- a/ClosedXML/Excel/Tables/XLTableField.cs +++ b/ClosedXML/Excel/Tables/XLTableField.cs @@ -5,22 +5,28 @@ internal class XLTableField: IXLTableField { private XLTable table; - public XLTableField(XLTable table) + public XLTableField(XLTable table, String name) { this.table = table; + this.name = name; } public Int32 Index { get; internal set; } - public String Name + private String name; + + public String Name { get { - return table.HeadersRow().Cell(Index + 1).GetString(); + return name; } set { - table.HeadersRow().Cell(Index + 1).SetValue(value); + if (table.ShowHeaderRow) + table.HeadersRow().Cell(Index + 1).SetValue(value); + + name = value; } } @@ -28,18 +34,18 @@ public String TotalsRowLabel { get { return totalsRowLabel; } - set + set { totalsRowFunction = XLTotalsRowFunction.None; table.TotalsRow().Cell(Index + 1).SetValue(value); - totalsRowLabel = value; + totalsRowLabel = value; } } public String TotalsRowFormulaA1 { get { return table.TotalsRow().Cell(Index + 1).FormulaA1; } - set + set { totalsRowFunction = XLTotalsRowFunction.Custom; table.TotalsRow().Cell(Index + 1).FormulaA1 = value; @@ -59,7 +65,7 @@ public XLTotalsRowFunction TotalsRowFunction { get { return totalsRowFunction; } - set + set { if (value != XLTotalsRowFunction.None && value != XLTotalsRowFunction.Custom) { @@ -85,7 +91,7 @@ cell.Style.NumberFormat = lastCell.Style.NumberFormat; } } - totalsRowFunction = value; + totalsRowFunction = value; } } } diff --git a/ClosedXML/Excel/XLWorkbook_Load.cs b/ClosedXML/Excel/XLWorkbook_Load.cs index 6aa64ab..d3b05dc 100644 --- a/ClosedXML/Excel/XLWorkbook_Load.cs +++ b/ClosedXML/Excel/XLWorkbook_Load.cs @@ -153,7 +153,7 @@ differentialFormats = s.DifferentialFormats.Elements().ToDictionary(k => dfCount++); else differentialFormats = new Dictionary(); - + var sheets = dSpreadsheet.WorkbookPart.Workbook.Sheets; Int32 position = 0; foreach (Sheet dSheet in sheets.OfType()) @@ -304,7 +304,7 @@ } else xlTable.ShowAutoFilter = false; - + if (xlTable.ShowTotalsRow) { @@ -342,11 +342,11 @@ // **** MAYBE FUTURE SHAPE SIZE SUPPORT XDocument xdoc = GetCommentVmlFile(wsPart); - + foreach (Comment c in comments) { // find cell by reference var cell = ws.Cell(c.Reference); - + XLComment xlComment = cell.Comment as XLComment; xlComment.Author = authors[(int)c.AuthorId.Value].InnerText; //xlComment.ShapeId = (Int32)c.ShapeId.Value; @@ -360,9 +360,9 @@ LoadFont(runProperties, rt); } - + XElement shape = GetCommentShape(xdoc); - + LoadShapeProperties(xlComment, shape); var clientData = shape.Elements().First(e => e.Name.LocalName == "ClientData"); @@ -378,7 +378,7 @@ //var insetmode = (string)shape.Attributes().First(a=> a.Name.LocalName == "insetmode"); //xlComment.Style.Margins.Automatic = insetmode != null && insetmode.Equals("auto"); - + shape.Remove(); } } @@ -466,7 +466,7 @@ { return XLColor.FromHtml(color); } - + } private void LoadColorsAndLines(IXLDrawing drawing, XElement shape) @@ -489,7 +489,7 @@ { String opacityVal = opacity.Value; if (opacityVal.EndsWith("f")) - drawing.Style.ColorsAndLines.FillTransparency = + drawing.Style.ColorsAndLines.FillTransparency = Double.Parse(opacityVal.Substring(0, opacityVal.Length - 1), CultureInfo.InvariantCulture) / 65536.0; else drawing.Style.ColorsAndLines.FillTransparency = Double.Parse(opacityVal, CultureInfo.InvariantCulture); @@ -504,7 +504,7 @@ { String opacityVal = opacity.Value; if (opacityVal.EndsWith("f")) - drawing.Style.ColorsAndLines.LineTransparency = + drawing.Style.ColorsAndLines.LineTransparency = Double.Parse(opacityVal.Substring(0, opacityVal.Length - 1), CultureInfo.InvariantCulture) / 65536.0; else drawing.Style.ColorsAndLines.LineTransparency = Double.Parse(opacityVal, CultureInfo.InvariantCulture); @@ -522,7 +522,7 @@ else drawing.Style.ColorsAndLines.LineDash = XLDashStyle.SquareDot; } - else + else { switch (dashStyleVal) { @@ -711,7 +711,7 @@ return Double.Parse(value.Replace(knownUnit.Key, String.Empty), CultureInfo.InvariantCulture) * knownUnit.Value; } - + private void LoadDefinedNames(Workbook workbook) { @@ -911,7 +911,7 @@ if (!hasRuns) hasRuns = true; } - + if(!hasRuns) xlCell._cellValue = sharedString.Text.InnerText; @@ -974,7 +974,7 @@ else xlCell._dataType = XLCellValues.Number; } - + } } else if (cell.CellValue != null) @@ -1039,7 +1039,7 @@ LoadBorderValues(borderSource.RightBorder, border.SetRightBorder, border.SetRightBorderColor); LoadBorderValues(borderSource.TopBorder, border.SetTopBorder, border.SetTopBorderColor); LoadBorderValues(borderSource.BottomBorder, border.SetBottomBorder, border.SetBottomBorderColor); - + } private void LoadBorderValues(BorderPropertiesType source, Func setBorder, Func setColor ) @@ -1053,7 +1053,7 @@ } } - + private void LoadFill(Fill fillSource, IXLFill fill) { @@ -1111,7 +1111,7 @@ } var verticalTextAlignment = fontSource.Elements().FirstOrDefault(); - + if (verticalTextAlignment == null) return; fontBase.VerticalAlignment = verticalTextAlignment.Val != null ? verticalTextAlignment.Val.Value.ToClosedXml() : XLFontVerticalTextAlignmentValues.Baseline; @@ -1125,7 +1125,7 @@ { Int32 rowIndex = row.RowIndex == null ? ++lastRow : (Int32) row.RowIndex.Value; var xlRow = ws.Row(rowIndex, false); - + if (row.Height != null) xlRow.Height = row.Height; else @@ -1492,7 +1492,7 @@ conditionalFormat.ShowIconOnly = !iconSet.ShowValue.Value; if (iconSet.Reverse != null) conditionalFormat.ReverseIconOrder = iconSet.Reverse.Value; - + if (iconSet.IconSetValue != null) conditionalFormat.IconSetStyle = iconSet.IconSetValue.Value.ToClosedXml(); else @@ -1504,7 +1504,7 @@ { foreach (var formula in fr.Elements()) { - if (formula.Text != null + if (formula.Text != null && (conditionalFormat.ConditionalFormatType == XLConditionalFormatType.CellIs || conditionalFormat.ConditionalFormatType == XLConditionalFormatType.Expression)) { @@ -1515,7 +1515,7 @@ ws.ConditionalFormats.Add(conditionalFormat); } } - + } private static XLFormula GetFormula(String value) @@ -1523,7 +1523,7 @@ var formula = new XLFormula(); formula._value = value; formula.IsFormula = !(value[0] == '"' && value.EndsWith("\"")); - return formula; + return formula; } private void ExtractConditionalFormatValueObjects(XLConditionalFormat conditionalFormat, OpenXmlElement element) @@ -1731,6 +1731,7 @@ if (sheetView == null) return; + if (sheetView.RightToLeft != null) ws.RightToLeft = sheetView.RightToLeft.Value; if (sheetView.ShowFormulas != null) ws.ShowFormulas = sheetView.ShowFormulas.Value; if (sheetView.ShowGridLines != null) ws.ShowGridLines = sheetView.ShowGridLines.Value; if (sheetView.ShowOutlineSymbols != null) @@ -1983,10 +1984,10 @@ } } - + if (!UInt32HasValue(cellFormat.NumberFormatId)) return; - + var numberFormatId = cellFormat.NumberFormatId; string formatCode = String.Empty; @@ -2024,4 +2025,4 @@ return false; } } -} \ No newline at end of file +} diff --git a/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/Excel/XLWorkbook_Save.cs index 2e69e73..c4135aa 100644 --- a/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/Excel/XLWorkbook_Save.cs @@ -3432,6 +3432,10 @@ else sheetView.TabSelected = null; + if (xlWorksheet.RightToLeft) + sheetView.RightToLeft = true; + else + sheetView.RightToLeft = null; if (xlWorksheet.ShowFormulas) sheetView.ShowFormulas = true; diff --git a/ClosedXML_Examples/ClosedXML_Examples.csproj b/ClosedXML_Examples/ClosedXML_Examples.csproj index 5012f6d..6603cc8 100644 --- a/ClosedXML_Examples/ClosedXML_Examples.csproj +++ b/ClosedXML_Examples/ClosedXML_Examples.csproj @@ -73,6 +73,7 @@ + diff --git a/ClosedXML_Examples/Creating/CreateFiles.cs b/ClosedXML_Examples/Creating/CreateFiles.cs index 2b94f60..4a17302 100644 --- a/ClosedXML_Examples/Creating/CreateFiles.cs +++ b/ClosedXML_Examples/Creating/CreateFiles.cs @@ -67,6 +67,7 @@ new FreezePanes().Create(Path.Combine(path, "FreezePanes.xlsx")); new UsingTables().Create(Path.Combine(path, "UsingTables.xlsx")); new AddingRowToTables().Create(Path.Combine(path, "AddingRowToTables.xlsx")); + new RightToLeft().Create(Path.Combine(path, "RightToLeft.xlsx")); new ShowCase().Create(Path.Combine(path, "ShowCase.xlsx")); new CopyingWorksheets().Create(Path.Combine(path, "CopyingWorksheets.xlsx")); new InsertingTables().Create(Path.Combine(path, "InsertingTables.xlsx")); diff --git a/ClosedXML_Examples/Misc/RightToLeft.cs b/ClosedXML_Examples/Misc/RightToLeft.cs new file mode 100644 index 0000000..c9a5fa8 --- /dev/null +++ b/ClosedXML_Examples/Misc/RightToLeft.cs @@ -0,0 +1,21 @@ +using ClosedXML.Excel; +using System; + +namespace ClosedXML_Examples.Misc +{ + public class RightToLeft : IXLExample + { + public void Create(String filePath) + { + var wb = new XLWorkbook(); + + var ws = wb.Worksheets.Add("RightToLeftSheet"); + ws.Cell("A1").Value = "A1"; + ws.Cell("B1").Value = "B1"; + ws.Cell("C1").Value = "C1"; + ws.RightToLeft = true; + + wb.SaveAs(filePath); + } + } +} diff --git a/ClosedXML_Tests/ClosedXML_Tests.csproj b/ClosedXML_Tests/ClosedXML_Tests.csproj index d4d5ba6..5ea0151 100644 --- a/ClosedXML_Tests/ClosedXML_Tests.csproj +++ b/ClosedXML_Tests/ClosedXML_Tests.csproj @@ -248,6 +248,7 @@ + @@ -259,4 +260,4 @@ --> - \ No newline at end of file + diff --git a/ClosedXML_Tests/Examples/MiscTests.cs b/ClosedXML_Tests/Examples/MiscTests.cs index 38f7e8e..04a1ede 100644 --- a/ClosedXML_Tests/Examples/MiscTests.cs +++ b/ClosedXML_Tests/Examples/MiscTests.cs @@ -152,6 +152,12 @@ } [Test] + public void RightToLeft() + { + TestHelper.RunTestExample(@"Misc\RightToLeft.xlsx"); + } + + [Test] public void SheetProtection() { TestHelper.RunTestExample(@"Misc\SheetProtection.xlsx"); @@ -181,4 +187,4 @@ TestHelper.RunTestExample(@"Misc\WorkbookProperties.xlsx"); } } -} \ No newline at end of file +} diff --git a/ClosedXML_Tests/Excel/Tables/TablesTests.cs b/ClosedXML_Tests/Excel/Tables/TablesTests.cs index c5c392b..a3eb678 100644 --- a/ClosedXML_Tests/Excel/Tables/TablesTests.cs +++ b/ClosedXML_Tests/Excel/Tables/TablesTests.cs @@ -1,10 +1,10 @@ -using System; +using ClosedXML.Excel; +using NUnit.Framework; +using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; -using ClosedXML.Excel; -using NUnit.Framework; namespace ClosedXML_Tests.Excel { @@ -261,9 +261,13 @@ .CellBelow().SetValue("B") .CellBelow().SetValue("C"); - ws.RangeUsed().CreateTable().SetShowHeaderRow(false); + IXLTable table = ws.RangeUsed().CreateTable(); - IXLTable table = ws.Tables.First(); + Assert.AreEqual("Categories", table.Fields.First().Name); + + table.SetShowHeaderRow(false); + + Assert.AreEqual("Categories", table.Fields.First().Name); Assert.IsTrue(ws.Cell(1, 1).IsEmpty(true)); Assert.AreEqual(null, table.HeadersRow()); @@ -277,7 +281,6 @@ Assert.AreNotEqual(null, headerRow); Assert.AreEqual("Categories", headerRow.Cell(1).GetString()); - table.SetShowHeaderRow(false); ws.FirstCell().SetValue("x"); @@ -315,6 +318,15 @@ Assert.AreEqual("LName", nameBefore); Assert.AreEqual("LastName", nameAfter); Assert.AreEqual("LastName", cellValue); + + tbl.ShowHeaderRow = false; + tbl.Field(tbl.Fields.Last().Index).Name = "LastNameChanged"; + nameAfter = tbl.Field(tbl.Fields.Last().Index).Name; + Assert.AreEqual("LastNameChanged", nameAfter); + + tbl.SetShowHeaderRow(true); + nameAfter = tbl.Cell("B1").Value.ToString(); + Assert.AreEqual("LastNameChanged", nameAfter); } } } diff --git a/ClosedXML_Tests/Resource/Examples/Misc/RightToLeft.xlsx b/ClosedXML_Tests/Resource/Examples/Misc/RightToLeft.xlsx new file mode 100644 index 0000000..1faf041 --- /dev/null +++ b/ClosedXML_Tests/Resource/Examples/Misc/RightToLeft.xlsx Binary files differ