none
[VB2008]如何將DataGridview的內容採用Unicode的格式寫到txt檔 RRS feed

  • 問題

  • 小弟目前把DataGridview的內容寫到txt檔的程式如下:

            Dim table As DataTable
            Dim row As DataRow
            Dim column As DataColumn
            Dim Filename As String = SaveFileInfo.Text
            Dim [Unicode] As Encoding = Encoding.Unicode

     

            Using FileSave As StreamWriter = File.CreateText(SaveFileInfo.Text)


                For Each table In mydataset.Tables

                    For Each colObject In table.Columns
                        FileSave.WriteLine(colObject.caption & ";", [Unicode])
                    Next

                    FileSave.WriteLine()

                    For Each row In table.Rows

                        For Each column In table.Columns

                            FileSave.WriteLine(row(column).ToString() & ";", [Unicode])

                        Next column
                        FileSave.WriteLine()
                    Next row

                Next table

            End Using

            Sub_Form.Dispose()

    但是寫出來的txt檔是UTF-8的格式

    若要將其改成Unicode的格式

    不知該怎麼修改呢??

    看了很多資料

    幾乎都是從紅色字的地方改的

    但是如果改成紅色字的程式碼

    儲存的txt檔卻不是我要的

    頭大中...

    請各位大大指導一下

    謝謝!!

    2009年1月8日 上午 05:51

解答

  • 搞定了...

    參考StreamWriter 建構函式 (Stream, Encoding)

    把程式改寫成以下程式碼:

            Dim table As DataTable
            Dim row As DataRow
            Dim column As DataColumn
            Dim Filename As FileStream = New FileStream(SaveFileInfo.Text, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)

            Using FileSave As StreamWriter = New StreamWriter(Filename, System.Text.Encoding.Unicode)


                For Each table In mydataset.Tables

                    For Each colObject In table.Columns
                        FileSave.Write(colObject.caption & ";")
                    Next

                    FileSave.WriteLine()

                    For Each row In table.Rows

                        For Each column In table.Columns

                            FileSave.Write(row(column).ToString() & ";")

                        Next column
                        FileSave.WriteLine()
                    Next row

                Next table

            End Using

            Sub_Form.Dispose()

    紅色的部分就是改寫的部分

    這樣就可以指定開啟的txt檔案為Unicode格式而把資料寫進去了...

    2009年1月8日 上午 07:52

所有回覆

  • 剛剛看了一下

    在這一行:

    Using FileSave As StreamWriter = File.CreateText(SaveFileInfo.Text)

    開啟檔案的時候就是採用UTF-8的格式開啟的

    不知有無方法改成用Unicod的格式開啟呢??

    2009年1月8日 上午 06:26
  • 搞定了...

    參考StreamWriter 建構函式 (Stream, Encoding)

    把程式改寫成以下程式碼:

            Dim table As DataTable
            Dim row As DataRow
            Dim column As DataColumn
            Dim Filename As FileStream = New FileStream(SaveFileInfo.Text, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)

            Using FileSave As StreamWriter = New StreamWriter(Filename, System.Text.Encoding.Unicode)


                For Each table In mydataset.Tables

                    For Each colObject In table.Columns
                        FileSave.Write(colObject.caption & ";")
                    Next

                    FileSave.WriteLine()

                    For Each row In table.Rows

                        For Each column In table.Columns

                            FileSave.Write(row(column).ToString() & ";")

                        Next column
                        FileSave.WriteLine()
                    Next row

                Next table

            End Using

            Sub_Form.Dispose()

    紅色的部分就是改寫的部分

    這樣就可以指定開啟的txt檔案為Unicode格式而把資料寫進去了...

    2009年1月8日 上午 07:52