Does spreadsheetLight has any functionality for creating the excel sheet in Right-to-Left Direction. That is, A column should appear at the right on the excel sheet.
-
One quick search I made, and found something. Not an alignment per column, but per cell. But it may help, so there you go: http://spreadsheetlight.com/downloads/samplecode/Alignment.cs – Renato Afonso Jun 08 '17 at 16:11
-
Thank you for your inputs! It was not what I was looking for though. – Hershika Sharma Jun 10 '17 at 17:24
2 Answers
SpreadSheetLight does persist the RightToLeft property, if you open an existing Workbook with the RightToLeft property set to True you can modify the existing workbook and Save or SaveAs and the result will also be set RightToLeft.
The difficulty is the complexity of something that seems simple since it exists in a single checkbox in Excel, the property is set per sheet view though most are ignored for this value. Since SpreadSheetLight does persist this, it means it does read it and save it back out so you can actually change it; however, it's not exposed so you'll need to use reflection.
Here's a quick and dirty example of how you can read and modify it globally for a Workbook/SLDocument in VB.net, should be easy enough to convert to C#
Imports System.Reflection
Imports System.Runtime.CompilerServices
Imports SpreadsheetLight
Public Module SlDocumentExtender
<Extension>
Public Sub SetRightToLeft(ByVal slDoc As SLDocument, ByVal rightToLeft As Boolean)
Dim sheetViews = GetSheetViews(slDoc.GetSlws)
If Not sheetViews.Any AndAlso rightToLeft Then 'Don't bother creating a default SheetView if false since it's the default
slDoc.FreezePanes(1, 0)
slDoc.UnfreezePanes()
sheetViews = GetSheetViews(slDoc.GetSlws)
End If
sheetViews.ForEach(Sub(sv) SetRightToLeftSheetView(sv, rightToLeft))
End Sub
<Extension>
Public Function GetRightToLeft(ByVal slDoc As SLDocument) As Boolean
Dim sheetViews = GetSheetViews(slDoc.GetSlws)
Return sheetViews.Any(Function(sv) GetRightToLeftSheetView(sv)) 'Empty collection will return false which is the default so we're good
End Function
<Extension>
Public Function GetSlws(ByVal slDoc As SLDocument) As Object 'SLWorksheet
Return slDoc.GetType.GetField("slws", BindingFlags.Instance Or BindingFlags.NonPublic).GetValue(slDoc)
End Function
Private Sub SetRightToLeftSheetView(ByVal sheetView As Object, ByVal rightToLeft As Boolean)
GetRequiredType(sheetView, "SpreadsheetLight.SLSheetView").GetProperty("RightToLeft", BindingFlags.Instance Or BindingFlags.NonPublic).SetValue(sheetView, rightToLeft)
End Sub
Private Function GetRightToLeftSheetView(ByVal sheetView As Object) As Boolean
Return GetRequiredType(sheetView, "SpreadsheetLight.SLSheetView").GetProperty("RightToLeft", BindingFlags.Instance Or BindingFlags.NonPublic).GetValue(sheetView)
End Function
Private Function GetSheetViews(ByVal slws As Object) As List(Of Object) 'List<SLSheetView>
Return DirectCast(GetRequiredType(slws, "SpreadsheetLight.SLWorksheet").GetProperty("SheetViews", BindingFlags.Instance Or BindingFlags.NonPublic).GetValue(slws), IList).OfType(Of Object).ToList()
End Function
Private Function GetRequiredType(ByVal obj As Object, ByVal requiredType_FullName As String) As Type
Dim type = obj.GetType
If Not type.FullName.Equals(requiredType_FullName) Then
Throw New NotSupportedException(String.Format("Type ""{0}"" is not supported, this method only handles only type ""{1}"".", type.FullName, requiredType_FullName))
End If
Return type
End Function
End Module

- 1,086
- 8
- 9
-
Thanks for your response. i have also tried this approach of modifying the right to left excel and then saving after making the changes. However, this approach fails in Arabic system. That is when run the code in Arabic server which has Arabic excel setup. It first shows the excel is corrupted and when i try to repair it it displays the excel again in left to right format. – Hershika Sharma Jun 29 '17 at 05:53
-
Also, i have implemented this feature by saving the excel sheet with data using SpreadSheetLight and then update the RightToLeft property of that excel using openXML library and saving the excel again. – Hershika Sharma Jun 29 '17 at 06:17
I have checked this in depth and came to know that SpreadsheetLight doesn't support Right to left sheet layout at the moment.

- 105
- 1
- 14