简体   繁体   English

SpreadsheetLight c#

[英]SpreadsheetLight c#

Does spreadsheetLight has any functionality for creating the excel sheet in Right-to-Left Direction . 电子表格 Light是否具有在从右到左的方向上创建Excel工作表的任何功能。 That is, A column should appear at the right on the excel sheet. 也就是说, 应出现在excel表格的右侧。

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. SpreadSheetLight确实保留RightToLeft属性,如果您将RightToLeft属性设置为True打开现有工作簿,则可以修改现有工作簿以及Save或SaveAs,结果也将设置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. 困难之处在于某些事情似乎很简单,因为它存在于Excel中的单个复选框中,尽管每个值都忽略了大多数属性,但每个工作表视图都设置了该属性。 Since SpreadSheetLight does persist this, it means it does read it and save it back out so you can actually change it; 由于SpreadSheetLight确实保留了此内容,因此意味着它确实读取并保存了该内容,因此您可以进行实际更改。 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# 这是一个快速而又肮脏的示例,说明如何在VB.net中为Workbook / SLDocument全局读取和修改它,应该足够容易地转换为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目前不支持从右到左的页面布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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