简体   繁体   中英

SpreadsheetLight c#

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.

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

我对此进行了深入检查,发现SpreadsheetLight目前不支持从右到左的页面布局。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM