方法一:用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_Const

    PHP_Const 常量 规则: 1 总是大写 2 A-Z 及 从127-255的ASCII字符 3 全局范围 4 用define函数定义 5 只能包含标量数据 如Boolean integer fl ...

  2. Redis - Keepalived + redis 主备热备切换

    1. 热备方案 硬件:server两台,分别用于master-redis及slave-redis 软件:redis.keepalived 实现目标: 由keepalived对外提供虚拟IP(VIP)进 ...

  3. My97DatePicker日期控件用法

    用法很简单,主要演示都在myDate.html  <meta http-equiv="content-type" content="text/html; chars ...

  4. POJ3974 (manacher)

    var s,t:ansistring; n,op:longint; p:..] of longint; procedure pre; var i:longint; begin s:='$*'; to ...

  5. 关于string的练习题目

    /*Are they equal*/#include<iostream>#include<string>using namespace std;int n;string dea ...

  6. 2015GitWebRTC编译实录5

    2015.07.20 libaudio_encoder_interface/libaudio_decoder_interface 编译通过将encoder,decoder两个lib合并了,后面需要看看 ...

  7. 增加mvc:resources后访问不了注解配置的controller的问题

    刚开始没有配置mvc:resourcescontroller能够正确访问,但是由于web.xml使用/拦截了所有的请求,所以静态资源访问不上增加mvc:resources之后,静态资源是能访问上了,但 ...

  8. itextsharp、Aspose.Words、Aspose.Cells联合使用

    最近做了一个系统需要把文件转换为pdf然后把转换后的pdf合成一个pdf文件,网上搜索了半天,最终决定使用itestsharp.dll配合Aspose.words和Aspose.cells来做,废话少 ...

  9. ubuntu 以root 运行程序

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  10. PCL Nodelets 和 3D 点云---36

    原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ 1.首先确保你的kinect驱动或者uvc相机驱动能正常启动,如果你没有安装kinect深度相机驱动,请 ...