using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.io;
using system.xml;
namespace mywindows
{
/**//// <summary>
/// 這個示例演示如何把office文件編碼為xml文件以及如何把生成的xml文件轉(zhuǎn)換成office文件
/// 把文件轉(zhuǎn)換成xml格式,然后就可以用web服務(wù),.net remoting,winsock等傳送了(其中后兩者可以不轉(zhuǎn)換也可以傳送)
/// xml解決了在多層架構(gòu)中數(shù)據(jù)傳輸?shù)膯栴},比如說在客戶端可以用web服務(wù)獲取服務(wù)器端的office文件,修改后再回傳給服務(wù)器
/// 只要把文件轉(zhuǎn)換成xml格式,便有好多方案可以使用了,而xml具有平臺無關(guān)性,你可以在服務(wù)端用.net用發(fā)布web服務(wù),然后客戶端用
/// java寫一段applit小程序來處理發(fā)送過來的文件,當(dāng)然我舉的例子幾乎沒有任何顯示意義,它卻給了我們不少的啟示.
/// 另外如果你的解決方案是基于多平臺的,那么他們之間的交互最好不要用遠程應(yīng)用程序接口調(diào)用(rpc),應(yīng)該盡量用基于文檔的交互,
/// 比如說.net下的msmq,j2ee的jmq.
///
/// 示例中設(shè)計到好多的類,我并沒有在所有的地方做過多注釋,有不明白的地方請參閱msdn,這是偶第一個windows程序,有不對的地方
/// 歡迎各位指導(dǎo)
/// </summary>
public class form1 : system.windows.forms.form
{
/**//// <summary>
/// 聲明四個button,一個openfiledialog,一個savefiledialog,以及兩個xmldocument
/// </summary>
private system.windows.forms.button button1;
private system.windows.forms.button button2;
private system.windows.forms.openfiledialog openfiledialog1;
private system.windows.forms.savefiledialog savefiledialog1;
private system.windows.forms.button button3;
private system.windows.forms.button button4;
private system.xml.xmldocument mxmldoc;
private system.xml.xmldocument doc;
private system.componentmodel.container components = null;
public form1()
{
//
// windows 窗體設(shè)計器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
}
/**//// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
windows 窗體設(shè)計器生成的代碼#region windows 窗體設(shè)計器生成的代碼
/**//// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void initializecomponent()
{
this.button1 = new system.windows.forms.button();
this.button2 = new system.windows.forms.button();
this.openfiledialog1 = new system.windows.forms.openfiledialog();
this.savefiledialog1 = new system.windows.forms.savefiledialog();
this.button3 = new system.windows.forms.button();
this.button4 = new system.windows.forms.button();
this.suspendlayout();
//
// button1
//
this.button1.location = new system.drawing.point(96, 32);
this.button1.name = "button1";
this.button1.tabindex = 0;
this.button1.text = "生成xml";
this.button1.click += new system.eventhandler(this.button1_click);
//
// button2
//
this.button2.location = new system.drawing.point(96, 80);
this.button2.name = "button2";
this.button2.tabindex = 1;
this.button2.text = "生成doc";
this.button2.click += new system.eventhandler(this.button2_click);
//
// button3
//
this.button3.location = new system.drawing.point(8, 32);
this.button3.name = "button3";
this.button3.tabindex = 2;
this.button3.text = "加載doc";
this.button3.click += new system.eventhandler(this.button3_click);
//
// button4
//
this.button4.location = new system.drawing.point(8, 80);
this.button4.name = "button4";
this.button4.tabindex = 3;
this.button4.text = "加載xml";
this.button4.click += new system.eventhandler(this.button4_click);
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(184, 141);
this.controls.add(this.button4);
this.controls.add(this.button3);
this.controls.add(this.button2);
this.controls.add(this.button1);
this.name = "form1";
this.text = "form1";
this.resumelayout(false);
//
//手工注冊一下load和closed事件
//
this.load += new system.eventhandler(this.form1_load);
this.closed += new system.eventhandler(this.form1_closed);
}
#endregion
/**//// <summary>
/// 從這個入口啟動窗體
/// </summary>
static void main()
{
application.run(new form1());
}
/**//// <summary>
/// 把加載的office文件轉(zhuǎn)換為xml文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_click(object sender, system.eventargs e)
{
savefiledialog1.filter = "xml 文件|*.xml";//設(shè)置打開對話框的文件過濾條件
savefiledialog1.title = "保存成 xml 文件";//設(shè)置打開對話框的標題
savefiledialog1.filename="";
savefiledialog1.showdialog();//打開對話框
if(savefiledialog1.filename != "")//檢測用戶是否輸入了保存文件名
{
mxmldoc.save(savefiledialog1.filename);//用私有對象mxmldoc保存文件,mxmldoc在前面聲明過
messagebox.show("保存成功");
}
}
/**//// <summary>
/// 把加載的xml文件轉(zhuǎn)換為office文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_click(object sender, system.eventargs e)
{
//從私有對象dox里選取me節(jié)點,這里的一些對xml對象的操作詳細說明可以參考msdn以獲取更多信息
xmlnode node=doc.documentelement .selectsinglenode("me") ;
xmlelement ele=(xmlelement)node;//獲取一個xml元素
string pic=ele.getattribute ("aa");//獲取ele元素的aa屬性并報訊在一個臨時字符串變量pic
byte[] bytes=convert.frombase64string (pic);//聲明一個byte[]用來存放base64解碼轉(zhuǎn)換過來的數(shù)據(jù)流
//從保存對話框里獲取文件保存地址
savefiledialog1.filter = "office documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt";
savefiledialog1.title = "保存成 office 文件";
savefiledialog1.filename="";
savefiledialog1.showdialog();
if(savefiledialog1.filename != "")
{
//創(chuàng)建文件流并保存
filestream outfile=new system.io .filestream (savefiledialog1.filename,system.io.filemode.createnew);
outfile.write(bytes,0,(int)bytes.length );
messagebox.show("保存成功");
}
}
/**//// <summary>
/// 加載窗口時的一些初始化行為
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void form1_load(object sender, system.eventargs e)
{
messagebox.show("歡迎使用蛙蛙牌文檔轉(zhuǎn)換器");
}
/**//// <summary>
/// 卸載窗體時把臨時變量全部釋放
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void form1_closed(object sender, system.eventargs e)
{
mxmldoc=null;
doc=null;
}
/**//// <summary>
/// 加載office文件并編碼序列花為一個xmldocument變量
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_click(object sender, system.eventargs e)
{
string strfilename;
openfiledialog1.filter = "office documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt" ;
openfiledialog1.filterindex = 1;
openfiledialog1.filename = "";
openfiledialog1.showdialog();
strfilename = openfiledialog1.filename;
if(strfilename.length != 0)
{
system.io.filestream infile=new filestream(strfilename,system.io.filemode.open,system.io.fileaccess.read);
byte[] binarydata=new byte [infile.length];
infile.read(binarydata, 0,(int)infile.length);
string mstr=convert.tobase64string(binarydata);
string hh=mstr;
mxmldoc=new system.xml.xmldocument();
mstr=string.format ("<wawa><me aa=/"{0}/"/></wawa>",mstr);
mxmldoc.loadxml( mstr);
messagebox.show("加載成功");
}
}
/**//// <summary>
/// 加載xml文件到私有對象dox
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_click(object sender, system.eventargs e)
{
string strfilename;
openfiledialog1.filter = "xml 文件|*.xml" ;
openfiledialog1.filterindex = 1;
openfiledialog1.filename = "";
openfiledialog1.showdialog();
strfilename = openfiledialog1.filename;
//if the user does not cancel, open the document.
if(strfilename.length != 0)
{
doc=new xmldocument();
doc.load(strfilename);
messagebox.show("加載成功");
}
}
}
}
新聞熱點
疑難解答