ASP.NET用XML的方式导出到excel多sheet的实现方式

private void ToExcel(String FileName)
    {
        //要转换的XML文件

        DataSet dsBook = new DataSet();
        dsBook = new Select().Select_();
        int rows = dsBook.Tables[0].Rows.Count + 1;
        int cols = dsBook.Tables[0].Columns.Count;

        //将要生成的Excel文件 
        string ExcelFileName = System.AppDomain.CurrentDomain.BaseDirectory + "\\ExcelFile\\"+FileName+".xls";
        if (File.Exists(ExcelFileName))
        {
            File.Delete(ExcelFileName);
        }
        StreamWriter writer = new StreamWriter(ExcelFileName, false);

        writer.WriteLine("<?xml version=\"1.0\"?>");
        writer.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>");
        writer.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
        writer.WriteLine(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
        writer.WriteLine(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
        writer.WriteLine(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
        writer.WriteLine(" xmlns:html=\"http://www.w3.org/TR/REC-html40/\">");
        writer.WriteLine(" <DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">");
        writer.WriteLine(" <Author>Automated Report Generator Example</Author>");
        writer.WriteLine(string.Format(" <Created>{0}T{1}Z</Created>", DateTime.Now.ToString("yyyy-mm-dd"), DateTime.Now.ToString("HH:MM:SS")));
        writer.WriteLine(" <Company>minyou</Company>");
        writer.WriteLine(" <Version>11.6408</Version>");
        writer.WriteLine(" </DocumentProperties>");

        writer.WriteLine(" <ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">");
        writer.WriteLine(" <WindowHeight>8955</WindowHeight>");
        writer.WriteLine(" <WindowWidth>11355</WindowWidth>");
        writer.WriteLine(" <WindowTopX>480</WindowTopX>");
        writer.WriteLine(" <WindowTopY>15</WindowTopY>");
        writer.WriteLine(" <ProtectStructure>False</ProtectStructure>");
        writer.WriteLine(" <ProtectWindows>False</ProtectWindows>");
        writer.WriteLine(" </ExcelWorkbook>");

        writer.WriteLine(" <Styles>");
        writer.WriteLine(" <Style ss:ID=\"Default\" ss:Name=\"Normal\">");
        writer.WriteLine("   <Alignment ss:Vertical=\"Bottom\"/>");
        writer.WriteLine("   <Borders/>");
        writer.WriteLine("   <Font/>");
        writer.WriteLine("   <Interior/>");
        writer.WriteLine("   <Protection/>");
        writer.WriteLine(" </Style>");
        writer.WriteLine(" <Style ss:ID=\"s21\">");
        writer.WriteLine("   <Alignment ss:Vertical=\"Bottom\" ss:WrapText=\"1\"/>");
        writer.WriteLine(" </Style>");
        writer.WriteLine(" </Styles>");
        //第一个表
        writer.WriteLine(" <Worksheet ss:Name=\"有油机\">");
        writer.WriteLine(string.Format(" <Table ss:ExpandedColumnCount=\"{0}\" ss:ExpandedRowCount=\"{1}\" x:FullColumns=\"1\"", cols.ToString(), rows.ToString()));
        writer.WriteLine("   x:FullRows=\"1\">");


        //生成标题
        writer.WriteLine("<Row>");
        foreach (DataColumn eachCloumn in dsBook.Tables[0].Columns)
        {
            writer.Write("<Cell ss:StyleID=\"s21\"><Data ss:Type=\"String\">");
            writer.Write(eachCloumn.ColumnName.ToString());
            writer.WriteLine("</Data></Cell>");
        }
        writer.WriteLine("</Row>");

        //生成数据记录
        foreach (DataRow eachRow in dsBook.Tables[0].Rows)
        {
            writer.WriteLine("<Row>");
            for (int currentRow = 0; currentRow != cols; currentRow++)
            {
                writer.Write("<Cell ss:StyleID=\"s21\"><Data ss:Type=\"String\">");
                writer.Write(eachRow[currentRow].ToString());
                writer.WriteLine("</Data></Cell>");
            }
            writer.WriteLine("</Row>");
        }
        writer.WriteLine(" </Table>");

        writer.WriteLine(" <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
        writer.WriteLine("   <Selected/>");
        writer.WriteLine("   <Panes>");
        writer.WriteLine("    <Pane>");
        writer.WriteLine("     <Number>3</Number>");
        writer.WriteLine("     <ActiveRow>1</ActiveRow>");
        writer.WriteLine("    </Pane>");
        writer.WriteLine("   </Panes>");
        writer.WriteLine("   <ProtectObjects>False</ProtectObjects>");
        writer.WriteLine("   <ProtectScenarios>False</ProtectScenarios>");
        writer.WriteLine(" </WorksheetOptions>");
        writer.WriteLine(" </Worksheet>");

        //第二个表
        writer.WriteLine(" <Worksheet ss:Name=\"Sheet2\">");
        writer.WriteLine(" <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
        writer.WriteLine("   <ProtectObjects>False</ProtectObjects>");
        writer.WriteLine("   <ProtectScenarios>False</ProtectScenarios>");
        writer.WriteLine(" </WorksheetOptions>");
        writer.WriteLine(" </Worksheet>");

        //第三个表
        writer.WriteLine(" <Worksheet ss:Name=\"Sheet3\">");
        writer.WriteLine(" <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
        writer.WriteLine("   <ProtectObjects>False</ProtectObjects>");
        writer.WriteLine("   <ProtectScenarios>False</ProtectScenarios>");
        writer.WriteLine(" </WorksheetOptions>");
        writer.WriteLine(" </Worksheet>");
        writer.WriteLine("</Workbook>");
        writer.Close();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(ExcelFileName));
        Response.ContentType = "application/ms-excel";// 指定返回的是一个不能被客户端读取的流,必须被下载
        Response.WriteFile(ExcelFileName); // 把文件流发送到客户端
        Response.End();
    }
posted @ 2013-01-28 21:41  盛开的雨季  阅读(446)  评论(0编辑  收藏  举报