导出多级表头表格到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 ...
随机推荐
- D - Mysterious Present
这个题和求最长递增序列的题类似,为了能输出一组可行的数据,我还用了一点儿链表的知识. Description Peter decided to wish happy birthday to his f ...
- linux命令:rm
1.介绍: rm用来删除文件或者目录,对于链接文件,只删除了链接,不删除源文件.rm是一个非常危险的命令,像rm -rf /这个命令运行后,后果不堪设想. 2.命令格式: rm [选项] 文件/目录 ...
- BUY LOW, BUY LOWER_最长下降子序列
Description The advice to "buy low" is half the formula to success in the bovine stock mar ...
- 暴力枚举——Help Me with the Game
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3394 Accepted: 2172 Description You ...
- Qt5 添加右键菜单简单测试
1.在.h文件中包含相关头文件 #include <QMenu> #include <QContextMenuEvent> 2.在.h文件中定义动作对象 QAction *ed ...
- coreseek(sphinx)安装1(xml数据源配置和测试)
1.下载coreseek-3.2.14-32版本.网址:http://www.coreseek.cn/products-install/install_on_windows/ (有详细的安装说明) ...
- 用python做爬虫的例子
主要就是用了两个库,urllib和BeautifulSoup. 作用是从HTML中解析出解梦的查询词和具体的解释. # -*- coding: utf-8 -*- import urllib, url ...
- 原生js获取鼠标坐标方法全面讲解:clientX/Y,pageX/Y,offsetX/Y,layerX/Y,screenX/Y【转】
关于js鼠标事件综合各大浏览器能获取到坐标的属性总共以下五种 event.clientX/Y event.pageX/Y event.offsetX/Y event.layerX/Y event.sc ...
- javascript判断浏览器的版本
在javascript中直接的使用navigator.userAgent就可以获取当前浏览器的版本等信息,以下是列出来的关于不同浏览器显示的值(Windows.Android.iPhone): IE6 ...
- smartUpload组件文件上传
public class SmartUploadServlet extends HttpServlet { public void doGet(HttpServletRequest request, ...