• .net中导出excel时不能自动显示网格线的解决方案


    protected void btnExport_Click(object sender, EventArgs e)
        {
            MemoryStream stream = new MemoryStream();
            this.OlapGrid1.Export(stream);
            StreamReader streamReader = new System.IO.StreamReader(stream);
            stream.Position = 0;
            string data = streamReader.ReadToEnd();
            Response.Clear();
            Response.Charset = "UTF-8";
            Response.ContentEncoding = System.Text.Encoding.Default;
            // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
            Response.AddHeader("Content-Disposition", "attachment; filename=Model.xls");
            // 添加头信息,指定文件大小,让浏览器能够显示下载进度
            Response.AddHeader("Content-Length", data.Length.ToString());
            // 指定返回的是一个不能被客户端读取的流,必须被下载
            Response.ContentType = "application/ms-excel";
            Response.Write("<style type='text/css'>td{text-align:left;border:solid 1px Black;}</style>");
            Response.Write(AddExcelHead()); //显示网格线
            Response.Write(data);//导出数据
            Response.Write(AddExcelbottom());//显示网格线
            Response.Flush();
            Response.End();
        }

        public static string AddExcelHead()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
            sb.Append(" <head>");
            sb.Append(" <!--[if gte mso 9]><xml>");
            sb.Append("<x:ExcelWorkbook>");
            sb.Append("<x:ExcelWorksheets>");
            sb.Append("<x:ExcelWorksheet>");
            sb.Append("<x:Name></x:Name>");
            sb.Append("<x:WorksheetOptions>");
            sb.Append("<x:Print>");
            sb.Append("<x:ValidPrinterInfo />");
            sb.Append(" </x:Print>");
            sb.Append("</x:WorksheetOptions>");
            sb.Append("</x:ExcelWorksheet>");
            sb.Append("</x:ExcelWorksheets>");
            sb.Append("</x:ExcelWorkbook>");
            sb.Append("</xml>");
            sb.Append("<![endif]-->");
            sb.Append(" </head>");
            sb.Append("<body>");
            return sb.ToString();

        }


        public static string AddExcelbottom()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("</body>");
            sb.Append("</html>");
            return sb.ToString();
        }

  • 相关阅读:
    import()函数
    node-sass安装报错
    npm 安装扩展模块时,因缓存报错的问题汇总
    测试
    export default 和 export 区别
    正则
    物联网
    第十二次课堂总结
    第十二次作业
    作业10
  • 原文地址:https://www.cnblogs.com/yhyblog/p/2112962.html
Copyright © 2020-2023  润新知