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

页面打开excel的更多相关文章

  1. 使EditText不要在页面打开时自动获取焦点(因为软键盘会自动弹出)

    当页面上有EditText时,第一个EditText会在页面打开时自动的获取焦点,这样带来的问题就是系统键盘会自动的弹出来. 解决方法比较简单只要为EditText的父控件设置一下就行了: andro ...

  2. 麦咖啡阻挡正常打开Excel文件

    双击打开Excel文件,提示如下图: Excel文件被麦咖啡做阻挡,无法正常打开 处理方案: 过一会儿还是出现此问题,干脆就把缓冲区保护给禁用掉

  3. Outlook不能预览和打开Excel文件:

    无法打开Outlook邮箱中的Excel附件,确实让人恼火 先不要着急: 1.在开始->运行,输入"regedit" 2.找到路径:HKEY_CURRENT_USER\Sof ...

  4. C#中的Excel操作【1】——设置Excel单元格的内容,打开Excel文件的一种方式

    前言 作为项目管理大队中的一员,在公司里面接触最多的就是Excel文件了,所以一开始就想从Excel入手,学习简单的二次开发,开始自己的编程之路! 程序界面 功能说明 打开文件按钮,可以由使用者指定要 ...

  5. 打开Excel的报错,提示:不能使用对象链接和嵌入

    计算机这几天在打开Excel文档的时候,提示:不能使用对象链接和嵌入, 而且出现如下的提示:Microsoft Office Excel进程已停止工作, 每次打开Excel的时候都是同样的问题,害我跟 ...

  6. 页面打开 抛出w3wp.exe 中发生未处理异常

    页面打开 抛出w3wp.exe 中发生未处理异常

  7. 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格式不一致的解决办法

    -----转载:http://blog.csdn.net/sgear/article/details/7663502 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格 ...

  8. VB中后台打开Excel文件实现代码

    某些时候需要打开Excel文件来获取或者写入数据,但又不希望跳出打开的Excel文件窗口,可以用下面的代码: Dim eb As New excel.Application, wb as excel. ...

  9. VUE路由新页面打开的方法总结

    平常做单页面的场景比较多,所以大部分的业务是在同一个页面进行跳转.要通过VUE路由使用新页面打开且传递参数,可以采用以下两个方法: 1.router-link的target <router-li ...

随机推荐

  1. js 自定义加减乘除方法(防止js自身计算错误)

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  2. Angular Material 学习笔记 Chips

    依据 material guidelines, chips 可以用来做 filter https://material.io/design/components/chips.html#filter-c ...

  3. easyExcel用于导入导出

    1.添加依赖: <!-- 现在已经更新到1.1.2-beta5 --> <dependency> <groupId>com.alibaba</groupId& ...

  4. vscode IIsExpress用法

    最近前端调试项目,都要安装IIS,使用IIS Express插件不需要另外在IIS架设站点,方便使用 1.安装IIS Express插件 2.ctrl+shfit+p 启动IIS Express 命令 ...

  5. linux Ubuntu14.04 make编译文件报错:No rule to make target `/usr/lib/libpython2.7.so', needed by `python/_pywraps2.so'. Stop.

    错误过程:当“make”编译文件时报错No rule to make target `/usr/lib/libpython2.7.so', needed by `python/_pywraps2.so ...

  6. restTemplate源码解析(目录)

    restTemplate是spring实现的,基于restful风格的http请求模板.使用restTemplate可以简化请求操作的复杂性,同时规范了代码风格.本系列文章,将根据以下目录顺序,从源码 ...

  7. iOS - 外包开发常用第三方库(1)

    一:第三方插件1:基于响应式编程思想的oc地址:https://github.com/ReactiveCocoa/ReactiveCocoa2:hud提示框地址:https://github.com/ ...

  8. 【转载】为什么我的网站加www是打不开的呢

    在访问网站的过程中,我们发现有些网站访问不带www的主域名可以正常访问,反而访问加www的域名打不开,那为什么有的网站加www是打不开的呢?此情况很大可能是因为没有解析带www的域名记录或者主机Web ...

  9. java的输入流中的两个坑

    1.输入流的所有实例中的read()方法皆是阻塞的. 输入流只会在发生错误或者异常关闭的时候回返回-1,如果流中没有数据,不会返回-1而是一直阻塞. 2.BufferedReader的readLine ...

  10. javascript_10-函数

    函数 //定义函数 0-100 相加 function getSum() { var sum = 0; for (let i = 1; i <= 100; i++) { sum += i; } ...