2013-10-31 44 views
-1

我有一個xml文件,我通過從Excel工作簿中提取數據創建的。從xml文件粘貼數據到Excel工作簿

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文件。

在此先感謝。

回答

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; 
+0

我不確定你要在這裏做什麼。你下載了文件嗎?我使用xml文件的內容編輯了我的初始文章,而x和y不是後代。此外,x [0]和y [1]會給出一個錯誤「無法對[]應用索引到類型爲」System.Collections.Generic.IEnumerable 「的表達式。 – user2456733

相關問題