html table to excel 然后打印,Convert Html Table to Excel
问题
i want to export an HTML table to excel in MVC.
I have the following code in my controller:
public JsonResult ExportToExcel(Control ctl)
{
Response.Clear();
Response.ContentType = "application/ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=ExcelCopy.xls");
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
ctl.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();
return Json(1);
}
and the follwing function in jQuery:
function btnConvertToExcelClick() {
var inputParamtrs={ ????????? }
$.ajax({
type: "POST",
url: "/Expenses/ExportToExcel",
data: inputParamtrs,
success: function (json) {
}
});
return false;
}
I guess what im trying to ask is how to pass the whole HTML table to the JsonResult function as a Control. Help!
回答1:
Json is not a format that is supported by Excel, so you're using the wrong ActionResult.
Instead try creating your excel file as an HTML table or a CSV file. I recommend exporting a CSV file as it's probably the easiest to construct.
Further, you can use an excel library to construct an Excel file. Here are a couple.
http://e-infotainment.com/applications/csharp-excel-library/
http://code.google.com/p/excellibrary/
After you've constructed your CSV you can return the CSV as with a ContentResult (instead of json.)
public ActionResult MakeExcelCSV()
{
string excelCSV = null;
// consruct your CSV
return Content(excelCSV, "application/ms-excel");
}
Or you if you made an excel file with the library you can return it with a FileResult (instead of json)
public ActionResult MakeExcelFile()
{
Stream excelStream = null;
// consruct your excel file
return File(excelStream, "application/ms-excel", "myexcel.xlsx");
}
In either case, you won't need an ajax call to load the content, just link to it and open it in a new window.
Your Text
来源:https://stackoverflow.com/questions/11050815/convert-html-table-to-excel
html table to excel 然后打印,Convert Html Table to Excel相关推荐
- 数据到入到excel和打印功能
借花献佛,一直来在用的东西,但是不知道那里找到的了 ExportToExcel.aspx文件: 1 <%@ Page language="c#" Codebehind=&q ...
- Web页面打印及GridView导出到Excel
打印Web页面 在头部<head>加入 打印事件 <script language=javascript> function doPrint() { ...
- excel设置打印区域_仅在没有打印区域的情况下打印Excel表
excel设置打印区域 Today, we'll look at a time-saving Table trick that you might not know about. See how to ...
- WOrd ,excel实现打印功能
PrintDocument print = new PrintDocument(); string sDefault = print.PrinterSettings.Print ...
- ASP.NET中数据库数据导入Excel并打印
众所周知,WEB上的打印是比较困难的,常见的WEB上打印的方法大概有三种: 1.直接利用IE的打印功能.一般来说,这种方法可以做些扩展,而不是单单的调用javascript:print()这样简单,比 ...
- ai中如何插入签名_技巧不求人167期 Excel如何打印到1页纸的2种方法 Excel如何插入手写签名...
嗨,大家好,上期我们介绍了Excel如何制作漂亮的文件盒侧标签.今天我们继续分享,在日常excel编辑过程中,常常会遇到因为数列或者行数过多导致内容不全,根本就不能打印在一张纸上面,假如分开打印到两张 ...
- excel表头_「Excel技巧」关于Excel表格打印常见的四个问题,你会解决吗?
在日常办公中,打印是家常便饭的事.那么,就有朋友在说平时在打印Excel表格的时候,经常会碰到一些问题. 今天我专门对Excel表格打印的常见几个问题整理了一下.大家可以参考参考. 一.如何使打印出来 ...
- Microsoft Excel 教程「3」:如何在 Excel 中打印工作表?
欢迎观看 Microsoft Excel 教程,小编带大家学习 Microsoft Excel 的使用技巧,了解如何在 Excel 中打印工作表. 可打印全部或部分工作表,可一次打印一个,也可一次打印 ...
- NPOI导出excel设置打印为A4纸张
NPOI导出excel设置打印为A4纸张: Sheet sheet1 = hssfworkbook.CreateSheet("表(横版)"); sh ...
最新文章
- 比特币官网管理者因支持BCH被建议移交权限
- CentOS6.0搭建DNS
- WPF触屏Touch事件在嵌套控件中的响应问题
- Maven依赖Scope标签用法
- linux 脚本 整数 赋值,shell基础!!熟悉编程规范与变量
- Python使用tkinter编写图片浏览程序
- CCF202006-5 乔乔和牛牛逛超市【最大流】(100分题解链接)
- 2017杭电ACM集训队单人排位赛 - 1(ALL题解)
- Industrial Design System v4.5 1CD(工业产品设计软件)
- 日文翻译-在线免费日文翻译软件
- 显示硬件发展与视频开发系列(6)----显示标注与视频处理单元(4):GPGPU
- 克拉夫斯曼高端定制 刘霞---【YBC中国国际青年创业计划】
- 【国内动态】服务器列表
- 法拉科机器人接头_【发那科】图解FANUC机器人I/O信号板
- 打码,需要一个好的键盘
- 玩转SQL语句之group by 多字段分组查询与having子句,一篇解决你的疑惑!
- 物联网开发笔记(25)- 使用Micropython开发ESP32开发板之控制LCD1602显示屏
- Android禁用返回键
- Thinkphp开发视频打赏平台源码
- 爱心点击特效php代码,网页点击特效 - 鼠标点击浮现爱心特效
热门文章
- android 杀死程序收不到推送_Android APP切换到后台接收不到推送消息
- 基于SPRINGBOOT的健康饮食管理系统
- 线性代数——常用结论
- elsx文件转为csv并隔三取一
- 用pycatia给出一个半径为10mm的球体零件沿着一个半径为100mm的圆曲线作圆周运动的代码案例...
- Unity/C#、Python 参数传递
- 有序列表、无序列表、自定义列表
- 【QCC302x项目实战】两人对讲蓝牙耳机
- OpenHarmony常用调试命令
- 《深入理解Android:Wi-Fi,NFC和GPS》章节连载[节选]--第三章 Wi-Fi基础知识