從數據集生成在 excel 2002 或 excel 2003 中使用的 xml
本節說明如何創建 dataset 對象,以及如何使用 writexml 方法將該對象包含的數據導出到 xml 文件中。生成的 xml 文件可以直接在 excel 中打開。為便于說明,使用 jet oledb 提供程序從 microsoft access northwind 示例數據庫創建了 dataset 對象。但是,類似的代碼可與您使用 visual c# .net 創建的任何 dataset 對象一起使用。 1. 啟動 microsoft visual studio .net。在文件菜單上,單擊新建,然后單擊項目。從 visual c# 項目類型中選擇 windows 應用程序。默認情況下創建 form1。
2. 在視圖菜單上,選擇工具箱以顯示“工具箱”,然后向 form1 中添加一個按鈕。
3. 雙擊 button1。將出現該窗體的代碼窗口。
4. 將下面的 using 指令添加到 form1.cs 頂部:
using system.data.oledb;
using system.xml;
5. 將下面的私有成員變量添加到 form1 類中:
private string strconn ="provider=microsoft.jet.oledb.4.0;data source="
+ " c://program files//microsoft office//office10//samples//"
+ "northwind.mdb;";
注意:您可能需要修改連接字符串中 northwind.mdb 的路徑,以便與您安裝的位置相匹配。
6. 在 button1_click 處理程序中添加以下代碼:
//connect to the data source.
oledbconnection objconn = new oledbconnection (strconn);
try
{
objconn.open();
//fill a dataset with records from the customers table.
oledbcommand objcmd = new oledbcommand(
"select customerid, companyname, contactname, "
+ "country, phone from customers", objconn);
oledbdataadapter objadapter = new oledbdataadapter();
objadapter.selectcommand = objcmd;
dataset objdataset = new dataset();
objadapter.fill(objdataset);
//create the filestream to write with.
system.io.filestream fs = new system.io.filestream(
"c://customers.xml", system.io.filemode.create);
//create an xmltextwriter for the filestream.
system.xml.xmltextwriter xtw = new system.xml.xmltextwriter(
fs, system.text.encoding.unicode);
//add processing instructions to the beginning of the xml file, one
//of which indicates a style sheet.
xtw.writeprocessinginstruction("xml", "version='1.0'");
//xtw.writeprocessinginstruction("xml-stylesheet",
// "type='text/xsl' href='customers.xsl'");
//write the xml from the dataset to the file.
objdataset.writexml(xtw);
xtw.close();
//close the database connection.
objconn.close();
}
catch (system.exception ex)
{
messagebox.show(ex.message);
}
7. 按 f5 鍵生成并運行程序。
8. 單擊 button1 以創建 xml 文件,然后關閉 form1 以結束該程序。
9. 啟動 excel 2002 或 excel 2003 并打開 c:/customers.xml 輸出文件。
10. 在您看到已將 xml 分析成新工作簿中的行和列后,請關閉文件并退出 excel。
返回頁首
使用樣式表格式化 xml
該步驟介紹如何使用樣式表 (xsl) 來轉換 xml 數據在 excel 工作簿中的格式和排列方式。 1. 使用任何 html 編輯器或文本編輯器(例如 notepad.exe),將下面的 xsl 另存為 c:/customers.xsl:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0">
<xsl:template match="/">
<html>
<head>
<style>
.hdr { background-color:bisque;font-weight:bold }
</style>
</head>
<body>
<table>
<colgroup width="100" align="center"></colgroup>
<colgroup width="200" align="left"></colgroup>
<colgroup width="200" align="left"></colgroup>
<colgroup width="100" align="left"></colgroup>
<colgroup width="100" align="left"></colgroup>
<td class="hdr">customer id</td>
<td class="hdr">company</td>
<td class="hdr">contact</td>
<td class="hdr">country</td>
<td class="hdr">phone</td>
<xsl:for-each select="newdataset/table">
<tr>
<td><xsl:value-of select="customerid"/></td>
<td><xsl:value-of select="companyname"/></td>
<td><xsl:value-of select="contactname"/></td>
<td><xsl:value-of select="country"/></td>
<td><xsl:value-of select="phone"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
2. 在 button1_click 處理程序中取消對以下代碼行的注釋:
xtw.writeprocessinginstruction("xml-stylesheet",
"type='text/xsl' href='customers.xsl'");
此行代碼向 xml 文件寫入了一個處理指令,excel 使用該指令來查找樣式表 (customers.xsl)。
3. 按 f5 鍵生成并運行程序。
4. 單擊 button1 以創建 xml 文件,然后關閉 form1 以結束該程序。
5. 啟動 excel 2002 或 excel 2003 并打開 c:/customers.xml 輸出文件。
6. 因為從 excel 可以看到 xml 中樣式表的處理指令,所以您在打開文件時會收到一個對話框提示。在導入 xml 對話框中,選擇打開該文件,應用以下樣式表。在列表中,選擇 customers.xsl 并單擊 確定。注意,xml 數據已格式化,并已經根據樣式表排列各個列。
7. 關閉該文件并退出 excel。
返回頁首
使用代碼打開轉換的 xml
此時,您已經使用 excel 中的用戶界面打開了 xml 文件。本節介紹如何以編程方式使 excel 自動打開工作簿。下面的示例說明如何通過首先將 dataset 對象中的 xml 轉換成 html 來打開轉換的 xml,而不需要用戶干預。
新聞熱點
疑難解答
圖片精選