当前位置:编程学习 > C#/ASP.NET >>

关于vb控制excel问题

 excelXLS = CreateObject("Excel.Application")
 excelWK = excelXLS.Workbooks.Open(strFieldName)
                  // j = 0 
             excelWS = excelWK.Worksheets(j + 1)  //选中sheet1            excelWK.Sheets.Add()
            excelWK.Sheets("Sheet1").Select()
            excelWS.Range("A1:Y10").Select()
            excelWS.Range("A1:Y10").Copy()
            excelWK.Sheets("Sheet" & (j + 2)).Select()
            excelWK.ActiveSheet.Paste()
            excelWS = excelWK.Worksheets(j + 2)
            excelWS.Cells.Select()
            'excelWS.Selection.ColumnWidth = 5
            excelWS.Cells.ColumnWidth = 5
            excelWS = excelWK.Worksheets(j + 1)
流程是 在sheet1后面新加一个sheet 然后复制sheet1的 A1:Y10 粘贴到sheet2 然后全选sheet2 把列宽改成5  我在excelWS.Cells.Select()
这里报错 说类range 的select无效,求各位帮忙看看 Excel VB --------------------编程问答-------------------- 试了一下,没问题,实在不行自己调试看看
        Dim excelXLS As Excel.Application
        Dim excelWK As Workbook
        Dim excelWS As Worksheet
        Dim j As Integer = 0
        excelXLS = CreateObject("Excel.Application")
        excelXLS.Visible = True
        excelWK = excelXLS.Workbooks.Open("C:\test.xlsx")

        excelWS = excelWK.Worksheets(j + 1)  '//选中sheet1            excelWK.Sheets.Add()
        excelWK.Sheets("Sheet1").Select()
        excelWS.Range("A1:Y10").Select()
        excelWS.Range("A1:Y10").Copy()
        excelWK.Sheets("Sheet" & (j + 2)).Select()
        excelWK.ActiveSheet.Paste()
        excelWS = excelWK.Worksheets(j + 2)
        excelWS.Cells.Select()
        'excelWS.Selection.ColumnWidth = 5
        excelWS.Cells.ColumnWidth = 2
        excelWS = excelWK.Worksheets(j + 1)

        excelWK.Save()
        excelWK.Close()
--------------------编程问答-------------------- excelWS.Cells.Select()根本就不需要,多余,直接用excelWS.Cells.ColumnWidth = 5就OK了
补充:.NET技术 ,  VB.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,