📄 form1.vb
字号:
Case 2 '三次样条插值
Me.DataGridView3.Visible = False
Dim x(200) As Single
Dim y(200) As Single
Dim mFunction As New MyFunction
For i As Integer = 0 To 200
If Me.DataGridView1.Rows(i).Cells(0).Value <> "" And Me.DataGridView1.Rows(i).Cells(1).Value <> "" Then
x(i) = Convert.ToSingle(Me.DataGridView1.Rows(i).Cells(0).Value)
y(i) = Convert.ToSingle(Me.DataGridView1.Rows(i).Cells(1).Value)
Else
Exit For
End If
Next
ReDim Preserve x(num - 1)
ReDim Preserve y(num - 1)
'插值计算
For j As Integer = 0 To 200
If Me.DataGridView2.Rows(j).Cells(0).Value <> "" Then
Dim ChaZhiX As Single = Convert.ToSingle(Me.DataGridView2.Rows(j).Cells(0).Value)
Me.DataGridView2.Rows(j).Cells(1).Value = mFunction.spline(x, y, ChaZhiX, 5).ToString(".###")
End If
Next
'在excel中绘图
MyChart = MyExcelApp.Charts.Add()
MyChart.ChartType = Excel.XlChartType.xlXYScatterSmooth
MyChart.PlotArea.Width = 560
MyChart.PlotArea.Height = 400
MyChart.PlotArea.Top = 10
MyChart.PlotArea.Left = 80
Dim mySeriesCollection As Excel.SeriesCollection
mySeriesCollection = MyChart.SeriesCollection
Dim mySeries As Excel.Series
mySeries = mySeriesCollection.Item(1)
mySeries.XValues = "=Sheet1!R2C1:R201C1"
mySeries.Values = "=Sheet1!R2C2:R201C2"
mySeries.Name = "=""原始数据"""
Dim x1(200) As String
Dim y1(200) As String
Dim strx As String = ""
Dim stry As String = ""
Dim temp As Integer = 0
For row As Integer = 0 To 200
If Me.DataGridView2.Rows(row).Cells(0).Value <> "" And Me.DataGridView2.Rows(row).Cells(1).Value <> "" Then
x1(row) = Me.DataGridView2.Rows(row).Cells(0).Value
y1(row) = Me.DataGridView2.Rows(row).Cells(1).Value
Else
temp = row - 1
Exit For
End If
Next
strx = "{" & x1(0)
stry = "{" & y1(0)
For row1 As Integer = 1 To temp
strx = strx & "," & x1(row1)
stry = stry & "," & y1(row1)
Next
strx = strx & "}"
stry = stry & "}"
If strx <> "{}" And stry <> "{}" Then
Dim mySeries1 As Excel.Series
mySeries1 = mySeriesCollection.NewSeries
mySeries1.Border.LineStyle = Excel.XlLineStyle.xlLineStyleNone
mySeries1.Name = "=""插值点"""
mySeries1.XValues = strx
mySeries1.Values = stry
End If
MyChart.Legend.Top = 340
MyChart.Legend.Left = 525
MyChart.Location(Where:=Excel.XlChartLocation.xlLocationAsNewSheet, Name:="插值与拟合")
MyExcelApp.ActiveWindow.Zoom = 100
MyExcelSheet.Name = "原始数据"
End Select
My.Computer.Clipboard.Clear() '先清空clipboard。
Drawing() '复制图表到clipboard,并把它赋值给picturebox。
' MyExcelApp.Visible = True
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub 清除数据ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 清除数据ToolStripMenuItem.Click
For i As Integer = 0 To 100
Me.DataGridView1.Rows(i).Cells(0).Value = ""
Me.DataGridView1.Rows(i).Cells(1).Value = ""
Next
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.ToolStripStatusLabel1.Text = My.Computer.Clock.LocalTime.ToString
End Sub
Sub CloseWorkBook() '关闭正在隐藏的活动的工作簿实例
For Each MyExcelBook In MyExcelApp.Workbooks
If MyExcelBook.Application.Visible = False Then
MyExcelApp.DisplayAlerts = False
MyExcelBook.Close()
MyExcelBook = Nothing
End If
Next
MyExcelApp.DisplayAlerts = True
End Sub
Sub Drawing()
Try
Dim odataobj As IDataObject = My.Computer.Clipboard.GetDataObject
'唉,下面复制图表的语句终于不出现异常了。但是就是不明白mychart.copypicture就引发异常呢?
MyExcelApp.ActiveChart.CopyPicture(Excel.XlPictureAppearance.xlScreen, _
Excel.XlCopyPictureFormat.xlBitmap, Excel.XlPictureAppearance.xlScreen)
' 开心,下面的语句终于成功了。
' My.Computer.Clipboard.GetImage.Save("E:\123.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
Me.PictureBox1.Image = My.Computer.Clipboard.GetImage
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
'读取计算所需数据文件
Sub ReadData(ByVal strOfFileName As String, ByVal drv As DataGridView)
Dim defaultEncoding As Encoding
defaultEncoding = Encoding.Default
'使用系统默认的编码,防止出现乱码的情况。
Dim myReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(strOfFileName, defaultEncoding)
' myReader = My.Computer.FileSystem.OpenTextFieldParser(strOfFileName)
'表示文件的内容是字节分隔。
myReader.TextFieldType = FileIO.FieldType.Delimited
'定义文本文件的字节分隔符号。
myReader.Delimiters = New String() {vbTab}
Dim CurrentRow(10) As String
'循环处理文本文件中的所有字段。
Dim num As Integer = 0
While Not myReader.EndOfData
Try
CurrentRow = myReader.ReadFields() '读取一行数据
If CurrentRow.GetUpperBound(0) > 0 Then
drv.Rows(num).Cells(0).Value = CurrentRow(0)
drv.Rows(num).Cells(1).Value = CurrentRow(1)
num += 1
ElseIf CurrentRow.GetUpperBound(0) = 0 Then
drv.Rows(num).Cells(0).Value = CurrentRow(0)
num += 1
End If
Catch ex As Exception
MessageBox.Show("数据格式有误!")
Exit Sub
End Try
End While
End Sub
Private Sub 导入数据ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 导入数据ToolStripMenuItem.Click
If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
ReadData(Me.OpenFileDialog1.FileName, Me.DataGridView1)
End If
End Sub
Private Sub 清除数据ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 清除数据ToolStripMenuItem1.Click
For i As Integer = 0 To 100
Me.DataGridView2.Rows(i).Cells(0).Value = ""
Me.DataGridView2.Rows(i).Cells(1).Value = ""
Next
End Sub
Private Sub 导入数据ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 导入数据ToolStripMenuItem1.Click
If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
ReadData(Me.OpenFileDialog1.FileName, Me.DataGridView2)
End If
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -