2011-03-08 114 views
0

我有一個excel與窗體結構文件。我需要這個表單結構excel sheert將轉換爲xml。例如excel表單具有以下結構。如何生成excel文件,以XML

Student Name ...........  Age...... 
Roll No  ......... 


--------------------------------- 
| sn | Subject | Marks |Total | 
| |   |---------|  | 
| |   |Max |Obt.|  | 
--------------------------------- 
| 1 | maths | 100| 85 | 85 | 
--------------------------------- 
| 2 | Eng  | 100| 85 | 85 | 
--------------------------------- 
| 3 | lang2 | 100| 85 | 85 | 
--------------------------------- 
| 4 | Science | 100| 85 | 85 | 
--------------------------------- 

------------ Total marks: 340   
|Grade| A | 
------------ 

有一些數據和標記是在表結構。爲此我需要xml格式。 任何人都可以請幫忙嗎?

在此先感謝。

Eshwer

回答

1

我只有很少或幾乎沒有Excel的知識,所以它可能有一個內置的函數來進行。

但是,如果沒有這樣的功能,一個快速和骯髒的方法是建立在電子表格中的XML節點,以串聯。例如:="<"&A1&">"&A2&"</"&A1&">"

它會污染與出口的東西您的電子表格,但它會很快地工作,並允許您添加數據或列,並考慮到這些無痛苦(我已經做到了)。

0

取決於你想要做什麼,你使用的Excel版本,您可以:

或使用本VBA:http://www.meadinkent.co.uk/xl_xml1.htm

Attribute VB_Name = "XL_to_XML" 
Sub MakeXML() 
' create an XML file from an Excel table 
Dim MyRow As Integer, MyCol As Integer, Temp As String, YesNo As Variant, DefFolder As String 
Dim XMLFileName As String, XMLRecSetName As String, MyLF As String, RTC1 As Integer 
Dim RangeOne As String, RangeTwo As String, Tt As String, FldName(99) As String 

MyLF = Chr(10) & Chr(13) ' a line feed command 
DefFolder = "C:\" 'change this to the location of saved XML files 

YesNo = MsgBox("This procedure requires the following data:" & MyLF _ 
& "1 A filename for the XML file" & MyLF _ 
& "2 A groupname for an XML record" & MyLF _ 
& "3 A cellrange containing fieldnames (col titles)" & MyLF _ 
& "4 A cellrange containing the data table" & MyLF _ 
& "Are you ready to proceed?", vbQuestion + vbYesNo, "MakeXML CiM") 

If YesNo = vbNo Then 
Debug.Print "User aborted with 'No'" 
Exit Sub 
End If 

XMLFileName = FillSpaces(InputBox("1. Enter the name of the XML file:", "MakeXML CiM", "xl_xml_data")) 
If Right(XMLFileName, 4) <> ".xml" Then 
XMLFileName = XMLFileName & ".xml" 
End If 

XMLRecSetName = FillSpaces(InputBox("2. Enter an identifying name of a record:", "MakeXML CiM", "record")) 

RangeOne = InputBox("3. Enter the range of cells containing the field names (or column titles):", "MakeXML CiM", "A3:D3") 
If MyRng(RangeOne, 1) <> MyRng(RangeOne, 2) Then 
    MsgBox "Error: names must be on a single row" & MyLF & "Procedure STOPPED", vbOKOnly + vbCritical, "MakeXML CiM" 
    Exit Sub 
End If 
MyRow = MyRng(RangeOne, 1) 
For MyCol = MyRng(RangeOne, 3) To MyRng(RangeOne, 4) 
If Len(Cells(MyRow, MyCol).Value) = 0 Then 
    MsgBox "Error: names range contains blank cell" & MyLF & "Procedure STOPPED", vbOKOnly + vbCritical, "MakeXML CiM" 
    Exit Sub 
End If 
FldName(MyCol - MyRng(RangeOne, 3)) = FillSpaces(Cells(MyRow, MyCol).Value) 
Next MyCol 

RangeTwo = InputBox("4. Enter the range of cells containing the data table:", "MakeXML CiM", "A4:D8") 
If MyRng(RangeOne, 4) - MyRng(RangeOne, 3) <> MyRng(RangeTwo, 4) - MyRng(RangeTwo, 3) Then 
    MsgBox "Error: number of field names <> data columns" & MyLF & "Procedure STOPPED", vbOKOnly + vbCritical, "MakeXML CiM" 
    Exit Sub 
End If 
RTC1 = MyRng(RangeTwo, 3) 

If InStr(1, XMLFileName, ":\") = 0 Then 
XMLFileName = DefFolder & XMLFileName 
End If 

Open XMLFileName For Output As #1 
Print #1, "<?xml version=" & Chr(34) & "1.0" & Chr(34) & " encoding=" & Chr(34) & "ISO-8859-1" & Chr(34) & "?>" 
Print #1, "<meadinkent>" 

For MyRow = MyRng(RangeTwo, 1) To MyRng(RangeTwo, 2) 
Print #1, "<" & XMLRecSetName & ">" 
    For MyCol = RTC1 To MyRng(RangeTwo, 4) 
    ' the next line uses the FormChk function to format dates and numbers 
    Print #1, "<" & FldName(MyCol - RTC1) & ">" & RemoveAmpersands(FormChk(MyRow, MyCol)) & "</" & FldName(MyCol - RTC1) & ">" 
    ' the next line does not apply any formatting 
    ' Print #1, "<" & FldName(MyCol - RTC1) & ">" & RemoveAmpersands(Cells(MyRow, MyCol).Value) & "</" & FldName(MyCol - RTC1) & ">" 
    Next MyCol 
Print #1, "</" & XMLRecSetName & ">" 

Next MyRow 
Print #1, "</meadinkent>" 
Close #1 
MsgBox XMLFileName & " created." & MyLF & "Process finished", vbOKOnly + vbInformation, "MakeXML CiM" 
Debug.Print XMLFileName & " saved" 
End Sub 
Function MyRng(MyRangeAsText As String, MyItem As Integer) As Integer 
' analyse a range, where MyItem represents 1=TR, 2=BR, 3=LHC, 4=RHC 

Dim UserRange As Range 
Set UserRange = Range(MyRangeAsText) 
Select Case MyItem 
Case 1 
MyRng = UserRange.Row 
Case 2 
MyRng = UserRange.Row + UserRange.Rows.Count - 1 
Case 3 
MyRng = UserRange.Column 
Case 4 
MyRng = UserRange.Columns(UserRange.Columns.Count).Column 
End Select 
Exit Function 

End Function 
Function FillSpaces(AnyStr As String) As String 
' remove any spaces and replace with underscore character 
Dim MyPos As Integer 
MyPos = InStr(1, AnyStr, " ") 
Do While MyPos > 0 
Mid(AnyStr, MyPos, 1) = "_" 
MyPos = InStr(1, AnyStr, " ") 
Loop 
FillSpaces = LCase(AnyStr) 
End Function 

Function FormChk(RowNum As Integer, ColNum As Integer) As String 
' formats numeric and date cell values to comma 000's and DD MMM YY 
FormChk = Cells(RowNum, ColNum).Value 
If IsNumeric(Cells(RowNum, ColNum).Value) Then 
FormChk = Format(Cells(RowNum, ColNum).Value, "#,##0 ;(#,##0)") 
End If 
If IsDate(Cells(RowNum, ColNum).Value) Then 
FormChk = Format(Cells(RowNum, ColNum).Value, "dd mmm yy") 
End If 
End Function 

Function RemoveAmpersands(AnyStr As String) As String 
Dim MyPos As Integer 
' replace Ampersands (&) with plus symbols (+) 

MyPos = InStr(1, AnyStr, "&") 
Do While MyPos > 0 
Mid(AnyStr, MyPos, 1) = "+" 
MyPos = InStr(1, AnyStr, "&") 
Loop 
RemoveAmpersands = AnyStr 
End Function 

從發現

希望它有幫助,

0

正如JMax所說,你有內置函數來做這件事。首先,你必須根據你的Excel電子表格編寫XML模式定義。成功創建XSD文件後,你需要做以下步驟.. 1)單擊「文件」(我假設你有Excel 2007或更高版本) 2)單擊「選項」 3)從彈出「Excel選項」點擊「自定義功能區」 4)勾選「開發人員」並點擊確定。 5)現在,你會在你的excel中獲得一個開發者標籤。點擊這個標籤。 6)點擊「Source」並瀏覽你的xsd文件。 7)之後,您必須通過拖放方法或選擇列和地圖元素來映射元素。

我希望這有助於..