0

我有一个通过从 Excel 工作簿中提取数据创建的 xml 文件。

http://www.2shared.com/document/wbAYcQ4F/XMLTest.html

<?xml version="1.0"?>
<?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">
  <Styles>
    <Style ss:ID="Default" ss:Name="Normal">
      <Alignment ss:Vertical="Bottom" />
      <Borders />
      <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000" />
      <Interior />
      <NumberFormat />
      <Protection />
    </Style>
    <Style ss:ID="s16">
      <Borders>
        <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="2" />
        <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="2" />
        <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="2" />
      </Borders>
    </Style>
    <Style ss:ID="s17">
      <Borders>
        <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="2" />
        <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="2" />
      </Borders>
    </Style>
    <Style ss:ID="s18">
      <Borders>
        <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="2" />
        <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="2" />
        <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="2" />
      </Borders>
      <Interior ss:Color="#16365C" ss:Pattern="Solid" />
    </Style>
  </Styles>
  <Worksheet ss:Name="First Worksheet">
    <Table ss:ExpandedColumnCount="5" ss:ExpandedRowCount="9" ss:DefaultRowHeight="15">
      <Column ss:Index="3" ss:Width="54" />
      <Row ss:Height="15.75" />
      <Row>
        <Cell ss:Index="3" ss:StyleID="s16">
          <Data ss:Type="Number">12345</Data>
        </Cell>
      </Row>
      <Row>
        <Cell ss:Index="3" ss:StyleID="s17" />
      </Row>
      <Row>
        <Cell ss:Index="3" ss:StyleID="s17">
          <Data ss:Type="String">Some Text</Data>
        </Cell>
      </Row>
      <Row>
        <Cell ss:Index="3" ss:StyleID="s17" />
      </Row>
      <Row ss:Height="15.75">
        <Cell ss:Index="3" ss:StyleID="s18" />
      </Row>
    </Table>
  </Worksheet>
</Workbook>

xml 文件包含我选择的范围的数据。工作表名称,以及范围内的所有值和格式(颜色、边框等) 在此示例中,只有 C 列有一个带数字的单元格、一个带字符串的单元格和一个彩色单元格,以及围绕所有数据的边框。当您使用 Excel 打开文件时,它会准确显示数据的提取方式。

现在我想知道是否可以以编程方式将其提取为具有所有值和格式的 Excel.Range,以将其粘贴到新的 Excel 工作簿中。最好不要在 Excel 中打开 xml 文件。

提前致谢。

4

1 回答 1

0
XDocument doc = XDocument.Load( "XMLFile1.xml" );

var x= doc.Descendants( "x" );
var y= doc.Descendants( "y" );
object[,] z;
Excel.Application ActiveExelAplication = Globals.ThisAddIn.Application as Excel.Application;
Excel.Worksheet ExWorksheet = ActiveExelAplication.ActiveSheet as Excel.Worksheet;
z= ExWorksheet.get_Range("v"+x[0].ToString() , "v" + y[1].ToString() as Excel.Range;
于 2013-10-31T16:26:47.387 回答