国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

使用 Visual C# .NET 向 Microsoft Excel 2002 傳輸 XML 數據

2024-07-10 12:59:12
字體:
來源:轉載
供稿:網友


收集最實用的網頁特效代碼!

從數據集生成在 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,而不需要用戶干預。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴彦淖尔市| 河间市| 景宁| 陆川县| 横山县| 澜沧| 安国市| 景德镇市| 遵义市| 庆云县| 玛纳斯县| 永靖县| 龙山县| 临泉县| 双鸭山市| 兴和县| 武义县| 吉林省| 济源市| 富顺县| 苍山县| 江川县| 南雄市| 五大连池市| 邮箱| 龙游县| 前郭尔| 渭源县| 金华市| 丹棱县| 苍南县| 繁昌县| 黄骅市| 通海县| 黄陵县| 定结县| 平南县| 临湘市| 临武县| 铅山县| 侯马市|