转:用XML编写EXCEL文件

1,文件信息:

<? xml version="1.0" ?>
<? mso-application progid="Excel.Sheet" ?>

 

2,正文部分应包含在 < Workbook ></ Workbook >之间:

< Workbook  xmlns ="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o
="urn:schemas-microsoft-com:office:office"
xmlns:x
="urn:schemas-microsoft-com:office:excel"
xmlns:ss
="urn:schemas-microsoft-com:office:spreadsheet" >

……

</ Workbook >

 

3, 文档属性选项应包含在Workbook标识中,并处于Worksheet范围之外:

< DocumentProperties  xmlns ="urn:schemas-microsoft-com:office:office" >
< Author >Castor </ Author >
< Created >2010-03-13T18:25:49Z </ Created >
< Company >Not defined </ Company >
< Version >1.0 </ Version >
</ DocumentProperties >

 

4,ExcelWorkbook 记录了一些环境变量:

< ExcelWorkbook  xmlns ="urn:schemas-microsoft-com:office:excel" >
< WindowHeight >10005 </ WindowHeight >
< WindowWidth >10005 </ WindowWidth >
< WindowTopX >120 </ WindowTopX >
< WindowTopY >135 </ WindowTopY >
< ProtectStructure >False </ ProtectStructure >
< ProtectWindows >False </ ProtectWindows >
</ ExcelWorkbook >

 

5,在Worksheet之外可以定义多个Style,所有Style应位于 < Styles ></ Styles >之间,这些Style在书写各单元格的时候会用到:

< Styles >
< Style  ss:ID ="Default"  ss:Name ="Normal" >
    < Alignment  ss:Vertical ="Top" />
    < Borders />
    < Font  ss:FontName ="Arial"  x:Family ="Swiss" />
    < Interior />
    < NumberFormat />
    < Protection />
</ Style >
< Style  ss:ID ="s62"  ss:Name ="Hyperlink" >
    < Borders >
     < Border  ss:Position ="Bottom"  ss:LineStyle ="Continuous"  ss:Weight ="1" />
     < Border  ss:Position ="Left"  ss:LineStyle ="Continuous"  ss:Weight ="1" />
     < Border  ss:Position ="Right"  ss:LineStyle ="Continuous"  ss:Weight ="1" />
     < Border  ss:Position ="Top"  ss:LineStyle ="Continuous"  ss:Weight ="1" />
    </ Borders >
    < Font  ss:FontName ="Arial"  x:Family ="Swiss"  ss:Color ="#0000FF"
    ss:Underline
="Single" />
</ Style >
< Style  ss:ID ="s63" >
    < Alignment  ss:Horizontal ="Left"  ss:Vertical ="Bottom"  ss:WrapText ="1" />
    < Borders >
     < Border  ss:Position ="Bottom"  ss:LineStyle ="Continuous"  ss:Weight ="1" />
     < Border  ss:Position ="Left"  ss:LineStyle ="Continuous"  ss:Weight ="1" />
     < Border  ss:Position ="Right"  ss:LineStyle ="Continuous"  ss:Weight ="1" />
     < Border  ss:Position ="Top"  ss:LineStyle ="Continuous"  ss:Weight ="1" />
    </ Borders >
    < Font  ss:FontName ="Arial"  x:Family ="Swiss"  ss:Bold ="1" />
</ Style >
</ Styles >

 

6,Worksheet标识之间定义了一个工作表,一个文件可以定义多个Worksheet:

< Worksheet  ss:Name ="My First Sheet" >
</ Worksheet >

 

7,Worksheet之内有个Tables,定义了表的内容:

< Table  ss:ExpandedColumnCount ="2"  ss:ExpandedRowCount ="2"  x:FullColumns ="1"    x:FullRows ="1" >

……

</ Table >

 

8, < Column >定义了各列的宽度:

    < Column  ss:Width ="50.25" />
    < Column  ss:Width ="99.75" />

 

9,接下来,每一个 < Row ></ Row >之间定义的就是表格一行的内容,每个 < Cell ></ Cell >则对应一个单元格,注意每行的列数和总的行数应该和 < Table >中定义的一致:

    < Row  ss:AutoFitHeight ="0"  ss:Height ="63.75" >
     < Cell  ss:StyleID ="s63" >< Data  ss:Type ="String" >A1 </ Data ></ Cell >
     < Cell  ss:StyleID ="s63" >< Data  ss:Type ="String" >B1 </ Data ></ Cell >
    </ Row >

 

10,Table定义完之后,还可以用 <>定义一些设置:

< WorksheetOptions  xmlns ="urn:schemas-microsoft-com:office:excel" >
    < Print >
     < ValidPrinterInfo />
     < PaperSizeIndex >9 </ PaperSizeIndex >
     < HorizontalResolution >600 </ HorizontalResolution >
     < VerticalResolution >600 </ VerticalResolution >
    </ Print >
    < DoNotDisplayGridlines />
    < Panes >
     < Pane >
      < Number >3 </ Number >
      < ActiveRow >1 </ ActiveRow >
     </ Pane >
    </ Panes >
    < ProtectObjects >False </ ProtectObjects >
    < ProtectScenarios >False </ ProtectScenarios >
</ WorksheetOptions >

 

 

****************************************

 

 

下面是一个完整的示例,可以COPY到记事本中,然后存成XML文件,即可用EXCEL打开:

<? xml version="1.0" ?> <!-- Wrote by Castor -->
<? mso-application progid="Excel.Sheet" ?>
< Workbook  xmlns ="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o
="urn:schemas-microsoft-com:office:office"
xmlns:x
="urn:schemas-microsoft-com:office:excel"
xmlns:ss
="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html
="http://www.w3.org/TR/REC-html40" >
< DocumentProperties  xmlns ="urn:schemas-microsoft-com:office:office" >
< Author >Castor </ Author > <!-- Qm Xu -->
< Created >2010-03-13T18:25:49Z </ Created >
< Company >Unknown </ Company >
< Version >1.0 </ Version >
</ DocumentProperties >
< OfficeDocumentSettings  xmlns ="urn:schemas-microsoft-com:office:office" >
< AllowPNG />
</ OfficeDocumentSettings >
< ExcelWorkbook  xmlns ="urn:schemas-microsoft-com:office:excel" >
< WindowHeight >10005 </ WindowHeight >
< WindowWidth >10005 </ WindowWidth >
< WindowTopX >120 </ WindowTopX >
< WindowTopY >135 </ WindowTopY >
< ActiveSheet >1 </ ActiveSheet >
< ProtectStructure >False </ ProtectStructure >
< ProtectWindows >False </ ProtectWindows >
</ ExcelWorkbook >
< Styles >
< Style  ss:ID ="Default"  ss:Name ="Normal" >
    < Alignment  ss:Vertical ="Top" />
    < Borders />
    < Font  ss:FontName ="Arial"  x:Family ="Swiss" />
    < Interior />
    < NumberFormat />
    < Protection />
</ Style >
< Style  ss:ID ="s66"  ss:Name ="Hyperlink" >
    < Font  ss:FontName ="Arial"  x:Family ="Swiss"  ss:Color ="#0000FF"
    ss:Underline
="Single" />
</ Style >
< Style  ss:ID ="s63" >
    < Alignment  ss:Horizontal ="Left"  ss:Vertical ="Bottom"  ss:WrapText ="1" />
    < Borders >
     < Border  ss:Position ="Bottom"  ss:LineStyle ="Continuous"  ss:Weight ="1" />
     < Border  ss:Position ="Left"  ss:LineStyle ="Continuous"  ss:Weight ="1" />
     < Border  ss:Position ="Right"  ss:LineStyle ="Continuous"  ss:Weight ="1" />
     < Border  ss:Position ="Top"  ss:LineStyle ="Continuous"  ss:Weight ="1" />
    </ Borders >
    < Font  ss:FontName ="Arial"  x:Family ="Swiss"  ss:Bold ="1" />
</ Style >
< Style  ss:ID ="s67" >
    < Alignment  ss:Horizontal ="Left"  ss:Vertical ="Top"  ss:WrapText ="1" />
    < Borders >
     < Border  ss:Position ="Bottom"  ss:LineStyle ="Continuous"  ss:Weight ="1" />
     < Border  ss:Position ="Left"  ss:LineStyle ="Continuous"  ss:Weight ="1" />
     < Border  ss:Position ="Right"  ss:LineStyle ="Continuous"  ss:Weight ="1" />
     < Border  ss:Position ="Top"  ss:LineStyle ="Continuous"  ss:Weight ="1" />
    </ Borders >
</ Style >
< Style  ss:ID ="s68"  ss:Parent ="s66" >
    < Borders >
     < Border  ss:Position ="Bottom"  ss:LineStyle ="Continuous"  ss:Weight ="1" />
     < Border  ss:Position ="Left"  ss:LineStyle ="Continuous"  ss:Weight ="1" />
     < Border  ss:Position ="Right"  ss:LineStyle ="Continuous"  ss:Weight ="1" />
     < Border  ss:Position ="Top"  ss:LineStyle ="Continuous"  ss:Weight ="1" />
    </ Borders >
    < Interior />
</ Style >
</ Styles >
< Worksheet  ss:Name ="My First Sheet" >
< Table  ss:ExpandedColumnCount ="2"  ss:ExpandedRowCount ="2"  x:FullColumns ="1"  x:FullRows ="1" >
    < Column  ss:Width ="50.25" />
    < Column  ss:Width ="99.75" />
    < Row  ss:AutoFitHeight ="0"  ss:Height ="63.75" >
     < Cell  ss:StyleID ="s63" >< Data  ss:Type ="String" >A1 </ Data ></ Cell >
     < Cell  ss:StyleID ="s63" >< Data  ss:Type ="String" >B1 </ Data ></ Cell >
    </ Row >
    < Row  ss:AutoFitHeight ="0"  ss:Height ="25.5" >
     < Cell  ss:StyleID ="s68"  ss:HRef ="http://hi.baidu.com/xuqm" >< Data
      
ss:Type ="String" >A2 </ Data ></ Cell >
     < Cell  ss:StyleID ="s67" >< Data  ss:Type ="String" >B2 </ Data ></ Cell >
    </ Row >
</ Table >
< WorksheetOptions  xmlns ="urn:schemas-microsoft-com:office:excel" >
    < Print >
     < ValidPrinterInfo />
     < PaperSizeIndex >9 </ PaperSizeIndex >
     < HorizontalResolution >600 </ HorizontalResolution >
     < VerticalResolution >600 </ VerticalResolution >
    </ Print >
    < DoNotDisplayGridlines />
    < Panes >
     < Pane >
      < Number >3 </ Number >
      < ActiveRow >1 </ ActiveRow >
     </ Pane >
    </ Panes >
    < ProtectObjects >False </ ProtectObjects >
    < ProtectScenarios >False </ ProtectScenarios >
</ WorksheetOptions >
</ Worksheet >
< Worksheet  ss:Name ="My Second Sheet" >
< Table  ss:ExpandedColumnCount ="1"  ss:ExpandedRowCount ="1"  x:FullColumns ="1"
   x:FullRows
="1" >
    < Row >
     < Cell >< Data  ss:Type ="String" >A1,2 </ Data ></ Cell >
    </ Row >
</ Table >
< WorksheetOptions  xmlns ="urn:schemas-microsoft-com:office:excel" >
    < Selected />
    < Panes >
     < Pane >
      < Number >3 </ Number >
      < ActiveRow >1 </ ActiveRow >
     </ Pane >
    </ Panes >
    < ProtectObjects >False </ ProtectObjects >
    < ProtectScenarios >False </ ProtectScenarios >
</ WorksheetOptions >
</ Worksheet >
</ Workbook >
<!-- 到此结束(Castor) -->

你可能感兴趣的:(转:用XML编写EXCEL文件)