用ASPOSE.Cells将HTML表格存为Excel
前端生成的html表格经常需要导出到excel中,利用JS和Office控件可以做到,但仅限于IE,还要启用安全设置。
想找一个简单的办法将HTML内容直接转换成Excel文件,如果直接修改网页头信息输出,虽然可以导出,但打开时会提示格式不是Excel的,怎样才能导出真正的Excel文件?
aspose.cells是个功能强大的控件,可以方便的生成excel文件。经考虑,将html发送到后台,保存为xls文件(其实是html内容),再用aspose.cells打开,输出到客户端,这样就变成了真正的excel文件了。
基本代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="htmlTableToExcel.aspx.cs" Inherits="ASPOSE_htmlTableToExcel" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<input id="Button1" type="button" value="button" onclick="toExcel()" />
<div>
<table style="width: 200px; border:1px red solid;" id="table1">
<tr>
<td style="width:30px;">2</td>
<td style="width:80px;">ddsf</td>
<td style="width:90px;">s</td>
</tr>
<tr>
<td>3</td>
<td style="color:red; border:2px green solid;">sfas</td>
<td>f</td>
</tr>
<tr>
<td>5</td>
<td>ggg</td>
<td>g</td>
</tr>
<tr>
<td> </td>
<td rowspan="2">fffg</td>
<td> </td>
</tr>
<tr>
<td>z</td>
<td>f</td>
</tr>
</table>
</div>
</form>
</body>
</html>
<script src="../JS/Jquery-1.8.2.min.js"></script>
<script type="text/javascript">
function toExcel() {
if (!document.getElementById('div_div_div_1234567890')) {
$('body').append("<div id='div_div_div_1234567890' style='display:none;'>"
+ "<iframe id='submitFrame' name='submitFrame'></iframe>"
+ "<form id='form_form_form_1234567890' method='post' target='submitFrame' onsubmit='return false;'>"
+ "<input type='hidden' id='input_input_input_1234567890' name='table' />"
+ "</form>"
+ "</div>");
}
document.getElementById('input_input_input_1234567890').value = document.getElementById('table1').outerHTML;
document.getElementById('form_form_form_1234567890').action = "htmlTableToExcel.ashx"
document.getElementById('form_form_form_1234567890').submit();
}
</script>
htmlTableToExcel.ashx:
<%@ WebHandler Language="C#" Class="htmlTableToExcel" %> using System;
using System.Web;
using Aspose.Cells;
using System.IO;
using System.Text; public class htmlTableToExcel : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
string table = context.Request["table"]; //test info
if (table == null || table == "")
{
table = @"<table style='width: 200px' id='table1'>
<tr>
<td style='width:30px;'>abc</td>
<td style='width:80px;'>123</td>
<td style='width:90px;'>这是测试信息</td>
</tr>
</table>";
} MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter( stream );
writer.Write( table );
writer.Flush(); //加载选项,html,否则会出错
LoadOptions lo = new LoadOptions(LoadFormat.Html);
Workbook wb = new Workbook(stream, lo);
//输出到浏览器
wb.Save(context.Response, "output.xls", ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Excel97To2003));
} public bool IsReusable
{
get
{
return false;
}
} }以上为简单的测试代码,经检验可以生成excel文件,只是格式方面像边框、宽度什么的就没有了,合并单元格什么的能保留;要是要求不高就将就用着吧。
用ASPOSE.Cells将HTML表格存为Excel的更多相关文章
- Aspose.Cells Smart markers 基于模板导出Excel
Aspose.Cells可以预先定义Excel模板,然后填充数据(官方文档:http://www.aspose.com/docs/display/cellsjava/Smart+Markers). 设 ...
- c#使用aspose.cells 从datatable导出数据到excel
string json=value.Value; DataTable dt=Utils.JsonDataTableConvert.ToDataTable(json); string fileName ...
- C# 读写Excel的一些方法,Aspose.Cells.dll
需求:现有2个Excel,一个7000,一个20W,7000在20W是完全存在的.现要分离20W的,拆分成19W3和7000. 条件:两个Excel都有“登录名”,然后用“登录名”去关联2个Excel ...
- Aspose Cells 添加数据验证(动态下拉列表验证)
参考 :http://www.componentcn.com/kongjianjishu/kongjianjishu/2015-06-04/2781.html Aspose Cells是一款操作和处理 ...
- C# Aspose.Cells方式导入Excel文件
读取Excel 类 我返回的是DataTable 类型 也可以返回DataSet类型 public class XlsFileHelper { public DataTable ImportExcel ...
- 常用类-Excel-使用Aspose.Cells插件
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Xm ...
- C#使用Aspose.Cells导出Excel简单实现
首先,需要添加引用Aspose.Cells.dll,官网下载地址:http://downloads.aspose.com/cells/net 将DataTable导出Xlsx格式的文件下载(网页输出) ...
- 使用Aspose.Cells读取Excel
最新更新请访问: http://denghejun.github.io Aspose.Cells读取Excel非常方便,以下是一个简单的实现读取和导出Excel的操作类: 以下是Aspose.Ce ...
- Aspose.Cells 首次使用,用到模版填充数据,合并单元格,换行
Aspose.Cells 首次使用,用到模版填充数据,合并单元格,换行 模版格式,图格式是最简单的格式,但实际效果不是这种,实际效果图如图2 图2 ,注意看红色部分,一对一是正常的,但是有一对多的订单 ...
随机推荐
- SpringBoot2.x使用EasyPOI导入Excel浅谈
SpringBoot2.x使用EasyPOI导入Excel浅谈 平时经常遇到客户要帮忙导入一些数据到数据库中,有些数据比较多有时候手动录入就会很耗时间,所以就自己写一个Excel导入的demo记录一下 ...
- bzoj1030【JSOI2007】文本生成器
1030: [JSOI2007]文本生成器 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 2891 Solved: 1193 [Submit][St ...
- 14.翻译系列:从已经存在的数据库中生成上下文类和实体类【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/code-first-from-existing-database.aspx EF 6 ...
- Python时间,日期,时间戳之间转换,时间转换时间戳,Python时间戳转换时间,Python时间转换时间戳
#1.将字符串的时间转换为时间戳方法: a = "2013-10-10 23:40:00" #将其转换为时间数组 import time timeArray = time.strp ...
- Spring Boot系列——7步集成RabbitMQ
RabbitMQ是一种我们经常使用的消息中间件,通过RabbitMQ可以帮助我们实现异步.削峰的目的. 今天这篇,我们来看看Spring Boot是如何集成RabbitMQ,发送消息和消费消息的.同时 ...
- 戳破ZigBee技术智能家居的谎言!
戳破ZigBee技术智能家居的谎言 一.ZigBee介绍 简介 在蓝牙技术的使用过程中,人们发现蓝牙技术尽管有许多优点,但仍存在许多缺陷.对工业,家庭自动化控制和遥测遥控领域而言,蓝牙技术显得太复杂, ...
- why-the-default-authentication-hadoop-is-unsecured ?
https://www.learningjournal.guru/article/hadoop/hadoop-security-using-kerberos/ https://stackoverflo ...
- return返回两个三元表达式的和,返回不正确,同样要注意在JavaScript中,也是如此
public string b() { string b = ""; "; } public int c() { public string b() { string b ...
- com.android.jack.CommandLine: Internal compiler error
Android studio编译的时候出现错误: SEVERE: com.android.jack.CommandLine: Internal compiler error Error:Executi ...
- myeclipse中的classpath .
博客分类: java基础 myeclipse中的classpath是一个很重要的问题 myeclipse的在查找的时候都是按照其查找,而且myeclipse有一个专门的文件来保存classpath ...