Excel单元格格式文本更改

时间:2009-12-14 10:57:39

标签: vb.net excel

我使用过microsoft Excel Sheet ..我在excel表中出现了不同的值没有问题.. 但是我已经失去了一些像这样的数字列(00023785678)..在这个列中,第一个零是没有得到它.. 所以我去改变文本的单元格格式的列..

如何在Vb.net代码中创建..已经我已经掉了excel表这个方法...

       Dim oXL As Excel.Application
            Dim oWB As Excel.Workbook
            Dim oSheet As Excel.Worksheet
            Dim columnrange = oSheet.Columns
            Dim therange = oSheet.UsedRange
            ' Dim wb As Microsoft.Office.Interop.Excel.Workbook
            ''Dim style As Microsoft.Office.Interop.Excel.Style
            oXL = CreateObject("Excel.Application")
            oXL.Visible = True
            oWB = oXL.Workbooks.Add
            oSheet = oWB.ActiveSheet
            'oXL.Selection.num()
            oXL.Selection.NumberFormat = "Text"

            For c As Integer = 0 To dt.Columns.Count - 1
                oSheet.Cells(1, c + 1).Value = dt.Columns(c).ColumnName

            Next
            For rCnt As Int16 = 2 To therange.Rows.Count
                Dim rowArray(therange.Columns.Count) As String
                For cCnt As Int16 = 1 To therange.Columns.Count
                    Dim Obj = CType(therange.Cells(rCnt, cCnt), Excel.Range)
                    Dim celltext As String
                    celltext = Obj.Value.ToString
                    rowArray((cCnt - 1)) = celltext

                Next
            Next

            For r As Integer = 0 To dt.Rows.Count - 1
                For c As Integer = 0 To dt.Columns.Count - 1
                    oSheet.Cells(r + 2, c + 1).Value = dt.Rows(r)(c)
                    oSheet.Cells(r + 2, c + 1).numberformat = "0"
                Next
            Next
            'With oWB
            '    .Cells(seriesName.GetUpperBound(0) + 7, 3).numberformat = "#.00"
            '    .Cells(seriesName.GetUpperBound(0) + 7, 5).numberformat = "0"
            'End With


            'With oSheet.Range("A1", "ZZ1")
            '    .Font.Bold = True
            '    .VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
            '    .HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter
            '    .EntireColumn.AutoFit()
            '    .EntireRow.AutoFit()
            'End With

            Dim FirstRow As Long
            Dim SecentRow As Long
            Dim ThirdRow As Long
            With oSheet.Range("A1", "BD1")
                FirstRow = 1
                SecentRow = 2
                ThirdRow = 3
                .Rows(0 + 1).EntireRow.Insert()
                .Rows(0 + 1).EntireRow.Insert()
                .Rows(0 + 1).EntireRow.Insert()
                .Font.Bold = True
                .VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
                .HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter
                .EntireColumn.AutoFit()
                .EntireRow.AutoFit()

                '.EntireRow.TextToColumns()


                .Offset.Justify()
                .Offset.BorderAround()
                .Offset.WrapText = True
                .Offset.Select()
            End With

但是现在还没有得到答案...... 请帮助我...

2 个答案:

答案 0 :(得分:4)

使用前导单引号'将要作为文本处理的单元格中的值作为前缀。这会强制Excel将单元格视为文本,并且单引号在工作表中不可见。

答案 1 :(得分:1)

oSheet.Range("A1", "BD1").NumberFormat = "@"

这样就可以了。