欢迎光临
我们一直在努力

使用XML创建Excel文档-.NET教程,XML应用

建站超值云服务器,限时71元/月

 

原例子使用vb.net写的,以下的用c#改写的代码

原文代码:

http://www.gotdotnet.com/community/usersamples/details.aspx?sampleguid=ddbaecb9-a260-4656-9f22-300b6a1ce66c

本例使用xml来创建excel文档,但在运行时不需要安装excel程序。

            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);
            }

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 使用XML创建Excel文档-.NET教程,XML应用
分享到: 更多 (0)