导出多级表头表格到Excel
方法一:用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的更多相关文章
- js导出复杂表头(多级表头)的excel
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- js导出页面的表格到excel(NB的大神洗了好几个,挑一个记下来)
var idTmr; function getExplorer() { var explorer = window.navigator.userAgent ; //ie if (explorer.in ...
- FineUI小技巧(7)多表头表格导出
前言 之前我们曾写过一篇文章 FineUI小技巧(3)表格导出与文件下载,对于在 FineUI 中导出表格数据进行了详细描述.今天我们要更进一步,介绍下如何导出多表头表格. 多表头表格的标签定义 在 ...
- 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的格式写入 ...
- element ui表格常用功能如:导出 新增 删除 多选 跨页多选 固定表头 多级表头 合并行列 等常见需求
<template> <div class="table-cooperte"> <el-table :data="tableData&quo ...
- 使用aspose.cell动态导出多表头 EXCEL
效果图: 前台调用: using System; using System.Collections.Generic; using System.Linq; using System.Web; usin ...
- Qt实现表格树控件-支持多级表头
目录 一.概述 二.效果展示 三.实现方式 四.多级表头 1.数据源 2.表格 3.QStyledItemDelegate绘制代理 五.测试代码 六.相关文章 原文链接:Qt实现表格树控件-支持多级表 ...
- NPOI导出多表头Execl(通过html表格遍历表头)
关于NPOI的相关信息,我想博客园已经有很多了,而且NPOI导出Execl的文章和例子也很多,但导出多表头缺蛮少的:今天要讲的通过自己画html表格:通过html表格来导出自定义的多表头: 先来看要实 ...
- thinkphp导出csv文件,用表格输出excel
1.thinkphp导出csv文件 导出csv文件可能就那几行代码,今天有个问题困扰我好久,就是导出之后出现一些html代码,这个不应该,view里面是空的,controller中最后也没有$this ...
随机推荐
- 移动端 meta 标签笔记
(转自http://www.cssue.com/xhtml-css/html5-css3/mobile-meta.html,版权归原作者所有!) 移动平台对 meta 标签的定义 一.meta 标签分 ...
- js打印方法总结
前段时间做web项目用到了页面打印,在网上找了些资料,自己也试了很多方法,将这些方案列出下: 1.window.print()方法打印,所有主要浏览器都支持 print() 方法 这个方法很实用,只需 ...
- C++类的运用 和 三大函数
在<数据结构与算法分析C++描述>一书中给出了三段代码,简单描述了C++类的接口.实现.与调用: #ifndef INTCELL_H_INCLUDED #define INTCELL_H_ ...
- “PEDIY CrackMe 2007” 下载地址
工欲善其事,必先利其器.本专辑收集了看雪论坛『CrackMe & ReverseMe』版块2004年4月-2006年12月31期间所有的CrackMe和ReverseMe,共350余个. 下载 ...
- Digit (数位DP)
一个正整数的价值就是把这个数的十进制写出来之后,最长的等差子串的长度. 求出在[l,r]范围内的数字的价值总和. (l<=r<=10^12) 记f[now,ml,l,d,pre,st,li ...
- 用 CSS 实现字符串截断
[要求]:如何用css实现字符串截断,超出约定长度后用缩略符...代替? ♪ 答: <html> <head> <meta charset="UTF-8&q ...
- 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 决策树模型 ID3/C4.5/CART算法比较
决策树模型在监督学习中非常常见,可用于分类(二分类.多分类)和回归.虽然将多棵弱决策树的Bagging.Random Forest.Boosting等tree ensembel 模型更为常见,但是“完 ...
- JAVA学习之Ecplise IDE 使用技巧(2)第二章:键盘小快手,代码辅助
上一篇:JAVA学习之Ecplise IDE 使用技巧(1)第一章:我的地盘我做主,工作空间 第二章:键盘小快手,代码辅助 内容包括: 第一:显示行号 如何设置行号:Ecplice菜单Windows& ...
- 使用ASP.NET web API创建REST服务(三)
本文档来源于:http://www.cnblogs.com/madyina/p/3390773.html Creating a REST service using ASP.NET Web API A ...