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. 使用 EPPlus 封装的 excel 表格导入功能 (二) delegate 委托 --永远滴神

    使用 EPPlus 封装的 excel 表格导入功能 (二) delegate 委托 --永远滴神 前言 接上一篇 使用 EPPlus 封装的 excel 表格导入功能 (一) 前一篇的是大概能用但是 ...

  2. manjaro找不到默认键盘布局

    1 问题描述 manjaro安装fcitx后,没有默认的键盘布局,不是这样: 而是: 2 解决方案 解决方案在启动fcitx时就已经有提示了: 缺少了libjson-c这个库,直接使用pacman搜索 ...

  3. 分布式锁的实现之 redis 篇

    为什么需要分布式锁 引入经典的秒杀情景,100件商品供客户抢.如果是单机版的话,我们使用synchronized 或者 lock 都可以实现线程安全.但是如果多个服务器的话,synchronized ...

  4. I - 树的直径 POJ - 1383

    The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is d ...

  5. hdu3594 强连通 tarjan

    题意: 判断是不是强连通图 ,同时每一条边必须只能在一个环里 思路:之前我的强连通用的全是双深搜,结果题目的第二个要求很难判断,一开始写了三个深搜加上并查集,结果越写越乱,其实就是在判断一个边是否只在 ...

  6. hdu4920 矩阵乘法%3

    题意:      给你两个矩阵,让你求两矩阵的乘积,然后3取余.矩阵是n*n的,n<=800 思路:        如果什么都不考虑的话,矩阵的乘法是o(n^3)的,800*800*800 = ...

  7. 2.逆向分析Hello World!程序-上

    先写一个HelloWorld程序(vs2015 / C++) 编译链接生成可执行文件XX.exe,然后用OD[OllyDbg]打开调试: 代码窗口:默认用于显示反汇编代码,还用于各种注释.标签,分析代 ...

  8. 【前端】使用layui、layer父子frame传值

    前提: 半前后台分离,前后台都是使用JSON格式的数据进行交互.[化外音,这里我说半分离,是因为使用了themleaf模板进行路由.] 业务说明: 前端通用的逻辑是:列表展示数据,点击事件弹出fram ...

  9. Elastic-Job原理

    概述Elastic-Job是一个分布式调度解决方案,由两个相互独立的子项目Elastic-Job-Lite和Elastic-Job-Cloud组成. Elastic-Job-Lite定位为轻量级无中心 ...

  10. 支付宝手机端网页支付 PHP(基于官方提供的demo)

    1.支付宝开放平台添加应用并且签约快捷手机wap支付(应用添加不做详细说明) 2.下载demo,文档中心SDK&Demo, 3.个人中心秘钥管理,查看商户appID,商户私钥,支付宝公钥,商户 ...