将Excel中的所有列格式化为文本

时间:2013-12-04 16:04:18

标签: vb.net excel

我正在VB.NET上开发一个非常基本的应用程序。

功能:要导入分隔的TXT文件并执行" TextToColumn",Column-AutoFit,将所有列格式化为TEXT,最后保存为Excel。

我面临的挑战:除了所有列都没有格式化为TEXT之外,我能够编码所有内容。因此,包含16位数字的列将保存在科学记数法中。

代码:

Imports Excel = Microsoft.Office.Interop.Excel
Imports System.IO
Public Class Form1
    Dim xlApp As New Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet
    Dim style As Microsoft.Office.Interop.Excel.Style
    Dim strFileName As String
    Dim pathFile As String
    Dim FileNameOnly As String
    Dim saveAsPath As String
    Dim delimiterType As String
    Dim fd As OpenFileDialog = New OpenFileDialog()
    Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Button2.Text = "Browse A Delimited File"
        Button1.Text = "Format and Save In Excel"
        Me.Text = "Delimiter To Excel - DEMO v1"
    End Sub

    Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            If RadioButton1.Checked = True Then
                delimiterType = RadioButton1.Text
            Else
                delimiterType = RadioButton2.Text
            End If
            xlWorkBook = xlApp.Workbooks.Open(strFileName)
            xlWorkSheet = xlWorkBook.Sheets(1)
            xlApp.Visible = False

            With xlWorkSheet
                .Columns(1).TextToColumns( _
                Destination:=.Cells(1, 1), _
                DataType:=Excel.XlTextParsingType.xlDelimited, _
                ConsecutiveDelimiter:=False, _
                TAB:=False, _
                Semicolon:=False, _
                Comma:=False, _
                Space:=False, _
                Other:=True, _
                OtherChar:=delimiterType, _
                FieldInfo:= <What_to_Fill_Here_to_Format_Every_Column_as_Text>
                TrailingMinusNumbers:=False)
            End With
        Catch ex As Exception
            MsgBox("Something is Wrong")
        End Try
        pathFile = Path.GetDirectoryName(fd.FileName)
        FileNameOnly = System.IO.Path.GetFileNameWithoutExtension(fd.FileName)
        saveAsPath = pathFile + "\" + FileNameOnly + ".xls"
        xlWorkBook.SaveAs(Filename:=saveAsPath, _
                          FileFormat:=39, _
                          ReadOnlyRecommended:=False _
                          )
        xlWorkBook.Close()
    End Sub

    Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        fd.Title = "Open File Dialog"
        fd.InitialDirectory = "C:\"
        fd.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
        fd.FilterIndex = 2
        fd.RestoreDirectory = True
        fd.Multiselect = False

        If fd.ShowDialog() = DialogResult.OK Then
            strFileName = fd.FileName
        End If
    End Sub
End Class

我把FIELDINFO留空了,因为我假设这个字段将任何列格式化为TEXT,GENERAL等。而且我不知道必须分配给它的值,以便将所有列格式化为TEXT。

请帮帮我。

4 个答案:

答案 0 :(得分:4)

在代码中的某处,在将值加载到单元格之前,请运行:

Cells.NumberFormat = "@"

这会将工作表中的所有单元格格式化为Text。

答案 1 :(得分:0)

这对我有用。

.Cells(Row,Col).NumberFormat =&#34; @&#34; &#39;将单元格格式更改为存储号码作为文本

.Cells(Row,Col).Value =

这将数字字符串存储为单元格中的文本。 但是excel会在小区的角落发出绿色标记警告。

答案 2 :(得分:0)

在您发布问题的代码中尝试此操作:

FieldInfo:=Array(1, 2)

答案 3 :(得分:0)

试试 -

With objSht
     rng = objSht.Columns("A:AX")
     rng.Style = "Text"
End With