分享

表格数据导到Txt、Excel

 *蓝星* 2019-07-20

'表格数据导到Txt

    Private Sub ToolStripButtonExpTxt_Click(sender As Object, e As EventArgs) Handles ToolMain_ButtonExpTxt.Click

        Dim cFileName As String = "D:\MyProg\" & "连板" & Date.Now.Month.ToString & Date.Now.Day.ToString & ".txt"

        Dim oFile As New StreamWriter(cFileName)

        Dim strTemp As String = Nothing

        Try

            For r = 0 To DGV_GpHqFind.RowCount - 1

                strTemp = ""

                For c = 0 To DGV_GpHqFind.Columns.Count - 1

                    strTemp &= DGV_GpHqFind(c, r).Value & ","

                Next

                oFile.WriteLine(strTemp)

            Next

            MsgBox("数据已导到:" & cFileName, 64, "信息提示")

            oFile.Close()

        Catch ex As Exception

            MsgBox(ex.ToString, 48, "错误信息")

        End Try

        oFile = Nothing

    End Sub

    '表格数据导到Excel

    Private Sub ToolStripButtonExpExcel_Click(sender As Object, e As EventArgs) Handles ToolMain_ButtonExpExcel.Click

        Dim oExce As Excel.Application

        Dim oWork As Excel.Workbook

        Dim oSh As Excel.Worksheet

        Dim oRng As Excel.Range

        Try

            Dim cTitle As String = "代码,名称,今开,昨收,今收,涨跌,涨幅%,最高,最低,成交量,成交额"

            Dim aTitle As Array = cTitle.Split(",")

            ' Start Excel and get Application object.

            oExce = CreateObject("Excel.Application")

            oExce.Visible = True

            ' Add a new workbook.

            oWork = oExce.Workbooks.Add

            oSh = oWork.ActiveSheet

            For c As Integer = 0 To aTitle.Length - 1

                oSh.Cells(1, c + 1).Value = aTitle(c)

            Next

            oExce.ScreenUpdating = False

            ' Format A1:D1 as bold, vertical alignment = center.

            oSh.Range("A:B").NumberFormatLocal = "@"

            For r = 0 To Me.DGV_GpHqFind.RowCount - 1

                For c = 0 To Me.DGV_GpHqFind.ColumnCount - 1

                    With oSh

                        If c < 2 Then

                            .Cells(r + 2, c + 1).Value = Me.DGV_GpHqFind.Item(c, r).Value.ToString

                        Else

                            .Cells(r + 2, c + 1).Value = Me.DGV_GpHqFind.Item(c, r).Value

                        End If

                    End With

                Next

            Next

            With oSh.Range("A1", "K" & DGV_GpHqFind.RowCount + 1)

                .Font.Name = "微软雅黑"

                .Font.Size = 11

            End With

            With oSh.Range("A1", "K1")

                .Font.Bold = True

                .VerticalAlignment = Excel.XlVAlign.xlVAlignCenter

                .EntireColumn.AutoFit()

            End With

            oExce.ScreenUpdating = True

            ' Make sure Excel is visible and give the user control

            ' of Excel's lifetime.

            ' Release object references.

            'oExce.Visible = True

            oRng = Nothing

            oSh = Nothing

            oWork = Nothing

            oExce.Quit()

            oExce = Nothing

        Catch ex As Exception

            MsgBox(ex.ToString, 48, "错误信息")

        End Try

    End Sub

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多