C#利用XML创建Excel文档的实现方法
程序员文章站
2024-02-16 09:52:46
一般来说c#在不安装excel软件的情况下,可以通过xml来创建excel文档。因此,运行本文所述代码您无需安装excel程序。本文原例子是使用vb.net写的,以下的用c...
一般来说c#在不安装excel软件的情况下,可以通过xml来创建excel文档。因此,运行本文所述代码您无需安装excel程序。本文原例子是使用vb.net写的,以下的用c#改写的代码,分享给大家,供大家参考。
具体代码如下:
dataset mdsdata = new dataset(); mdsdata.tables.add("mytable"); mdsdata.tables["mytable"].columns.add("id"); mdsdata.tables["mytable"].columns.add("name"); mdsdata.tables["mytable"].columns.add("password"); for (int i = 0; i < 10; i++) { datarow dr = mdsdata.tables["mytable"].newrow(); dr["id"] = i; dr["name"] = i; dr["password"] = i; mdsdata.tables["mytable"].rows.add(dr); } savefiledialog dialog1 = new savefiledialog(); dialog1.addextension = true; dialog1.checkpathexists = true; dialog1.filter = "excel workbooks (*.xls) | *.xls"; dialog1.overwriteprompt = true; dialog1.title = "save excel formatted report"; if (dialog1.showdialog() == dialogresult.ok) { int num2 = 0; int num3 = mdsdata.tables[0].rows.count + 1; int num1 = mdsdata.tables[0].columns.count; num2 = 0; string text1 = dialog1.filename; if (file.exists(text1)) { file.delete(text1); } streamwriter writer1 = new streamwriter(text1, false); streamwriter writer2 = writer1; writer2.writeline("<?xml version=\"1.0\"?>"); writer2.writeline("<?mso-application progid=\"excel.sheet\"?>"); writer2.writeline("<workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\""); writer2.writeline(" xmlns:o=\"urn:schemas-microsoft-com:office:office\""); writer2.writeline(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\""); writer2.writeline(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\""); writer2.writeline(" xmlns:html=\"http://www.w3.org/tr/rec-html40\">"); writer2.writeline(" <documentproperties xmlns=\"urn:schemas-microsoft-com:office:office\">"); writer2.writeline(" <author>automated report generator example</author>"); writer2.writeline(string.format(" <created>{0}t{1}z</created>", datetime.now.tostring("yyyy-mm-dd"), datetime.now.tostring("hh:mm:ss"))); writer2.writeline(" <company>your company here</company>"); writer2.writeline(" <version>11.6408</version>"); writer2.writeline(" </documentproperties>"); writer2.writeline(" <excelworkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">"); writer2.writeline(" <windowheight>8955</windowheight>"); writer2.writeline(" <windowwidth>11355</windowwidth>"); writer2.writeline(" <windowtopx>480</windowtopx>"); writer2.writeline(" <windowtopy>15</windowtopy>"); writer2.writeline(" <protectstructure>false</protectstructure>"); writer2.writeline(" <protectwindows>false</protectwindows>"); writer2.writeline(" </excelworkbook>"); writer2.writeline(" <styles>"); writer2.writeline(" <style ss:id=\"default\" ss:name=\"normal\">"); writer2.writeline(" <alignment ss:vertical=\"bottom\"/>"); writer2.writeline(" <borders/>"); writer2.writeline(" <font/>"); writer2.writeline(" <interior/>"); writer2.writeline(" <protection/>"); writer2.writeline(" </style>"); writer2.writeline(" <style ss:id=\"s21\">"); writer2.writeline(" <alignment ss:vertical=\"bottom\" ss:wraptext=\"1\"/>"); writer2.writeline(" </style>"); writer2.writeline(" </styles>"); writer2.writeline(" <worksheet ss:name=\"myreport\">"); writer2.writeline(string.format(" <table ss:expandedcolumncount=\"{0}\" ss:expandedrowcount=\"{1}\" x:fullcolumns=\"1\"", num1.tostring(), num3.tostring())); writer2.writeline(" x:fullrows=\"1\">"); foreach (datarow row1 in mdsdata.tables[0].rows) { writer2.writeline("<row>"); for (num2 = 0; num2 != num1; num2++) { writer2.write("<cell ss:styleid=\"s21\"><data ss:type=\"string\">"); writer2.write(row1[num2].tostring()); writer2.writeline("</data></cell>"); } writer2.writeline("</row>"); } writer2.writeline(" </table>"); writer2.writeline(" <worksheetoptions xmlns=\"urn:schemas-microsoft-com:office:excel\">"); writer2.writeline(" <selected/>"); writer2.writeline(" <panes>"); writer2.writeline(" <pane>"); writer2.writeline(" <number>3</number>"); writer2.writeline(" <activerow>1</activerow>"); writer2.writeline(" </pane>"); writer2.writeline(" </panes>"); writer2.writeline(" <protectobjects>false</protectobjects>"); writer2.writeline(" <protectscenarios>false</protectscenarios>"); writer2.writeline(" </worksheetoptions>"); writer2.writeline(" </worksheet>"); writer2.writeline(" <worksheet ss:name=\"sheet2\">"); writer2.writeline(" <worksheetoptions xmlns=\"urn:schemas-microsoft-com:office:excel\">"); writer2.writeline(" <protectobjects>false</protectobjects>"); writer2.writeline(" <protectscenarios>false</protectscenarios>"); writer2.writeline(" </worksheetoptions>"); writer2.writeline(" </worksheet>"); writer2.writeline(" <worksheet ss:name=\"sheet3\">"); writer2.writeline(" <worksheetoptions xmlns=\"urn:schemas-microsoft-com:office:excel\">"); writer2.writeline(" <protectobjects>false</protectobjects>"); writer2.writeline(" <protectscenarios>false</protectscenarios>"); writer2.writeline(" </worksheetoptions>"); writer2.writeline(" </worksheet>"); writer2.writeline("</workbook>"); writer2 = null; writer1.close(); messagebox.show("report created", "success", messageboxbuttons.ok, messageboxicon.asterisk); }
这只是主要的代码,使用前需要此入using相应的命名空间,如果不知道需要哪个命名空间,可在编译时根据提示逐个添加。
上一篇: Oralce 中查询锁表记录以及解锁
下一篇: C#统计字符串里中文汉字个数的方法