方法一:用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. PHP面向对象的程序设计一些简单的概念

    一.面向对象的概述    数组和对象,都属于PHP中的复合类型(一个变量可以存储多个单元) 对象比数组更强大,不仅可以存储多个数据,还可以将函数存在对象中 对象的三大特性:封装,继承,多态 面向对象编 ...

  2. 日历控件修改的JS代码

    var bMoveable=true; var _VersionInfo=" " ; //============================================= ...

  3. The Blocks Problem

    Description Many areas of Computer Science use simple, abstract domains for both analytical and empi ...

  4. ASP.NET中的指令:

    来源:http://www.cnblogs.com/zhuisha/archive/2008/07/02/1234222.html ASP.NET中的指令: @Page指令: @Page指令只能在.a ...

  5. 七、CCScene

    CCScene一般情况是游戏里面的根节点,称之为"场景",运行游戏时需要通过CCDirector启动第一个场景.当然,游戏稍微复杂一点的话,可能会包含很多个场景,这就涉及到场景的切 ...

  6. I.MX6 32G SD卡测试

    /*********************************************************************** * I.MX6 32G SD卡测试 * 说明: * 这 ...

  7. JS的强大

    JS很强大,对于网页设计者来说,会用JS真的很重要. 学好我的linux,和数据结构.

  8. 如何将character_set_database latin1 改为 gbk(转)

    第一篇文章: 原地址: Linux服务器下文件名为 my.cnf Windows 下文件名为 my.ini 问题:通过sql语言向数据库中添加中文的数据的时候,查询是显示的是乱码. 原因:当初安装数据 ...

  9. Eclipse 插件安装方法和插件加载失败解决办法

    一:是利用Eclipse Software  Update 添加网址,让Eclipse 自动的搜索下载最新的插件. 比如安装VE这个可视化编辑UI的插件,其步骤为 Help > Software ...

  10. Objective-c——UI基础开发第九天(QQ好友列表)

    一.知识点: 1.双模型的嵌套使用 2.Button的对齐方式 3.优化UITableView的加载 4.layoutSubview的使用 5.cell的折叠代理 二.双模型的嵌套定义: 注意是将se ...