导出多级表头表格到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;

定义方法:

  1 public bool OutPutIMOrEXRSTExcel(DataTable dt)
  2         {
  3             bool Result = false;
  4             //在内存中生成一个Excel文件:
  5             HSSFWorkbook book = new HSSFWorkbook();
  6             ISheet sheet = book.CreateSheet("进出口退补税款统计表");
  7
  8             sheet.DefaultRowHeight = 20 * 10;
  9
 10             IRow row;
 11             ICell cell;
 12             int rowIndex = 0;
 13             int StartColIndex = 0;
 14             int colIndex = StartColIndex;
 15
 16             //创建表头样式
 17             ICellStyle style = book.CreateCellStyle();
 18             style.Alignment = HorizontalAlignment.Center;
 19             style.WrapText = true;
 20             IFont font = book.CreateFont();
 21             font.FontHeightInPoints = 16;
 22             font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
 23             font.FontName = "简体中文";
 24             style.SetFont(font);//HEAD 样式
 25
 26
 27             #region 定义表头
 28             //合并单元格
 29             int InOrEx = sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex + 2, colIndex, colIndex));//进出口标志
 30             var cellRangeAddress1 = sheet.GetMergedRegion(InOrEx);
 31             for (int i = cellRangeAddress1.FirstRow; i <= cellRangeAddress1.LastRow; i++)
 32             {
 33                 row = sheet.CreateRow(rowIndex);
 34                 //row.GetCell(0).CellStyle = style;
 35
 36                 cell = row.CreateCell(colIndex);
 37             }
 38             row = sheet.GetRow(rowIndex);
 39             cell = row.GetCell(colIndex);
 40             cell.SetCellValue("进出口标志");
 41
 42             colIndex++;
 43             sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex, colIndex, colIndex + 2));
 44             cell = row.CreateCell(colIndex);
 45             cell.SetCellValue("纠错项数");
 46
 47             colIndex = colIndex + 3;
 48             sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex, colIndex, colIndex + 9));
 49             cell = row.CreateCell(colIndex);
 50             cell.SetCellValue("退补税额");
 51
 52             rowIndex++;
 53
 54             colIndex = StartColIndex + 1;
 55             int involSup = sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex + 1, colIndex, colIndex));//涉及补税
 56             var cellRangeAddress4 = sheet.GetMergedRegion(involSup);
 57             for (int j = cellRangeAddress4.FirstRow; j <= cellRangeAddress4.LastRow; j++)
 58             {
 59                 row = sheet.CreateRow(rowIndex);
 60                 //row.GetCell(0).CellStyle = style;
 61
 62                 cell = row.CreateCell(colIndex);
 63             }
 64             row = sheet.GetRow(rowIndex);
 65             cell = row.GetCell(colIndex);
 66             cell.SetCellValue("涉及补税");
 67
 68             colIndex++;
 69             int involRef = sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex + 1, colIndex, colIndex));//涉及退税
 70             var cellRangeAddress5 = sheet.GetMergedRegion(involRef);
 71             for (int j = cellRangeAddress5.FirstRow; j <= cellRangeAddress5.LastRow; j++)
 72             {
 73                 cell = row.CreateCell(colIndex);
 74             }
 75             row = sheet.GetRow(rowIndex);
 76             cell = row.GetCell(colIndex);
 77             cell.SetCellValue("涉及退税");
 78
 79             colIndex++;
 80             int NoRefSup = sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex + 1, colIndex, colIndex));//无退补税
 81             var cellRangeAddress6 = sheet.GetMergedRegion(NoRefSup);
 82             for (int j = cellRangeAddress6.FirstRow; j <= cellRangeAddress6.LastRow; j++)
 83             {
 84                 cell = row.CreateCell(colIndex);
 85             }
 86             row = sheet.GetRow(rowIndex);
 87             cell = row.GetCell(colIndex);
 88             cell.SetCellValue("无退补税");
 89
 90
 91             colIndex++;
 92             sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex, colIndex, colIndex + 4));
 93             cell = row.CreateCell(colIndex);
 94             cell.SetCellValue("补税");
 95
 96             colIndex = colIndex + 5;
 97             sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex, colIndex, colIndex + 4));
 98             cell = row.CreateCell(colIndex);
 99             cell.SetCellValue("退税");
100
101             //补税
102             rowIndex++;
103             row = sheet.CreateRow(rowIndex);
104             //row.GetCell(0).CellStyle = style;
105
106             colIndex = StartColIndex + 4;
107             cell = row.CreateCell(colIndex);
108             cell.SetCellValue("关税");
109
110             colIndex++;
111             cell = row.CreateCell(colIndex);
112             cell.SetCellValue("增值税");
113
114             colIndex++;
115             cell = row.CreateCell(colIndex);
116             cell.SetCellValue("消费税");
117
118             colIndex++;
119             cell = row.CreateCell(colIndex);
120             cell.SetCellValue("反倾销税");
121
122             colIndex++;
123             cell = row.CreateCell(colIndex);
124             cell.SetCellValue("协定关税");
125
126             //退税
127             colIndex++;
128             cell = row.CreateCell(colIndex);
129             cell.SetCellValue("关税");
130
131             colIndex++;
132             cell = row.CreateCell(colIndex);
133             cell.SetCellValue("增值税");
134
135             colIndex++;
136             cell = row.CreateCell(colIndex);
137             cell.SetCellValue("消费税");
138
139             colIndex++;
140             cell = row.CreateCell(colIndex);
141             cell.SetCellValue("反倾销税");
142
143             colIndex++;
144             cell = row.CreateCell(colIndex);
145             cell.SetCellValue("协定关税");
146             colIndex++;
147
148             #endregion
149
150             #region 定义表体并赋值
151             rowIndex++;
152             foreach (DataRow dr in dt.Rows)
153             {
154                 colIndex = StartColIndex;
155                 row = sheet.CreateRow(rowIndex);
156                 foreach (DataColumn dc in dt.Columns)
157                 {
158                     cell = row.CreateCell(colIndex);
159                     cell.SetCellValue(dr[colIndex].ToString());
160
161                     colIndex++;
162                 }
163                 rowIndex++;
164             }
165             #endregion
166
167             //Excel 输出
168             string fileName = @"ExitClassifiedCorrectionReport.xls";
169             try
170             {
171                 HttpResponse rs = System.Web.HttpContext.Current.Response;
172                 rs.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
173                 rs.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
174                 rs.ContentType = "application/ms-excel";
175                 using (MemoryStream ms = new MemoryStream())
176                 {
177                     book.Write(ms);
178                     rs.BinaryWrite(ms.ToArray());
179                     ms.Flush();
180                 }
181             }
182             catch (Exception ex)
183             {
184                 LogHelper.Write(ex);
185             }
186             return Result;
187         }

方法二:直接调用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)
        {
            string strHtml = form["hHtml"];
            strHtml = HttpUtility.HtmlDecode(strHtml);//Html解码
            byte[] fileContents = Encoding.UTF8.GetBytes(strHtml);
            return File(fileContents, "application/ms-excel", "CustomDistrictStructTAB.xls");

        }
时间: 2024-10-29 16:52:55

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

html的table多级表头表格的代码

1,两级表头的代码 <html> <head> <title>多层表头</title> <link rel="stylesheet" href="./css/reset.css"> <style type="text/css"> td, th { font-style: normal; font-weight: normal; text-align: center; } tr

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

前言 之前我们曾写过一篇文章 FineUI小技巧(3)表格导出与文件下载,对于在 FineUI 中导出表格数据进行了详细描述.今天我们要更进一步,介绍下如何导出多表头表格. 多表头表格的标签定义 在 ASPX 中,我们通过 GroupField 列来定义多表头,如下所示: <f:Grid ID="Grid1" Title="表格" EnableCollapse="true" ShowBorder="true" ShowH

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

效果图: 前台调用: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using ExportCells; namespace WebApplication1 { public partial class _Default : Sy

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

关于NPOI的相关信息,我想博客园已经有很多了,而且NPOI导出Execl的文章和例子也很多,但导出多表头缺蛮少的:今天要讲的通过自己画html表格:通过html表格来导出自定义的多表头: 先来看要实现的多表头格式: 第一步:画html表格(备注有一定的格式要求) //td需要4个属性,rowspan(跨行数).colspan(跨列数).row(所在行).col(所在列):备注:其实除了跨行和跨列数外,后面只需要所在列都可以了: <tr> <td rowspan="2"

Java导出数据行写入到Excel表格:基于Apache POI

Java导出数据行写入到Excel表格:基于Apache POI import java.io.File; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class Excel

适用于iview的表格转Excel插件

在网上找的一个表格转excel插件,经过修改后使其适用于iview中的table组件 let idTmr; const getExplorer = () => { let explorer = window.navigator.userAgent; //ie if (explorer.indexOf("MSIE") >= 0) { return 'ie'; } //firefox else if (explorer.indexOf("Firefox")

element UI实现动态生成多级表头

一.效果图 二.封装两个组件,分别为DynamicTable.vue和TableColumn.vue,TableColumn.vue主要是使用递归来对表头进行循环生成 DynamicTable.vue 1 <template> 2 <el-table :data="tableData" border :height="height"> 3 <template v-for="item in tableHeader"&g

导出数据到表格PHP

导出数据到表格 public function excel(){ $filename = '导出表格'; $header = ['编号','名称']; $index = ['id','name']; $list = array( 0 => array('id' => 1, 'name' => 'fdfs'), 1 => array('id' => 2, 'name' => 'ffdfdsafs'), 2 => array('id' => 3, 'name'

freemarker导出word——让表格数据行数 列数自动变化

行数.列数变化只需定义一个List<List<T>> freemarker遍历的话,只需要使用freemarker的标记性语言<#list report.qc_third_agentTable as  table2_tr>遍历即可,如图 实现的效果 freemarker导出word--让表格数据行数 列数自动变化,布布扣,bubuko.com