2010-03-22 25 views
2

我有一個XML文檔保存爲我的項目一個小的數據,我想我的XML轉換爲Excel文件(如Microsoft Office Excel 2003及以上)如何轉換XML到excel文件編程

我該怎麼辦這是編程?

+0

是否要將xml轉換爲xls?或者你想映射(格式以某種方式)數據從xml文件到xls - 表。 – mipe34

回答

-2

How to open an XML file in Excel 2003

總之,你可以簡單地打開它使用文件>打開。然後,當你選擇一個XML文件,系統會提示您選擇下列方法之一來導入XML數據:

  • 作爲XML列表
  • 作爲只讀工作簿
  • 使用XML源任務窗格
+0

我知道,謝謝;但我需要一個代碼來做到這一點,正如我在下面告訴的。 msdn文件是我發現的舊的,並且太複雜了::( – Cmptrb

-2

你不能簡單地在Excel中打開它嗎?我認爲Excel識別後綴XML?

+3

xml文件可以很容易地在excel中打開。通過程序轉換xml文件的原因是在excel中打開xml文件更正常。要麼不容易說這裏的人是你的xml文檔請用excel打開它改變文件擴展名不起作用 – Cmptrb

+0

晚會到了,但我不確定我是否理解你的解釋,我想你是說你想做它與代碼。但我不知道你的意思是「轉換的原因是打開...更普通」。我不知道這意味着什麼。 – MJB

2

如果您可以控制生成的XML,只需生成一個XML Spreadsheet文件(Excel 2002和2003的XML文件標準)即可。

這些在Excel中本地打開,無需更改擴展名。 (要在Excel中默認打開,應將文件擴展名XML設置爲使用「XML編輯器」打開,該編輯器是根據需要將XML文件路由到Excel,Word,PowerPoint,InfoPath或外部XML編輯器的Office應用程序。這是安裝Office時的默認映射,但對於某些用戶,特別是在文本編輯器中編輯XML文件的開發人員來說,它可能會不起作用。)

或者,使用NPOI庫生成本機(97/2000 BIFF/XLS)Excel文件而不是XML。

+0

是的,用excel打開xml是okey,但我想要將我的xml文件作爲excel文件,我將在excel小宏上編寫代碼 – Cmptrb

2

您甚至可以將XML文件讀取爲字符串,並使用正則表達式讀取標籤之間的內容並創建CSV文件或使用xpath表達式讀取XML文件數據並導出爲CSV文件。

+0

即使您可以編寫XSLT將XML轉換爲CSV –

1

我知道沒有簡單的方法可以將基於xml的電子表格轉換爲xls/xlsx的基於代碼的轉換。但你可以看看Microsoft Open Xml SDK,它可以讓你使用xlsx。您可以構建xlsx電子表格並將其與數據一起提供。在open-xml SDK的層面上,它就像構建xml文件一樣。

首先聲明這些必需的引用:

8

它可以通過使用Microsoft.Office.Interop.Excel如下所示來實現。

using System; 
using System.IO; 
using System.Reflection; 
using System.Runtime.InteropServices; 
using Microsoft.Office.Tools.Excel; 
using Microsoft.VisualStudio.Tools.Applications.Runtime; 
using Excel = Microsoft.Office.Interop.Excel; 
using Office = Microsoft.Office.Core; 
using System.Diagnostics; 
using Microsoft.Win32; 

不是創建Excel的應用程序,如下所示:

Excel.Application excel2; // Create Excel app 
Excel.Workbook DataSource; // Create Workbook 
Excel.Worksheet DataSheet; // Create Worksheet 
excel2 = new Excel.Application(); // Start an Excel app 
DataSource = (Excel.Workbook)excel2.Workbooks.Add(1); // Add a Workbook inside 
string tempFolder = System.IO.Path.GetTempPath(); // Get folder 
string FileName = openFileDialog1.FileName.ToString(); // Get xml file name 

在一個循環下面的代碼使用後,確保在XML文件中的所有項目複製

// Open that xml file with excel 
DataSource = excel2.Workbooks.Open(FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); 
// Get items from xml file 
DataSheet = DataSource.Worksheets.get_Item(1); 
// Create another Excel app as object 
Object xl_app; 
xl_app = GetObj(1, "Excel.Application"); 
Excel.Application xlApp = (Excel.Application)xl_app; 
// Set previous Excel app (Xml) as ReportPage 
Excel.Application ReportPage = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"); 
// Copy items from ReportPage(Xml) to current Excel object 
Excel.Workbook Copy_To_Excel = ReportPage.ActiveWorkbook; 

現在我們有一個Excel名爲Copy_To_Excel的對象,其中包含Xml中的所有項目.. 我們可以用我們想要的名稱保存並關閉Excel對象..

Copy_To_Excel.Save("thePath\theFileName"); 
Copy_To_Excel.Close(); 
+1

請提供您的答案的簡要說明 –

+0

它在Excel中打開FileName(將它傳遞給XML Filepath),允許在完成數據複製後將副本複製到Copy_To_Excel工作簿,關閉兩者並完成。 – John

+0

如果你不想使用這個從谷歌代碼看這個:http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c-sharp – John

2

1。將xml文件填充到數據集中,
2.將數據集轉換爲asp.net中的以下方法,使其優於以下方法

這些都是非常簡單的方法。

public static void Convert(System.Data.DataSet ds, System.Web.HttpResponse response) 
    { 
     //first let's clean up the response.object 
     response.Clear(); 
     response.Charset = ""; 
     //set the response mime type for excel 
     response.ContentType = "application/vnd.ms-excel"; 
     //create a string writer 
     System.IO.StringWriter stringWrite = new System.IO.StringWriter(); 


     //create an htmltextwriter which uses the stringwriter 
     System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite); 
     //instantiate a datagrid 
     System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid(); 
     //set the datagrid datasource to the dataset passed in 
     dg.DataSource = ds.Tables[0]; 
     //bind the datagrid 
     dg.DataBind(); 


     //tell the datagrid to render itself to our htmltextwriter 
     dg.RenderControl(htmlWrite); 
     //all that's left is to output the html 
     response.Write(stringWrite.ToString()); 
     response.End(); 
    }