c# 导出Excel Xml格式 不用安装Excel也不用引入任何组件

c# 导出Excel Xml格式 不用安装Excel也不用引入任何组件

public static void CreateExcel(string filepath, int roms, int troms, int zroms, string strdate, Model.Form8 form8)
    {
        if (filepath != "")
        {
            StreamWriter rw = new StreamWriter(filepath, false, Encoding.GetEncoding("UTF-8"), 10240);
            rw.WriteLine("<?xml version=/"1.0/"?>");
            rw.WriteLine("<?mso-application progid=/"Excel.Sheet/"?>");
            rw.WriteLine("<Workbook xmlns=/"urn:schemas-microsoft-com:office:spreadsheet/" xmlns:o=/"urn:schemas-microsoft-com:office:office/" xmlns:x=/"urn:schemas-microsoft-com:office:excel/" xmlns:ss=/"urn:schemas-microsoft-com:office:spreadsheet/" xmlns:html=/"http://www.w3.org/TR/REC-html40/">");
            rw.WriteLine("<DocumentProperties xmlns=/"urn:schemas-microsoft-com:office:office/">");
            rw.WriteLine("<Created>1996-12-17T01:32:42Z</Created>");
            rw.WriteLine("<LastSaved>2000-11-18T06:53:49Z</LastSaved>");
            rw.WriteLine("<Version>11.8107</Version>");
            rw.WriteLine("</DocumentProperties>");
            rw.WriteLine("<OfficeDocumentSettings xmlns=/"urn:schemas-microsoft-com:office:office/">");
            rw.WriteLine("<RemovePersonalInformation/>");
            rw.WriteLine("</OfficeDocumentSettings>");
            rw.WriteLine("<ExcelWorkbook xmlns=/"urn:schemas-microsoft-com:office:excel/">");
            rw.WriteLine("<WindowHeight>4530</WindowHeight>");
            rw.WriteLine("<WindowWidth>8505</WindowWidth>");
            rw.WriteLine("<WindowTopX>480</WindowTopX>");
            rw.WriteLine("<WindowTopY>120</WindowTopY>");
            rw.WriteLine("<AcceptLabelsInFormulas/>");
            rw.WriteLine("<ProtectStructure>False</ProtectStructure>");
            rw.WriteLine("<ProtectWindows>False</ProtectWindows>");
            rw.WriteLine("</ExcelWorkbook>");
            rw.WriteLine("<Styles>");
            rw.WriteLine("<Style ss:ID=/"Default/" ss:Name=/"Normal/">");
            rw.WriteLine("<Alignment ss:Vertical=/"Bottom/"/>");
            rw.WriteLine("<Borders/>");
            rw.WriteLine("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"12/"/>");
            rw.WriteLine("<Interior/>");
            rw.WriteLine("<NumberFormat/>");
            rw.WriteLine("<Protection/>");
            rw.WriteLine("</Style>");
            rw.WriteLine("<Style ss:ID=/"s24/">");
            rw.WriteLine("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"9/"/>");
            rw.WriteLine("<Alignment ss:Horizontal=/"Center/" ss:Vertical=/"Bottom/"/>");
            rw.WriteLine(" <Borders>");
            rw.WriteLine(" <Border ss:Position=/"Bottom/" ss:LineStyle=/"Continuous/" ss:Weight=/"1/"/>");
            rw.WriteLine("<Border ss:Position=/"Left/" ss:LineStyle=/"Continuous/" ss:Weight=/"1/"/>");
            rw.WriteLine("<Border ss:Position=/"Right/" ss:LineStyle=/"Continuous/" ss:Weight=/"1/"/>");
            rw.WriteLine("<Border ss:Position=/"Top/" ss:LineStyle=/"Continuous/" ss:Weight=/"1/"/>");
            rw.WriteLine("</Borders>");
            rw.WriteLine("<NumberFormat ss:Format=/"@/"/>");
            rw.WriteLine("</Style>");
            rw.WriteLine("<Style ss:ID=/"s25/">");
            rw.WriteLine("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"9/"/>");
            rw.WriteLine("<Alignment ss:Horizontal=/"Center/" ss:Vertical=/"Bottom/"/>");
            rw.WriteLine("<Borders>");
            rw.WriteLine(" <Border ss:Position=/"Bottom/" ss:LineStyle=/"Continuous/" ss:Weight=/"1/"/>");
            rw.WriteLine(" <Border ss:Position=/"Left/" ss:LineStyle=/"Continuous/" ss:Weight=/"1/"/>");
            rw.WriteLine(" <Border ss:Position=/"Right/" ss:LineStyle=/"Continuous/" ss:Weight=/"1/"/>");
            rw.WriteLine(" <Border ss:Position=/"Top/" ss:LineStyle=/"Continuous/" ss:Weight=/"1/"/>");
            rw.WriteLine("</Borders>");
            rw.WriteLine("</Style>");
            rw.WriteLine("<Style ss:ID=/"s26/">");
            rw.WriteLine("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"9/"/>");
            rw.WriteLine("<Alignment ss:Horizontal=/"Right/" ss:Vertical=/"Bottom/"/>");
            rw.WriteLine("</Style>");
            rw.WriteLine("<Style ss:ID=/"s27/">");
            rw.WriteLine("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"9/"/>");
            rw.WriteLine("<Alignment ss:Horizontal=/"Left/" ss:Vertical=/"Bottom/"/>");
            rw.WriteLine("</Style>");
            rw.WriteLine("<Style ss:ID=/"m24861836/">");
            rw.WriteLine("<Alignment ss:Horizontal=/"Center/" ss:Vertical=/"Center/"/>");
            rw.WriteLine("<Borders/>");
            rw.WriteLine("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"20/" ss:Bold=/"1/"/>");
            rw.WriteLine("<NumberFormat/>");
            rw.WriteLine("<Protection ss:Protected=/"0/"/>");
            rw.WriteLine("</Style>");           
            rw.WriteLine("<Style ss:ID=/"s28/">");
            rw.WriteLine("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"9/"/>");
            rw.WriteLine("<Alignment ss:Horizontal=/"Left/" ss:Vertical=/"Bottom/"/>");
            rw.WriteLine("<NumberFormat ss:Format=/"@/"/>");
            rw.WriteLine("</Style>");
            rw.WriteLine("</Styles>");
            //sheet
            rw.WriteLine("<Worksheet ss:Name=/"Sheet1/">");
            rw.WriteLine("<Table x:FullColumns=/"1/" x:FullRows=/"1/" ss:DefaultColumnWidth=/"75/" ss:DefaultRowHeight=/"14.25/">");

            if (strdate != null)
            {
                string[] str = strdate.Split(' ̄');
                int cou = 1;
                //设置表头
                rw.WriteLine("<Row ss:Height=/"31.5/">");
                rw.WriteLine("<Cell ss:MergeAcross=/"7/" ss:StyleID=/"m24861836/"><Data ss:Type=/"String/">123123</Data></Cell>");
                rw.WriteLine("</Row>");

                rw.WriteLine("<Row>");
                rw.WriteLine("<Cell ss:StyleID=/"s26/"><Data ss:Type=/"String/">123123</Data></Cell>");
                rw.WriteLine("<Cell ss:StyleID=/"s27/"/>");
                rw.WriteLine("<Cell ss:StyleID=/"s26/"><Data ss:Type=/"String/">123</Data></Cell>");
                rw.WriteLine("<Cell ss:MergeAcross=/"2/" ss:StyleID=/"s27/"><Data ss:Type=/"String/">asdasd</Data></Cell>");
                rw.WriteLine("<Cell ss:StyleID=/"s26/"><Data ss:Type=/"String/">123123</Data></Cell>");
                rw.WriteLine("<Cell ss:StyleID=/"s28/"><Data ss:Type=/"Number/">123</Data></Cell>");
                rw.WriteLine("</Row>");
                rw.WriteLine("<Row>");
                rw.WriteLine("<Cell ss:MergeAcross=/"1/" ss:StyleID=/"s25/"><Data ss:Type=/"String/">q123</Data></Cell>");
                rw.WriteLine("<Cell ss:StyleID=/"s25/"><Data ss:Type=/"String/">123</Data></Cell>");
                rw.WriteLine("<Cell ss:StyleID=/"s25/"><Data ss:Type=/"String/">123</Data></Cell>");
                rw.WriteLine("<Cell ss:StyleID=/"s25/"><Data ss:Type=/"String/">123</Data></Cell>");
                rw.WriteLine("<Cell ss:StyleID=/"s25/"><Data ss:Type=/"String/">123</Data></Cell>");
                rw.WriteLine("<Cell ss:StyleID=/"s25/"><Data ss:Type=/"String/">123</Data></Cell>");
                rw.WriteLine("<Cell ss:StyleID=/"s25/"><Data ss:Type=/"String/">123</Data></Cell>");
                rw.WriteLine("</Row>");

                for (int i = 0; i < roms; i++)
                {
                    rw.WriteLine("<Row>");
                    for (int j = 0; j < 8; j++)
                    {
                        if (j == 0)
                        {
                            if (i < troms)
                            {
                                rw.WriteLine("<Cell ss:StyleID=/"s25/"><Data ss:Type=/"String/">123</Data></Cell>");
                            }
                            if (i >= troms && i < (troms + zroms))
                            {
                                rw.WriteLine("<Cell ss:StyleID=/"s25/"><Data ss:Type=/"String/">123</Data></Cell>");
                            }
                        }
                        else
                        {
                            if (i < (troms + zroms))
                            {
                                try
                                {

                                    if (isNum(str[cou]))
                                    {
                                        rw.WriteLine("<Cell ss:StyleID=/"s24/"><Data ss:Type=/"Number/">" + str[cou] + "</Data></Cell>");
                                    }
                                    else
                                    {
                                        rw.WriteLine("<Cell ss:StyleID=/"s25/"><Data ss:Type=/"String/">" + str[cou] + "</Data></Cell>");
                                    }
                                    cou++;
                                }
                                catch (Exception ex) { }
                            }
                        }
                    }
                    rw.WriteLine("</Row>");
                }
            }
            //设置表尾
            rw.WriteLine("<Row>");
            rw.WriteLine("<Cell/>");
            rw.WriteLine("<Cell ss:StyleID=/"s26/"><Data ss:Type=/"String/">123:</Data></Cell>");
            rw.WriteLine("<Cell ss:StyleID=/"s27/"><Data ss:Type=/"String/">" + form8.P302 + "</Data></Cell>");
            rw.WriteLine("<Cell ss:StyleID=/"s26/"><Data ss:Type=/"String/">123:</Data></Cell>");
            rw.WriteLine("<Cell ss:StyleID=/"s27/"><Data ss:Type=/"String/">" + form8.P303 + "</Data></Cell>");
            rw.WriteLine("<Cell ss:StyleID=/"s26/"><Data ss:Type=/"String/">123:</Data></Cell>");
            rw.WriteLine("<Cell ss:StyleID=/"s27/"><Data ss:Type=/"String/">" + form8.P304 + "</Data></Cell>");
            rw.WriteLine("</Row>");

            rw.WriteLine("</Table>");
            rw.WriteLine("<WorksheetOptions xmlns=/"urn:schemas-microsoft-com:office:excel/">");
            rw.WriteLine("<Selected/>");
            rw.WriteLine("<ProtectObjects>False</ProtectObjects>");
            rw.WriteLine("<ProtectScenarios>False</ProtectScenarios>");
            rw.WriteLine("</WorksheetOptions>");
            rw.WriteLine("</Worksheet>");

            rw.WriteLine("<Worksheet ss:Name=/"Sheet2/">");
            rw.WriteLine("<Table ss:ExpandedColumnCount=/"0/" ss:ExpandedRowCount=/"0/" x:FullColumns=/"1/" x:FullRows=/"1/" ss:DefaultColumnWidth=/"54/" ss:DefaultRowHeight=/"14.25/"/>");
            rw.WriteLine("<WorksheetOptions xmlns=/"urn:schemas-microsoft-com:office:excel/">");
            rw.WriteLine("<Selected/>");
            rw.WriteLine("<ProtectObjects>False</ProtectObjects>");
            rw.WriteLine("<ProtectScenarios>False</ProtectScenarios>");
            rw.WriteLine("</WorksheetOptions>");
            rw.WriteLine("</Worksheet>");

            rw.WriteLine("<Worksheet ss:Name=/"Sheet3/">");
            rw.WriteLine("<Table ss:ExpandedColumnCount=/"0/" ss:ExpandedRowCount=/"0/" x:FullColumns=/"1/" x:FullRows=/"1/" ss:DefaultColumnWidth=/"54/" ss:DefaultRowHeight=/"14.25/"/>");
            rw.WriteLine("<WorksheetOptions xmlns=/"urn:schemas-microsoft-com:office:excel/">");
            rw.WriteLine("<Selected/>");
            rw.WriteLine("<ProtectObjects>False</ProtectObjects>");
            rw.WriteLine("<ProtectScenarios>False</ProtectScenarios>");
            rw.WriteLine("</WorksheetOptions>");
            rw.WriteLine("</Worksheet>");
            //sheet
            rw.WriteLine("</Workbook>");
            rw.Flush();
            rw.Close();
        }
    }

阅读更多
想对作者说点什么? 我来说一句

C#代码操作Excel,不需要安装office

2016年01月14日 964KB 下载

.net不安装office导出excel

2014年08月29日 95KB 下载

C# winform/web导出EXCEL

2011年05月24日 43KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭