WEB页面下载内容导出excel
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的更多相关文章
- PHP关于web页面交互内容
学php学了有一段时间了总结总结给大家分享一下 PHP中的引用 第一段程序: <?php $first_name="firstName"; $first=&$firs ...
- 在ASP.NET根据DataTable中的内容导出Excel
前台代码: <asp:Button ID="btnExcel" runat="server" Text="Excel导出" CssCl ...
- js灵活打印web页面区域内容的通用方法
我们做网站,经常需要打印页面指定区域的内容,而网上关于这块的说法很多,各种各样的打印控件也不少.但许多打印方案都不怎么好,至少我不喜欢,要么封装复杂,要么难以维护.正好现在的项目也需要用到 ...
- 使用web API和NPOI导出Excel
使用MVC controller输出excel的例子,自不待言,例子满天飞. 由于本项目使用的是Asp.net MVC API,因此在本项目使用API,实现了文件下载功能.代码的原理很简单,基本上是老 ...
- 将页面的内容导出使用html2canvas+jsPDF
第一首先是要引用 import jsPDF from 'jspdf' import html2canvas from 'html2canvas' import PDFJS from 'pdfjs-di ...
- web页面的数据从excel中读取
# -*- coding: utf-8 -*- import xdrlib ,sysimport xlrdimport datetimeimport jsonimport conf,reimport ...
- Microsoft.Office.Interop.Excel的用法以及利用Microsoft.Office.Interop.Excel将web页面转成PDF
1.常见用法 using Microsoft.Office.Interop.Excel; 1)新建一个Excel ApplicationClass ExcelApp = New A ...
- 利用Microsoft.Office.Interop.Excel 将web页面转成PDF
网上有很多将Web页面转成PDF的方法,还有许多收费的第三方插件.其实利用Office 自带的将EXCEL发布成PDF的功能就可以实现,如果你的需求没有多复杂,可以采用笔者的方法. 首先将web页面h ...
- jxl导出Excel文件
一.java项目实现读取Excel文件和导出Excel文件 实现读取和导出Excel文件的代码: package servlet; import java.io.FileInputStream; im ...
随机推荐
- 【笔记】《Redis设计与实现》chapter17 集群
17.1 节点 启动节点 Redis服务器启动时会根据cluster-enabled配置选项是否为yes来决定是否开启服务器的集群模式 节点会继续使用redisServer结构来保存服务器的状态,使用 ...
- Kubernetes查看可用的apiVersion版本
命令: kubectl api-versions
- 老和尚给小和尚讲故事引发了Java设计模式:组合模式
目录 示例 组合模式 定义 意图 主要解决问题 优缺点 安全式和透明式的组合模式 安全式的合成模式的结构 透明式的合成模式的结构 老和尚和小和尚的故事 示例 有一个绘图系统,可以描绘各种图形,假设现在 ...
- Periodic Strings UVA - 455
A character string is said to have period k if it can be formed by concatenating one or more repet ...
- 【pytest官方文档】解读fixtures - 11. fixture的执行顺序,3要素详解(长文预警)
当pytest要执行一个测试函数,这个测试函数还请求了fixture函数,那么这时候pytest就要先确定fixture的执行顺序了. 影响因素有三: scope,就是fixture函数的作用范围,比 ...
- 页面元素定位 - XPath
1. XPath 简介 2. 选取节点 2.1 选取节点表达式 2.2 XPath 运算符 2.3 XPath 常用函数 2.4 亲属关系匹配 2.5 *综合示例 1. XPath 简介 什么是 XP ...
- WebPack系列--开启HappyPack之后,再将项目打包速度缩短5秒
效果展示 打包时间:缩短了 26.296s-20.586s=5.71s 先看两组测试数据,第一组是没有使用DllPlugin的打包测试数据,测量三次取平均值是26.296s(25.72+25.56+2 ...
- Java整合极光推送 ( 简单 )
Java 整合极光推送官方文档:https://github.com/jpush/jpush-api-java-client 这里记录一下简单的使用步骤:创建一个普通的 Maven 工程然后添加依赖 ...
- 4- MySQL创建表以及增删改查
查看表结构 查看表的结构,使用命令:desc 表明: 创建表(命令) 格式:使用create table创建表,必须给出下列信息: 1.新表的名字. 2.表中列的名字和定义,用逗号隔开. 语法: cr ...
- POJ1201基础差分约束
题意: 有一条直线,直线上做多有50000个点,然后给你组关系 a b c表明a-b之间最少有c个点,问直线上最少多少个点. 思路: a-b最少有c个点可以想象a到b+1的距 ...