using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
namespace EasyFrame.Common
{
    /// <summary>
    ///title调用信息
    /// </summary>
    public class ChartTitle
    {
        /// <summary>
        /// 调用名
        /// </summary>
        public string CallName { get; set; }
        /// <summary>
        /// 标头信息
        /// </summary>
        public List<ChartName> SetNameList { get; set; }
    }
    public class ChartName
    {
        /// <summary>
        /// 一级标头
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 二级标头
        /// </summary>
        public List<string> ChartChildNameList { get; set; }

    }

    public class ExcelHelper
    {

        //
        /// <summary>
        /// Datatable导出Excel
        /// </summary>
        /// <param name="dt">数据</param>
        /// <param name="list">表头</param>
        /// <param name="counts">总条数</param>
        /// <param name="filePath">保存地址</param>
        /// <returns></returns>
        public static MemoryStream WriteExcel(DataTable dt, List<ChartTitle> objlist, string filePath)
        {
            var list = objlist.FirstOrDefault(a => a.CallName == "ccc").SetNameList;
            )
            {
                XSSFWorkbook book = new XSSFWorkbook();
                NPOI.SS.UserModel.ISheet sheet = book.CreateSheet(dt.TableName);
                ICell cell = null;
                ICellStyle style = book.CreateCellStyle();
                IFont font = book.CreateFont();//创建字体样式
                IFont fonts = book.CreateFont();//创建字体样式
                IFont fontss = book.CreateFont();//创建字体样式
                font.Color = NPOI.HSSF.Util.HSSFColor.Red.Index;//设置字体颜色
                ICellStyle styles = book.CreateCellStyle(); //红色
                fonts.Color = NPOI.HSSF.Util.HSSFColor.Green.Index;
                ICellStyle styless = book.CreateCellStyle(); //绿色
                fontss.Color = NPOI.HSSF.Util.HSSFColor.Blue.Index;
                ICellStyle stylesss = book.CreateCellStyle(); //蓝色
                styless.SetFont(fonts);
                styles.SetFont(font);
                stylesss.SetFont(fontss);
                style.Alignment = HorizontalAlignment.Center;
                NPOI.SS.UserModel.IRow row1 = sheet.CreateRow();
                NPOI.SS.UserModel.IRow row2 = sheet.CreateRow();
                var row = row1;
                ;//记录列
                var d = true;//是否第一次加载到二级表单
                #region  创建表头
                //两行单列
                ; i < list.Count; i++)
                {
                    if (!d)
                    {
                        row = row1;
                        d = true;
                    }
                    if (list[i].ChartChildNameList == null)
                    {
                        cell = row.CreateCell(num);
                        sheet.SetColumnWidth(num,  * );
                        cell.SetCellValue(list[i].Name);
                        cell.CellStyle = style;
                        sheet.AddMergedRegion(, , num, num));
                        num++;
                        continue;
                    }
                    //两行多列
                    var count = list[i].ChartChildNameList.Count; //列数
                    cell = row.CreateCell(num);
                    cell.SetCellValue(list[i].Name);
                    cell.CellStyle = style;
                    sheet.AddMergedRegion(, , num, count + num - ));
                    if (d)
                    {
                        d = false;
                        row = row2;
                    }
                    ;
                    );
                    ; j < count; j++)
                    {
                        sheet.SetColumnWidth(num + t,  * );
                        cell = row.CreateCell(num + t);
                        cell.SetCellValue(list[i].ChartChildNameList[j]);
                        cell.CellStyle = style;
                        t++;
                    }
                    num = num + count;
                }
                #endregion 创建表头
                #region 写入数据
                ;//记录列数
                ;//记录行数

                filePath = string.Format(filePath, "XXX");
                #region 导出操作
                ][].ToString());
                foreach (var item in data1)
                {
                    NPOI.SS.UserModel.IRow rows = sheet.CreateRow(clouns + );
                    customs = ;
                    rows.CreateCell(customs).SetCellValue(Convert.ToString(item.No)); customs++;
                    rows.CreateCell(customs).SetCellValue(Convert.ToString(item.LotteryOpenNo)); customs++;
                    ; i < item.Wan.Length; i++)
                    {
                        ] == item.Wan[i].ToString())
                        {
                            cell = rows.CreateCell(customs);
                            cell.SetCellValue(item.Wan[i]);
                            cell.CellStyle = styles;
                            customs++;
                        }
                        else
                        {
                            rows.CreateCell(customs).SetCellValue(item.Wan[i]); customs++;
                        }
                    }
                    ; j < item.Qian.Length; j++)
                    {
                        ] == item.Qian[j].ToString())
                        {
                            cell = rows.CreateCell(customs);
                            cell.SetCellValue(item.Qian[j]);
                            cell.CellStyle = styles;
                            customs++;
                        }
                        else
                        {
                            rows.CreateCell(customs).SetCellValue(item.Qian[j]); customs++;
                        }
                    }
                    ; k < item.Bai.Length; k++)
                    {
                        ] == item.Bai[k].ToString())
                        {
                            cell = rows.CreateCell(customs);
                            cell.SetCellValue(item.Bai[k]);
                            cell.CellStyle = styles;
                            customs++;
                        }
                        else
                        {
                            rows.CreateCell(customs).SetCellValue(item.Bai[k]); customs++;
                        }
                    }
                    ; l < item.Shi.Length; l++)
                    {
                        ] == (item.Shi[l].ToString()))
                        {
                            cell = rows.CreateCell(customs);
                            cell.SetCellValue(item.Shi[l]);
                            cell.CellStyle = styles;
                            customs++;
                        }
                        else
                        {
                            rows.CreateCell(customs).SetCellValue(item.Shi[l]); customs++;
                        }
                    }
                    ; m < item.Ge.Length; m++)
                    {
                        ] == (item.Ge[m].ToString()))
                        {
                            cell = rows.CreateCell(customs);
                            cell.SetCellValue(item.Ge[m]);
                            cell.CellStyle = styles;
                            customs++;
                        }
                        else
                        {
                            rows.CreateCell(customs).SetCellValue(item.Ge[m]); customs++;
                        }
                    }
                    ; n < item.Fen.Length; n++)
                    {
                        if (item.LotteryOpenNo.Contains(item.Fen[n].ToString()))
                        {
                            cell = rows.CreateCell(customs);
                            cell.SetCellValue(item.Fen[n]);
                            cell.CellStyle = styles;
                            customs++;
                        }
                        else
                        {
                            rows.CreateCell(customs).SetCellValue(item.Fen[n]); customs++;
                        }

                    }
                    clouns++;
                }

                #endregion

                #endregion 写入数据
                // 写入到客户端
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    book.Write(ms);
                    using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                    {
                        byte[] data = ms.ToArray();
                        fs.Write(data, , data.Length);
                        fs.Flush();
                    }
                    book = null;
                    return ms;
                }
            }
            return null;
        }

    }
}

c# Datatable导出Excel的更多相关文章

  1. asp.net DataTable导出Excel 自定义列名

    1.添加引用NPOI.dll 2.cs文件头部添加 using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System.IO; 3.代码如 ...

  2. Datatable导出Excel

    ; IRow headerRow = sheet.CreateRow(); ; ; ; iRowIndex++; } ; i < icolIndex; i++) { sheet.AutoSize ...

  3. C# DataTable导出EXCEL后身份证、银行卡号等长数字信息显示乱码解决

    在DataTable导出EXCEL后发现有些格式显示有问题,比如身份证.银行卡号等大于11位的数字显示为科学计数法.13681-1等 带中划线的两段数字显示为日期格式等. 处理方法如下: public ...

  4. 【转】C# DataTable 导出 Excel 进阶 多行表头、合并单元格、中文文件名乱码

    本文原创地址:http://blog.csdn.net/ranbolwb/article/details/8083983 ,转载请保留本行. 本例子是上一篇 DataTable 导出 Excel 的进 ...

  5. DataTable 更改在有数据列的类型方法+DataTable 导出excel功能

    /// <summary> /// 导出功能 /// </summary> /// <param name="sender"></para ...

  6. C# Datatable导出Excel方法

    C# 导出Excel方法  先引用下System.IO;System.data; 具体函数如下: public static bool ExportCSV(DataTable dt, string f ...

  7. NPOI DataTable导出excel

    /// <summary> /// DataTable导出到Excel文件 /// </summary> /// <param name="dtSource&q ...

  8. WinForm 使用 NPOI 2.2.1从datatable导出Excel

    最新的NOPI应该是2.3了,但在官网上还是2.2.1. 也是第一次使用NPOI来导出Excel文件. 在写的时候搜不到2.2.1的教程,搜了一个2.2.0的教程. 不过也没什么问题,NPOI是真的方 ...

  9. 【ASP.NET】DataTable导出EXCEL,弹窗提示下载保存(完整代码)

    //新建ASPX protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); Data ...

随机推荐

  1. Latex命令

    .tex代码中 |   在pdf文档中 空一行  代表回车,下一行空两格 // 代表回车,下一行顶格

  2. 【repost】H5的新特性及部分API详解

    h5新特性总览 移除的元素 纯表现的元素: basefont.big.center.font等 对可用性产生负面影响的元素: frame.frameset.noframes 新增的API 语义: 能够 ...

  3. Spring AOP中pointcut expression表达式

    Pointcut 是指那些方法需要被执行"AOP",是由"Pointcut Expression"来描述的. Pointcut可以有下列方式来定义或者通过&am ...

  4. java web 测试

    1.功能测试 bugfree mantis 2.性能测试:loadrunner 3.安全测试: burpsuite

  5. 2018.09.22 上海大学技术分享 - An Introduction To Go Programming Language

    老实说笔者学习 Go 的时间并不长,积淀也不深厚,这次因缘巧合,同组的同事以前是上海大学的开源社区推动者之一,同时我们也抱着部分宣传公司和技术分享的意图,更进一步的,也是对所学做一个总结,所以拟定了这 ...

  6. [Linux/Ubuntu] vi/vim 使用方法讲解

    vi/vim 基本使用方法 vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对Unix及Linux系统的 ...

  7. MyBatis 源码分析 - 配置文件解析过程

    * 本文速览 由于本篇文章篇幅比较大,所以这里拿出一节对本文进行快速概括.本篇文章对 MyBatis 配置文件中常用配置的解析过程进行了较为详细的介绍和分析,包括但不限于settings,typeAl ...

  8. LCA(最近公共祖先)——Tarjan

    什么是最近公共祖先? 在一棵没有环的树上,每个节点肯定有其父亲节点和祖先节点,而最近公共祖先,就是两个节点在这棵树上深度最大的公共的祖先节点. 换句话说,就是两个点在这棵树上距离最近的公共祖先节点. ...

  9. Eclipse 中打包插件 Fat Jar 的安装与使用

    Eclipse可以安装一个叫Fat Jar的插件,用这个插件打包非常方便,Fat Jar的功能非常强大. 首先要下载Fat Jar,下载地址:https://sourceforge.net/proje ...

  10. location匹配

    =/ 表示精确匹配  www.sensetime.com/ ~ :表示做正则表达式匹配,区分字符大小写 ~* : 表示做正则表达式匹配,不区分大小写 ^~: URI的左半部分匹配,不区分大小写 匹配优 ...