zoukankan      html  css  js  c++  java
  • 页面打开excel

    1. File => Stream / MemoryStream


    FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
                    try
                    {
                        MemoryStream stream2 = new MemoryStream();
                        try
                        {
                            byte[] buffer = new byte[0x1000];
                            int count = 0;
                            while ((count = stream.Read(buffer, 0, 0x1000)) > 0)
                            {
                                stream2.Write(buffer, 0, count);
                            }
                            if ((stream2 != null) && (stream2.Length > 0L))
                            {
                                return stream2;
                            }
                        }
                        catch
                        {
                            stream2.Close();
                        }
                    }
                    finally
                    {
                        if (stream != null)
                        {
                            stream.Close();
                        }
                        exporter.RemoveTempFiles();
                    }
                   
    2. String => File


                 FileStream fs = null;
                 string path = MapPath("~/" + summaryType + ".CSV");  
                 string content = csvHelper.ToString();
               //  fs = File.Create(path);
              //   fs.Write(System.Text.Encoding.Default.GetBytes(content), 0, content.Length);
                // fs.Write(System.Text.Encoding.UTF8.GetBytes(content), 0, content.Length);
               //  fs.Close();
                

                 fs = new FileStream(path, FileMode.Create, FileAccess.Write);
                 StreamWriter sw = null;
                 if (System.Text.Encoding.Default == System.Text.Encoding.GetEncoding("GB2312"))
                     sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));
                 else
                     sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
                
                 sw.Write(content);
                 sw.Close();
                
    3. File => IE open

    ExportDataToCSV(summaryType, type, days);  //Generate the excel
                    string path = MapPath("~/" + summaryType + ".CSV");
                    byte[] result = File.ReadAllBytes(path);

                    //  Response.ContentType = "application/vnd.ms-excel";
                    Response.ContentType = "text/CSV";
                    Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", summaryType + ".CSV"));
                    if (System.Text.Encoding.Default == System.Text.Encoding.GetEncoding("GB2312"))
                        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                    else
                        Response.ContentEncoding = System.Text.Encoding.UTF8;

                    // Response.BinaryWrite(new byte[] { 0xEF, 0xBB, 0xBF });
                    Response.BinaryWrite(result);
                   
                    if (File.Exists(path) == true)
                    {
                        File.Delete(path);   //Generate the excel
                    }
                    Response.End();

  • 相关阅读:
    Windows 环境下配置 git bash 的 HOME 默认路径
    SQL Server 2008 r2 输入SQL语句不能自动提示的解决办法
    搭建调用 WebService 的 ASP.NET 网站 (VS2010, C#)
    【问题解决】线程间操作无效:从不是创建控件“textBox1”的线程访问它
    使用 hexdump dump 文件内容
    调用 WebService 浏览器提示 500 (Internal Server Error) 的原因及解决办法
    PHP的文件格式应该以UTF-8无BOM编码
    继电器电路
    ROS零门槛教程系列(二)——Linux常用指令:mkdir、tar、 unzip、cp、scp、mv、rm、find、apt、ssh
    ROS零门槛教程系列(一)——ubuntu安装
  • 原文地址:https://www.cnblogs.com/sui84/p/6777036.html
Copyright © 2011-2022 走看看