Home > .NET > [原创]VB.net 使用XML 对 FpSpread 列宽记忆

[原创]VB.net 使用XML 对 FpSpread 列宽记忆

评分 0.00, 满分 5星 0
Loading...
Go to comments
'需要引入的 命名空间
Imports System.
Imports System.IO

#Region " 读取 写入 生成"
Private Sub createXML(ByVal xmlFileName As String)
	Try
	
		Dim writer As New .XmlTextWriter(xmlFileName, System.Text.Encoding.GetEncoding("utf-8"))
		'使用自动缩进便于阅读
		writer.Formatting = .Formatting.Indented
		writer.WriteRaw("<!--l version=""1.0"" encoding=""utf-8""-->")
		writer.WriteRaw("<!--   Xing   -->")
		'书写根元素()
		writer.WriteStartElement("xx")
		'关闭根元素
		writer.WriteFullEndElement()
		'将XML写入文件并关闭writer
		writer.Close()
	
	Catch ex As System.Exception
		MsgBox(ex.Message & vbCrLf & ex.StackTrace)
	End Try
End Sub

Private Sub _Column_Size_load(ByVal  As FarPoint.Win.Spread.)
	Dim xmlDoc As New XmlDocument
	Dim XmlFileName As String = "C:\XX\INI\_Config." '修改为实际XML路径
	Dim intCol As Integer
	
	Try
	
		If File.Exists(XmlFileName) Then
			xmlDoc.Load(XmlFileName)		
			Dim nodeList As XmlNodeList = xmlDoc.SelectSingleNode("xx").ChildNodes '获取xx节点的所有子节点
			Dim xn As XmlNode
			
			For Each xn In nodeList
				Dim xe As XmlElement = CType(xn, XmlElement)				
				If xe.GetAttribute("form") = Me.Name And xe.GetAttribute("Name") = FpSpread.Name Then				
					Dim nls As XmlNodeList = xe.ChildNodes '继续获取xe子节点的所有子节点
					Dim xn1 As XmlNode
					
					For Each xn1 In nls '遍历
						Dim xe2 As XmlElement = CType(xn1, XmlElement) '转换类型
						
						intCol = xe2.Name.Substring(1, xe2.Name.Length - 1)
						FpSpread.ActiveSheet.Columns(Val(intCol)).Width = Integer.Parse(xe2.InnerText)					
					Next xn1
				
				End If
			
			Next
		End If
		
	Catch ex As System.Exception
		File.Delete(XmlFileName)
	End Try
End Sub

Private Sub FpSpread_Column_Size_save(ByVal FpSpread As FarPoint.Win.Spread.FpSpread)
	Dim xmlDoc As New XmlDocument
	Dim XmlFileName As String = "C:\xx\INI\FpSpread_Config." '修改为实际XML路径
	Dim intRecord As Integer
	
	Try
	If File.Exists(XmlFileName) = False Then
		createXML(XmlFileName)
	End If
	
	xmlDoc.Load(XmlFileName)	
	Dim nodeList As XmlNodeList = xmlDoc.SelectSingleNode("xx").ChildNodes '获取xx节点的所有子节点
	Dim xn As XmlNode
	
		For Each xn In nodeList '遍历所有子节点
			Dim xe As XmlElement = CType(xn, XmlElement) '将子节点类型转换为XmlElement类型
			If xe.GetAttribute("form") = Me.Name And xe.GetAttribute("Name") = FpSpread.Name Then '如果form属性值为“当前窗口”Name为 当前spread 那么就 清空下面的数据
			intRecord += 1
			xe.RemoveAll() '*清空数据
			
				If intRecord < 2 Then
				xe.SetAttribute("form", Me.Name)
				xe.SetAttribute("Name", FpSpread.Name)
				
				Dim child_node As XmlElement
					For i As Integer = 0 To FpSpread.ActiveSheet.Columns.Count - 1
						child_node = xmlDoc.CreateElement("C" & i)
						child_node.InnerText = FpSpread.ActiveSheet.Columns(i).Width
						xe.AppendChild(child_node)
					
					Next
				End If
			End If
		Next xn
	
	If intRecord < 1 Then
		Dim root As XmlNode = xmlDoc.SelectSingleNode("xx")
		Dim xe1 As XmlElement = xmlDoc.CreateElement("spread")
		xe1.SetAttribute("form", Me.Name)
		xe1.SetAttribute("Name", FpSpread.Name)
		
		Dim child_node As XmlElement
			For i As Integer = 0 To FpSpread.ActiveSheet.Columns.Count - 1
			child_node = xmlDoc.CreateElement("C" & i)
			child_node.InnerText = FpSpread.ActiveSheet.Columns(i).Width
			xe1.AppendChild(child_node)
	
		Next
	
		root.AppendChild(xe1) '添加到节点中
	
	End If
	
	xmlDoc.Save(XmlFileName) '保存。
	Catch ex As System.Exception
	
	End Try
	
End Sub
#End Region

#Region "Form Load"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	'spread load
	FpSpread_Column_Size_load(Me.spListMain)
End Sub
#End Region

#Region "form 关闭"
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
	FpSpread_Column_Size_save(Me.spListMain)
End Sub
#End Region

生成的XML文件内如如下

<?xml version="1.0" encoding="utf-8"?>
<!--   Xing   -->
<xx>
  <spread form="FORM1" Name="spListMain">
    <C0>60</C0>
    <C1>90</C1>
    <C2>106</C2>
    <C3>60</C3>
    <C4>80</C4>
    <C5>60</C5>
    <C6>178</C6>
    <C7>60</C7>
    <C8>119</C8>
    <C9>82</C9>
    <C10>145</C10>
    <C11>88</C11>
    <C12>123</C12>
    <C13>60</C13>
  </spread>
  <spread form="FORM1" Name="spListList">
    <C0>17</C0>
    <C1>65</C1>
    <C2>65</C2>
    <C3>31</C3>
    <C4>60</C4>
    <C5>65</C5>
    <C6>118</C6>
    <C7>69</C7>
    <C8>62</C8>
    <C9>71</C9>
    <C10>34</C10>
    <C11>52</C11>
    <C12>58</C12>
    <C13>58</C13>
    <C14>59</C14>
    <C15>106</C15>
    <C16>34</C16>
    <C17>34</C17>
  </spread>
  <spread form="FORM1" Name="spList1">
    <C0>60</C0>
    <C1>60</C1>
    <C2>60</C2>
    <C3>32</C3>
    <C4>60</C4>
    <C5>71</C5>
    <C6>60</C6>
    <C7>60</C7>
    <C8>60</C8>
    <C9>60</C9>
    <C10>60</C10>
    <C11>72</C11>
    <C12>68</C12>
    <C13>70</C13>
    <C14>60</C14>
    <C15>60</C15>
    <C16>60</C16>
    <C17>60</C17>
    <C18>60</C18>
    <C19>60</C19>
    <C20>60</C20>
    <C21>60</C21>
    <C22>65</C22>
  </spread>
</xx>
声明: 本文采用 BY-NC-SA 协议进行授权. 转载请注明转自: [原创]VB.net 使用XML 对 FpSpread 列宽记忆
Subscribe to comments feed

  1. 知道你的英文名字了,嘻嘻

  1. Loading...