方法一:用NPOI定义多级表头导出:

引用头:

using NPOI.DDF;
using NPOI.OpenXmlFormats.Wordprocessing;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI.WebControls;

定义方法:

 public bool OutPutIMOrEXRSTExcel(DataTable dt)
{
bool Result = false;
//在内存中生成一个Excel文件:
HSSFWorkbook book = new HSSFWorkbook();
ISheet sheet = book.CreateSheet("进出口退补税款统计表"); sheet.DefaultRowHeight = * ; IRow row;
ICell cell;
int rowIndex = ;
int StartColIndex = ;
int colIndex = StartColIndex; //创建表头样式
ICellStyle style = book.CreateCellStyle();
style.Alignment = HorizontalAlignment.Center;
style.WrapText = true;
IFont font = book.CreateFont();
font.FontHeightInPoints = ;
font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
font.FontName = "简体中文";
style.SetFont(font);//HEAD 样式 #region 定义表头
//合并单元格
int InOrEx = sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex + , colIndex, colIndex));//进出口标志
var cellRangeAddress1 = sheet.GetMergedRegion(InOrEx);
for (int i = cellRangeAddress1.FirstRow; i <= cellRangeAddress1.LastRow; i++)
{
row = sheet.CreateRow(rowIndex);
//row.GetCell(0).CellStyle = style; cell = row.CreateCell(colIndex);
}
row = sheet.GetRow(rowIndex);
cell = row.GetCell(colIndex);
cell.SetCellValue("进出口标志"); colIndex++;
sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex, colIndex, colIndex + ));
cell = row.CreateCell(colIndex);
cell.SetCellValue("纠错项数"); colIndex = colIndex + ;
sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex, colIndex, colIndex + ));
cell = row.CreateCell(colIndex);
cell.SetCellValue("退补税额"); rowIndex++; colIndex = StartColIndex + ;
int involSup = sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex + , colIndex, colIndex));//涉及补税
var cellRangeAddress4 = sheet.GetMergedRegion(involSup);
for (int j = cellRangeAddress4.FirstRow; j <= cellRangeAddress4.LastRow; j++)
{
row = sheet.CreateRow(rowIndex);
//row.GetCell(0).CellStyle = style; cell = row.CreateCell(colIndex);
}
row = sheet.GetRow(rowIndex);
cell = row.GetCell(colIndex);
cell.SetCellValue("涉及补税"); colIndex++;
int involRef = sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex + , colIndex, colIndex));//涉及退税
var cellRangeAddress5 = sheet.GetMergedRegion(involRef);
for (int j = cellRangeAddress5.FirstRow; j <= cellRangeAddress5.LastRow; j++)
{
cell = row.CreateCell(colIndex);
}
row = sheet.GetRow(rowIndex);
cell = row.GetCell(colIndex);
cell.SetCellValue("涉及退税"); colIndex++;
int NoRefSup = sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex + , colIndex, colIndex));//无退补税
var cellRangeAddress6 = sheet.GetMergedRegion(NoRefSup);
for (int j = cellRangeAddress6.FirstRow; j <= cellRangeAddress6.LastRow; j++)
{
cell = row.CreateCell(colIndex);
}
row = sheet.GetRow(rowIndex);
cell = row.GetCell(colIndex);
cell.SetCellValue("无退补税"); colIndex++;
sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex, colIndex, colIndex + ));
cell = row.CreateCell(colIndex);
cell.SetCellValue("补税"); colIndex = colIndex + ;
sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex, colIndex, colIndex + ));
cell = row.CreateCell(colIndex);
cell.SetCellValue("退税"); //补税
rowIndex++;
row = sheet.CreateRow(rowIndex);
//row.GetCell(0).CellStyle = style; colIndex = StartColIndex + ;
cell = row.CreateCell(colIndex);
cell.SetCellValue("关税"); colIndex++;
cell = row.CreateCell(colIndex);
cell.SetCellValue("增值税"); colIndex++;
cell = row.CreateCell(colIndex);
cell.SetCellValue("消费税"); colIndex++;
cell = row.CreateCell(colIndex);
cell.SetCellValue("反倾销税"); colIndex++;
cell = row.CreateCell(colIndex);
cell.SetCellValue("协定关税"); //退税
colIndex++;
cell = row.CreateCell(colIndex);
cell.SetCellValue("关税"); colIndex++;
cell = row.CreateCell(colIndex);
cell.SetCellValue("增值税"); colIndex++;
cell = row.CreateCell(colIndex);
cell.SetCellValue("消费税"); colIndex++;
cell = row.CreateCell(colIndex);
cell.SetCellValue("反倾销税"); colIndex++;
cell = row.CreateCell(colIndex);
cell.SetCellValue("协定关税");
colIndex++; #endregion #region 定义表体并赋值
rowIndex++;
foreach (DataRow dr in dt.Rows)
{
colIndex = StartColIndex;
row = sheet.CreateRow(rowIndex);
foreach (DataColumn dc in dt.Columns)
{
cell = row.CreateCell(colIndex);
cell.SetCellValue(dr[colIndex].ToString()); colIndex++;
}
rowIndex++;
}
#endregion //Excel 输出
string fileName = @"ExitClassifiedCorrectionReport.xls";
try
{
HttpResponse rs = System.Web.HttpContext.Current.Response;
rs.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
rs.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
rs.ContentType = "application/ms-excel";
using (MemoryStream ms = new MemoryStream())
{
book.Write(ms);
rs.BinaryWrite(ms.ToArray());
ms.Flush();
}
}
catch (Exception ex)
{
LogHelper.Write(ex);
}
return Result;
}

方法二:直接调用html代码导出table(适用于数据量较少的table):

JS代码:

//导出table到Excel
function OutPutTab() {
var html = document.getElementById("myTable").outerHTML;
var shtml = htmlEncode(html); $("input[name='hHtml']").val(shtml);
//表单提交
document.getElementById("OutPutTab").submit();
}
//html代码编码(否则报字符错误)
function htmlEncode(value) {
return $('<div/>').text(value).html();
}

后台导出代码:



 /// <summary>
/// 下载统计表数据
/// </summary>
/// <param name="form"></param>
/// <returns></returns>
[HttpPost]
public FileResult ExportExcel(FormCollection form)
{
//第一种:使用FileContentResult
string content = Request.Form["hHtml"];
string strHtml = form["hHtml"];
strHtml = HttpUtility.HtmlDecode(strHtml);//Html解码
byte[] fileContents = Encoding.UTF8.GetBytes(strHtml);
string filename = DateTime.Now.ToString("yyyyMMddHHmmss");
return File(fileContents, "application/ms-excel", "进出口退补税额统计表" + filename + ".xls"); //第二种:使用FileStreamResult
var fileStream = new MemoryStream(fileContents);
return File(fileStream, "application/ms-excel", "fileStream.xls"); //第三种:使用FilePathResult
//服务器上首先必须要有这个Excel文件,然会通过Server.MapPath获取路径返回.
var fileName = Server.MapPath("~/uploads/选题信息导入模板.xls");
return File(fileName, "application/ms-excel", "fileName.xls"); }
 

遇到的问题及解决方案:

1、中文字符变成乱码:

导出的Excel中文字符变成乱码,网上查询到可能是编码格式的问题,通过查看网页源码发现是“UTF-8”的格式。所以我一直认为解码的默认格式就是“UTF-8”,

所以在转成byte[] 流时就一直用的默认的编码方式byte[] fileContents = Encoding.Default.GetBytes(strHtml);

转码成GB2312的byte[]方式:byte[] buffer= Encoding.GetEncoding("GB2312").GetBytes(strHtml);

或者转成字符串:string str=Encoding.GetEncoding("GB2312").GetString(buffer);

2、IE8下文件名丢失。后缀名丢失。

ie不支持中文文件名输出。将文件名变成英文就可以了。

导出多级表头表格到Excel的更多相关文章

  1. js导出复杂表头(多级表头)的excel

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. js导出页面的表格到excel(NB的大神洗了好几个,挑一个记下来)

    var idTmr; function getExplorer() { var explorer = window.navigator.userAgent ; //ie if (explorer.in ...

  3. FineUI小技巧(7)多表头表格导出

    前言 之前我们曾写过一篇文章 FineUI小技巧(3)表格导出与文件下载,对于在 FineUI 中导出表格数据进行了详细描述.今天我们要更进一步,介绍下如何导出多表头表格. 多表头表格的标签定义 在 ...

  4. Html Table用JS导出excel格式问题 导出EXCEL后单元格里的000412341234会变成412341234 7-14 会变成 2018-7-14(7月14) 自定义格式 web利用table表格生成excel格式问题 js导出excel增加表头、mso-number-format定义数据格式 数字输出格式转换 mso-number-format:"\@"

    Html Table用JS导出excel格式问题 我在网上找的JS把HTML Tabel导出成EXCEL.但是如果Table里的数字内容为0开的的导成Excel后会自动删除0,我想以text的格式写入 ...

  5. element ui表格常用功能如:导出 新增 删除 多选 跨页多选 固定表头 多级表头 合并行列 等常见需求

    <template> <div class="table-cooperte"> <el-table :data="tableData&quo ...

  6. 使用aspose.cell动态导出多表头 EXCEL

    效果图: 前台调用: using System; using System.Collections.Generic; using System.Linq; using System.Web; usin ...

  7. Qt实现表格树控件-支持多级表头

    目录 一.概述 二.效果展示 三.实现方式 四.多级表头 1.数据源 2.表格 3.QStyledItemDelegate绘制代理 五.测试代码 六.相关文章 原文链接:Qt实现表格树控件-支持多级表 ...

  8. NPOI导出多表头Execl(通过html表格遍历表头)

    关于NPOI的相关信息,我想博客园已经有很多了,而且NPOI导出Execl的文章和例子也很多,但导出多表头缺蛮少的:今天要讲的通过自己画html表格:通过html表格来导出自定义的多表头: 先来看要实 ...

  9. thinkphp导出csv文件,用表格输出excel

    1.thinkphp导出csv文件 导出csv文件可能就那几行代码,今天有个问题困扰我好久,就是导出之后出现一些html代码,这个不应该,view里面是空的,controller中最后也没有$this ...

随机推荐

  1. swift初识

    介绍: Swift是苹果2014年推出的全新编程语言,它继承了C语言.ObjC的特性,且克服了C语言的兼容性问题.Swift发展过程不仅保留了Objc很多语言特性,他也借鉴了多种现代化语言的特点,在其 ...

  2. IOS 多线程编程之Grand Central Dispatch(GCD)介绍和使用 多线程基础和练习

    介绍:前面内容源自网络 Grand Central Dispatch 简称(GCD)是苹果公司开发的技术,以优化的应用程序支持多核心处理器和其他的对称多处理系统的系统.这建立在任务并行执行的线程池模式 ...

  3. hdu 3172

    http://acm.hdu.edu.cn/showproblem.php?pid=3172 题意:输出每对朋友的关系网大小 并查集的时候维护一个数组记录根节点的大小即可,水题,这题坑在T组数据这个也 ...

  4. LeetCode Path Sum II (DFS)

    题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ...

  5. linux服务器并发与tcmalloc

    前一天使用pmap查看服务器中自己开发的游戏服务的内存使用情况,发现其中数据存储服务的内存占用率非常高,截图如下. 从截图中可以看出来,分配了大量的64MB左右的内存空间,因为对自己的服务比较了解,知 ...

  6. dedecms 列表页 list 判断flag给定指定样式 (本地测试有效)

    {dede:list pagesize='10'} [field:array runphp='yes'] if (@me['flag']=='a') @me=' <a class="n ...

  7. css样式兼容不同浏览器问题解决办法

    在网站设计的时候,应该注意css样式兼容不同浏览器问题,特别是对完全使用DIV CSS设计的网,就应该更注意IE6 IE7 FF对CSS样式的兼容,不然,你的网乱可能出去不想出现的效果! 所 有浏览器 ...

  8. SPAdes

    用后感: 拼个小基因组还好,对于很大的基因组,文库很多的,还是不要用了.服务器768G内存,都不够用.... 主页: http://bioinf.spbau.ru/spades 说明书: http:/ ...

  9. Date 对象总结

    Date对象,是操作日期和时间的对象.Date对象对日期和时间的操作只能通过方法. 无参数: var date=new Date(); console.log(date)     // Mon Jun ...

  10. java List 排序 Collections.sort()

    用Collections.sort方法对list排序有两种方法  第一种是list中的对象实现Comparable接口,如下: /** * 根据order对User排序 */ public class ...