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

摘要:
xmlversion=\“1.0\”?˃“);writer.WriteLine(”˂?mso applicationprogid=\“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();
    }

免责声明:文章转载自《ASP.NET用XML的方式导出到excel多sheet的实现方式》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇NVIDIA安倍架构腾讯云对象存储权限管理操作指南下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C#中对EXCEL保存的SAVEAS方法说明

object missing = System.Reflection.Missing.Value;myWorkbook.SaveAs(fileallpath,Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,missing,missing,missing,missing,Micro...

input="file" 浏览时只显示指定excel文件,筛选特定文件类型

<p>显示 .xls, .xlsx, .csv 文件...</p> <input type="file" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" runa...

详解如何利用FarPoint Spread表格控件来构造Winform的Excel表格界面输入

我们先来简单了解一下WinForm和FarPoint,WinForm是·Net开发平台中对Windows Form的一种称谓。而FarPoint是一款模拟EXCEL的控件。它可以根据用户的要求实现很大部份的EXCEL操作,且包括多个子表、表格风格定义、公式计算、排序、分组等等都可以实现。本文主要是介绍利用FarPoint Spread表格控件来构造Winf...

【Word&amp;amp;Excel】【2】excel把公式生成的#value!去掉

前言:用公式统一处理数据的时候,一些位置计算会得到#value!,需要改为空。此时搜索该单词并替换是没用的 正文: 使用时改为=IFERROR(原公式,"")即可 参考博客: excel怎样把#value!去掉? - 知乎https://www.zhihu.com/question/30576339...

Excel删除重复数据及用公式筛选重复项并标记颜色突出显示

当表格记录比较多时,常常会有重复数据,而重复记录往往只希望保存一条,因此需要把多余的删除;在 Excel 中,删除重复数据有两种方法,一种是用“删除重复数据”删除,另一种是用“高级筛选”删除;其中前者删除重复数据后会在表格末尾留下空行,而后者不会。如果只想查看重复数据而不必删除,可以给重复数据标记颜色突出显示,这样会一目了然。另外,还可以用公式统计每条重复...

tp5.1 使用PhpSpreadsheet 读写excel

PhpSpreadsheet是一个用纯PHP编写的库,提供了一组类,使您可以读取和写入不同的电子表格文件格式,例如Excel和LibreOffice Calc。 官方文档:https://phpspreadsheet.readthedocs.io/en/latest/ php 版本要求 使用PhpSpreadsheet开发的PHP 7.2或更高版本。 其他...