zoukankan      html  css  js  c++  java
  • ASP.NET用XML的方式导出到excel多sheet的实现方式

     考虑到用com组件太慢,用xml是最佳实践

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

  • 相关阅读:
    Python开发【第二十一篇】:Web框架之Django【基础】
    梳理
    Python Day15 jQuery
    day12 html基础
    Python day11 Mysql
    锻炼马甲线
    第一章:软件性能测试基本概念
    第4关—input()函数
    第3关—条件判断与嵌套
    第2关—数据类型与转换
  • 原文地址:https://www.cnblogs.com/tian_z/p/1774308.html
Copyright © 2011-2022 走看看