internal class DownloadHandler : IDownloadHandler
    {
        public DownloadHandler()
        {
        }

public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
        {
            WebBrowser ie = new WebBrowser();
            ie.Navigate(downloadItem.Url);

string strrtn = System.Web.HttpUtility.UrlDecode(downloadItem.Url);

if (strrtn.Contains("data:text/csv;charset=utf-8,"))
            {
                strrtn = strrtn.Replace("data:text/csv;charset=utf-8,", "");

string[] striparr = strrtn.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                DataTable dt = new DataTable();

string[] strcolhead = striparr[0].Split(',');
                foreach (string str in strcolhead)
                {
                    dt.Columns.Add(str);
                }
                for (int i = 1; i < striparr.Length; i++)
                {
                    DataRow dr = dt.NewRow();
                    string[] array = striparr[i].Split(',');
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        dr[j] = array[j];
                    }
                    dt.Rows.Add(dr);
                }

if (dt != null && dt.Rows.Count > 0)
                {

#region   验证可操作性

//申明保存对话框
                    SaveFileDialog dlg = new SaveFileDialog();
                    //默然文件后缀
                    dlg.DefaultExt = "xls";
                    //文件后缀列表
                    dlg.Filter = "EXCEL文件(*.XLS)|*.xls";
                    //默然路径是系统当前路径
                    //dlg.InitialDirectory = Directory.GetCurrentDirectory();
                    dlg.FileName = "管网设备.xls";
                    //打开保存对话框
                    if (dlg.ShowDialog() == DialogResult.Cancel) return;
                    //返回文件路径
                    string fileNameString = dlg.FileName;
                    //验证strFileName是否为空或值无效
                    if (string.IsNullOrEmpty(fileNameString.Trim()))
                    { return; }
                    //验证strFileName是否包含文件后缀            
                    if (fileNameString.IndexOf(".") > 0)
                    {
                        string ext = fileNameString.Substring(fileNameString.LastIndexOf(".") + 1, fileNameString.Length - (fileNameString.LastIndexOf(".") + 1));

if (ext.Trim() != "xls" && ext.Trim() != "XLS" && ext.Trim() != "Xls" && ext.Trim() != "XLs" && ext.Trim() != "xLS" && ext.Trim() != "xlS")
                        {
                            MessageBox.Show("保存Excel文件名称错误", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }
                    string fileNameExt = fileNameString.Substring(fileNameString.LastIndexOf("\\") + 1);
                    if (fileNameExt.IndexOf(".") == 0)
                    {
                        MessageBox.Show("请输入文件名", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    //定义表格内数据的行数和列数
                    int rowscount = dt.Rows.Count;
                    int colscount = dt.Columns.Count;
                    //行数必须大于0
                    if (rowscount <= 0)
                    {
                        MessageBox.Show("没有数据可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

//列数必须大于0
                    if (colscount <= 0)
                    {
                        MessageBox.Show("没有数据可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

//行数不可以大于65536
                    if (rowscount > 65536)
                    {
                        MessageBox.Show("数据记录数太多(最多不能超过65536条),不能保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

//列数不可以大于255
                    if (colscount > 255)
                    {
                        MessageBox.Show("数据记录行数太多,不能保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

//验证以fileNameString命名的文件是否存在,如果存在删除它
                    FileInfo file = new FileInfo(fileNameString);
                    if (file.Exists)
                    {
                        try
                        {
                            file.Delete();
                        }
                        catch (Exception error)
                        {
                            MessageBox.Show(error.Message, "删除失败 ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }
                    #endregion

try
                    {
                        GemBoxExcelLiteHelper.SaveToXls(fileNameString, dt);
                    }
                    catch (Exception error)
                    {
                        MessageBox.Show(error.Message, "警告 ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    MessageBox.Show(fileNameString + "\n\n导出完毕! ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);

}
                else
                {
                    SysMessageBox.Error("无数据");
                }

}

}
        public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
        {

}
        public bool OnDownloadUpdated(CefSharp.DownloadItem downloadItem)
        {
            return false;
        }
    }

internal class ImageDownloadHandler : IDownloadHandler
    {
        //public bool OnBeforeBrowse(IWebBrowser browser, IRequest request, NavigationType naigationvType, bool isRedirect)
        //{
        //    if (url.Contains("application/vnd.ms-excel;base64"))
        //    {
        //        string tmpContent = url;//获取传递上来的文件内容
        //        string contentHead = "data:application/vnd.ms-excel;base64,";
        //        int startIndex = tmpContent.IndexOf(contentHead);
        //        int name_StartIndex = tmpContent.IndexOf(contentHead) + contentHead.Length;
        //        int name_EndIndex = tmpContent.IndexOf('#');
        //        string fileName = "Excel表格";
        //        if (name_EndIndex != -1)
        //        {
        //            fileName = Uri.UnescapeDataString(tmpContent.Substring(name_StartIndex, name_EndIndex - name_StartIndex));
        //            tmpContent = tmpContent.Substring(name_EndIndex + 1);
        //        }
        //        else
        //        {
        //            tmpContent = tmpContent.Substring(name_StartIndex);
        //        }
        //        byte[] output = Convert.FromBase64String(tmpContent);
        //        SaveFileDialog dialog = new SaveFileDialog();
        //        dialog.FileName = fileName + ".xls";
        //        dialog.Filter = "(Excel文件)|*.xls";
        //        DialogResult result = dialog.ShowDialog();
        //        if (result == DialogResult.OK)
        //        {
        //            using (FileStream fs = new FileStream(dialog.FileName, FileMode.Create, FileAccess.Write))
        //            {
        //                fs.Write(output, 0, output.Length);
        //                fs.Flush();
        //            }
        //            return true;
        //        }
        //    }
        //    return false;
        //}
       
        public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
        {

if (!callback.IsDisposed)
            {
                using (callback)
                {
                    callback.Continue(@"C:\Users\" +
                            System.Security.Principal.WindowsIdentity.GetCurrent().Name +
                            @"\Downloads\" +
                            downloadItem.SuggestedFileName,
                        showDialog: true);
                }
            }
        }

public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
        {

}

public bool OnDownloadUpdated(CefSharp.DownloadItem downloadItem)
        {
            return false;
        }
    }

WEB页面下载内容导出excel的更多相关文章

  1. PHP关于web页面交互内容

    学php学了有一段时间了总结总结给大家分享一下 PHP中的引用 第一段程序: <?php $first_name="firstName"; $first=&$firs ...

  2. 在ASP.NET根据DataTable中的内容导出Excel

    前台代码: <asp:Button ID="btnExcel" runat="server" Text="Excel导出" CssCl ...

  3. js灵活打印web页面区域内容的通用方法

      我们做网站,经常需要打印页面指定区域的内容,而网上关于这块的说法很多,各种各样的打印控件也不少.但许多打印方案都不怎么好,至少我不喜欢,要么封装复杂,要么难以维护.正好现在的项目也需要用到 ...

  4. 使用web API和NPOI导出Excel

    使用MVC controller输出excel的例子,自不待言,例子满天飞. 由于本项目使用的是Asp.net MVC API,因此在本项目使用API,实现了文件下载功能.代码的原理很简单,基本上是老 ...

  5. 将页面的内容导出使用html2canvas+jsPDF

    第一首先是要引用 import jsPDF from 'jspdf' import html2canvas from 'html2canvas' import PDFJS from 'pdfjs-di ...

  6. web页面的数据从excel中读取

    # -*- coding: utf-8 -*- import xdrlib ,sysimport xlrdimport datetimeimport jsonimport conf,reimport ...

  7. Microsoft.Office.Interop.Excel的用法以及利用Microsoft.Office.Interop.Excel将web页面转成PDF

    1.常见用法           using Microsoft.Office.Interop.Excel; 1)新建一个Excel ApplicationClass ExcelApp = New A ...

  8. 利用Microsoft.Office.Interop.Excel 将web页面转成PDF

    网上有很多将Web页面转成PDF的方法,还有许多收费的第三方插件.其实利用Office 自带的将EXCEL发布成PDF的功能就可以实现,如果你的需求没有多复杂,可以采用笔者的方法. 首先将web页面h ...

  9. jxl导出Excel文件

    一.java项目实现读取Excel文件和导出Excel文件 实现读取和导出Excel文件的代码: package servlet; import java.io.FileInputStream; im ...

随机推荐

  1. CountDownLatch与CyclicBarrier的基本使用

    1 概述 CountDownLatch以及CyclicBarrier都是Java里面的同步工具之一,本文介绍了两者的基本原理以及基本使用方法. 2 CountDownLatch CountDownLa ...

  2. [ERROR]: gitstatus failed to initialize.

    1 问题描述 Manjaro升级后,zsh的主题p10k出现的问题. Your git prompt may disappear or become slow. Run the following c ...

  3. synchronized锁由浅入深解析

    一:几种锁的概念 1.1 自旋锁 自旋锁,当一个线程去获取锁时,如果发现锁已经被其他线程获取,就一直循环等待,然后不断的判断是否能够获取到锁,一直到获取到锁后才会退出循环. 1.2 乐观锁 乐观锁,是 ...

  4. scrapy爬虫框架调用百度地图api数据存入数据库

    scrapy安装配置不在本文 提及, 1.在开始爬取之前,必须创建一个新的Scrapy项目.进入自定义的项目目录中,运行下列命令 scrapy startproject mySpider 其中, my ...

  5. 1040 Longest Symmetric String

    Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...

  6. CPF 入门教程 - 属性和事件(七)

    CPF C#跨平台桌面UI框架 系列教程 CPF 入门教程(一) CPF 入门教程 - 数据绑定和命令绑定(二) CPF 入门教程 - 样式和动画(三) CPF 入门教程 - 绘图(四) CPF 入门 ...

  7. nginx下强制跳转到www域名

    跳转www #先监听 exp.com域名,然后转发到www下面 server { listen 80; server_name exp.com; rewrite ^(.*) $scheme://www ...

  8. 认识二进制安全与漏洞攻防技术 (Windows平台)

    二进制漏洞是指程序存在安全缺陷,导致攻击者恶意构造的数据(如Shellcode)进入程序相关处理代码时,改变程序原定的执行流程,从而实现破坏或获取超出原有的权限. 0Day漏洞 在计算机领域中,0da ...

  9. hdu4911 简单树状数组

    题意:      给你一串数字,然后给你最多进行k次交换(只能交换相邻的)问交换后的最小逆序数是多少. 思路:      首先要知道的一个就是给你一个序列,每次只能交换相邻的位置,把他交换成一个递增序 ...

  10. POJ2688状态压缩(可以+DFS剪枝)

    题意:       给你一个n*m的格子,然后给你一个起点,让你遍历所有的垃圾,就是终点不唯一,问你最小路径是多少? 思路:       水题,方法比较多,最省事的就是直接就一个BFS状态压缩暴搜就行 ...