正如您可以通过以下代码看到的,我是VBscripting for Excel的新手。我只是尝试将HTML文件(“DEMO8.HTM”)导入Excel并根据F列中的数据创建图表。图表应显示为创建的工作表中的对象。我在使用'Chart.Add'语法时遇到了问题。每当我改变它,其他的东西都会出错。无论如何,这是代码:
Dim chtChart
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\DEMO8.HTM")
objExcel.Visible = TRUE
Set chtChart = Charts.Add
With chtChart
chtChart.ChartType = xlColumnClustered
chtChart.SetSourceData.Range("'DEMO8'!$F:$F")
chtChart.Location.xlLocationAsObject
ActiveChart.SetSourceData Source:=Range("'DEMO8'!$F:$F")
ActiveChart.Legend.Select
Selection.Delete
ActiveChart.ChartArea.Select
ActiveChart.Axes(xlCategory).Select
Selection.Delete
ActiveChart.ChartArea.Select
ActiveChart.Axes(xlValue).Select
ActiveChart.Axes(xlValue).MaximumScale = 300
ActiveChart.Axes(xlValue).MinimumScale = 0
End With
' Give the user control of Excel
UserControl = true
我从各种代码中基本上'弗兰克斯顿'这一点。它似乎打开HTML文件但不创建图表。任何帮助表示赞赏。
答案 0 :(得分:2)
可以让你更接近你想要的东西;
Dim chtChart, objWorkbook, objExcel
const xlValue = 2
const xlColumnClustered = 51
const xlCategory = 1
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\temp\DEMO8.html")
objExcel.Visible = True
Set chtChart = objWorkbook.Charts.Add
chtChart.ChartType = xlColumnClustered
chtChart.SetSourceData objExcel.Sheets("DEMO8").Range("F:F")
objWorkbook.ActiveChart.Legend.Select
objExcel.Selection.Delete
objWorkbook.ActiveChart.ChartArea.Select
objWorkbook.ActiveChart.Axes(xlCategory).Select
objExcel.Selection.Delete
objWorkbook.ActiveChart.ChartArea.Select
objWorkbook.ActiveChart.Axes(xlValue).Select
objWorkbook.ActiveChart.Axes(xlValue).MaximumScale = 300
objWorkbook.ActiveChart.Axes(xlValue).MinimumScale = 0
答案 1 :(得分:0)
以下代码会将excel表格转换为html:
Dim oExcel
Dim objExcelWB
Dim objExcelWS, WScript
Dim strColumnCount, strTotRows, strData, strTable
Dim i, j, objFSO, objtxt
Dim strWbk
strWbk = "<excel sheet path>"
Dim strWsheetName
strWsheetName = "<workbook name>"
Dim strHTMLFile
strHTMLFile = "<html path>"
Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = False
Set objExcelWB = oExcel.Workbooks.Open(strWbk)
Set objExcelWS = objExcelWB.Worksheets(strWsheetName)
'Getting the rows and column count
strColumnCount = objExcelWS.UsedRange.Columns.Count
strTotRows = objExcelWS.UsedRange.Rows.Count
strTable = "<table border=""""1"""">"
'Create html table based on content of the excel file
For j = 1 To strTotRows
strTable = strTable & "<tr>"
For i = 1 To strColumnCount
strData = Trim(objExcelWS.Cells(j, i))
strTable = strTable & "<td>" & strData & "</td>"
Next
strTable = strTable & "</tr>"
Next
strTable = strTable & "</table>"
Set objFSO = CreateObject("scripting.FileSystemObject")
Set objtxt = objFSO.createTextFile(strHTMLFile)
objtxt.write (strTable)
'Closing the workbook
objExcelWB.Close
oExcel.Quit
Set objFSO = Nothing
Set objExcelWB = Nothing
Set oExcel = Nothing