using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Reflection;
using Aspose.Cells;
namespace Web
{
    /// <summary>
    /// excel帮助类
    /// </summary>
    public class ExcelHelp
    {
        /// <summary>
        /// Excel帮助类
        /// </summary>
        /// <param name="dataTable"></param>
        public static byte[] EntityDataToExcel(EntityDataTable dataTable, List<ExcelT> excelTs,bool isMuchHead,string title,int columnCount)
        {
          Workbook workbook = new Workbook();
           Worksheet sheet = workbook.Worksheets[];

           Cells cells = sheet.Cells;

            //插入标题
           Range rangeTitle = cells.CreateRange(, , , columnCount);

            //标题样式
            rangeTitle.RowHeight = ;
            int styleIndex = workbook.Styles.Add();
            Style rangeStyle =  workbook.Styles[styleIndex];
            rangeStyle.ForegroundColor = Color.Yellow;
            rangeStyle.Pattern = BackgroundType.Solid;
            rangeStyle.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
            rangeStyle.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
            rangeStyle.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
            rangeStyle.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
            rangeStyle.Font.IsBold=true;
            rangeStyle.Font.Size = ;
            rangeStyle.HorizontalAlignment=TextAlignmentType.Center;
            rangeStyle.VerticalAlignment = TextAlignmentType.Center;

            Cell cellTitle = rangeTitle[, ];
            cellTitle.PutValue(title);
            cellTitle.Style.Font.Size = ;
            rangeTitle.Style = rangeStyle;
            rangeTitle.Merge();

            ;
            ;
             : ;
            var count = dataTable.Count;
            var tCount = excelTs.Count;

            //头样式
            int headIndex = workbook.Styles.Add();
            Style headStyle = workbook.Styles[headIndex];
            headStyle.HorizontalAlignment=TextAlignmentType.Center;
            headStyle.VerticalAlignment = TextAlignmentType.Center;
            headStyle.Font.IsBold = true;
            //插入头
            ; h < tCount; h++)
            {
                ExcelT excelHead = excelTs[h];

                //有子表头的
                )
                {
                    var childCount = excelHead.ChildExcelT.Count;
                    Range csHeadRange = cells.CreateRange(rowIndex, colIndex, , childCount);
                    Cell rsCell = csHeadRange[, ];
                    rsCell.PutValue(excelHead.Head);
                    csHeadRange.Style = headStyle;
                    csHeadRange.Merge();
                    ;
                    foreach (var childT in excelHead.ChildExcelT)
                    {

                        cells[childRowIndex, colIndex].PutValue(childT.Head);
                        cells[childRowIndex, colIndex].Style = headStyle;
                        colIndex = colIndex + ;
                    }

                }
                else
                {
                    if (isMuchHead)
                    {
                        Range rsHeadRange = cells.CreateRange(rowIndex, colIndex,,);
                        Cell rsCell = rsHeadRange[, ];
                        rsCell.PutValue(excelHead.Head);
                        rsHeadRange.Style = headStyle;
                        rsHeadRange.Merge();
                        colIndex = colIndex + ;
                    }
                    //正常不含多表头的
                    else
                    {
                        cells[rowIndex, colIndex].PutValue(excelTs[h].Head);
                        cells[rowIndex, colIndex].Style = headStyle;
                        colIndex = colIndex + ;
                    }
                }

            }
            rowIndex = rowIndex + headRow;
            //插入数据
            ; i < count; i++)
            {
                var record = dataTable[i];
                var dt = record.DataTable;
                ;
                var rowValue = i + rowIndex;
                ; j < excelTs.Count; j++)
                {
                    var currentT = excelTs[j];
                    )
                    {
                        foreach (var childT in currentT.ChildExcelT)
                        {
                            cells[rowValue, dataI].PutValue(record[childT.DataIndex]);
                            if(childT.Width!=null)
                            {
                                cells.SetColumnWidth(dataI,Convert.ToDouble(childT.Width));
                            }
                            dataI = dataI + ;
                        }
                    }
                    else
                    {
                        cells[rowValue, dataI].PutValue(record[currentT.DataIndex]);
                        if (currentT.Width != null)
                        {
                            cells.SetColumnWidth(dataI, Convert.ToDouble(currentT.Width));
                        }
                        dataI = dataI + ;
                    }

                }
            }

           // sheet.AutoFitColumns();
            MemoryStream fileStream = new MemoryStream();
            fileStream = workbook.SaveToStream();
            return fileStream.ToArray();

        }

        /// <summary>
        /// Excel帮助类
        /// </summary>
        /// <param name="dataTable"></param>
        public static byte[] DynamicToExcel(List<dynamic> listData, List<ExcelT> excelTs, bool isMuchHead, string title, int columnCount)
        {
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[];

            Cells cells = sheet.Cells;

            //插入标题
            Range rangeTitle = cells.CreateRange(, , , columnCount);

            //标题样式
            rangeTitle.RowHeight = ;
            int styleIndex = workbook.Styles.Add();
            Style rangeStyle = workbook.Styles[styleIndex];
            rangeStyle.ForegroundColor = Color.Yellow;
            rangeStyle.Pattern = BackgroundType.Solid;
            rangeStyle.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
            rangeStyle.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
            rangeStyle.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
            rangeStyle.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
            rangeStyle.Font.IsBold = true;
            rangeStyle.Font.Size = ;
            rangeStyle.HorizontalAlignment = TextAlignmentType.Center;
            rangeStyle.VerticalAlignment = TextAlignmentType.Center;

            Cell cellTitle = rangeTitle[, ];
            cellTitle.PutValue(title);
            cellTitle.Style.Font.Size = ;
            rangeTitle.Style = rangeStyle;
            rangeTitle.Merge();

            ;
            ;
             : ;
            var count = listData.Count;
            var tCount = excelTs.Count;

            //头样式
            int headIndex = workbook.Styles.Add();
            Style headStyle = workbook.Styles[headIndex];
            headStyle.HorizontalAlignment = TextAlignmentType.Center;
            headStyle.VerticalAlignment = TextAlignmentType.Center;
            headStyle.Font.IsBold = true;
            //插入头
            ; h < tCount; h++)
            {
                ExcelT excelHead = excelTs[h];

                //有子表头的
                )
                {
                    var childCount = excelHead.ChildExcelT.Count;
                    Range csHeadRange = cells.CreateRange(rowIndex, colIndex, , childCount);
                    Cell rsCell = csHeadRange[, ];
                    rsCell.PutValue(excelHead.Head);
                    csHeadRange.Style = headStyle;
                    csHeadRange.Merge();
                    ;
                    foreach (var childT in excelHead.ChildExcelT)
                    {

                        cells[childRowIndex, colIndex].PutValue(childT.Head);
                        cells[childRowIndex, colIndex].Style = headStyle;
                        colIndex = colIndex + ;
                    }

                }
                else
                {
                    if (isMuchHead)
                    {
                        Range rsHeadRange = cells.CreateRange(rowIndex, colIndex, , );
                        Cell rsCell = rsHeadRange[, ];
                        rsCell.PutValue(excelHead.Head);
                        rsHeadRange.Style = headStyle;
                        rsHeadRange.Merge();
                        colIndex = colIndex + ;
                    }
                    //正常不含多表头的
                    else
                    {
                        cells[rowIndex, colIndex].PutValue(excelTs[h].Head);
                        cells[rowIndex, colIndex].Style = headStyle;
                        colIndex = colIndex + ;
                    }
                }

            }
            rowIndex = rowIndex + headRow;
            //插入数据
            ; i < count; i++)
            {
                var currentRow = listData[i];
                Type t = currentRow.GetType();
                ;
                var rowValue = i + rowIndex;
                ; j < excelTs.Count; j++)
                {
                    var currentT = excelTs[j];
                    )
                    {
                        foreach (var childT in currentT.ChildExcelT)
                        {
                            var filedIndex = childT.DataIndex;
                            var currentField = t.GetProperty(filedIndex);
                           var currentValue=  currentField.GetValue(currentRow,null);
                          cells[rowValue, dataI].PutValue(currentValue);
                            if (childT.Width != null)
                            {
                                cells.SetColumnWidth(dataI, Convert.ToDouble(childT.Width));
                            }
                            dataI = dataI + ;
                        }
                    }
                    else
                    {
                        var filedIndex = currentT.DataIndex;
                        var currentField = t.GetProperty(filedIndex);
                        var currentValue = currentField.GetValue(currentRow, null);
                        cells[rowValue, dataI].PutValue(currentValue);
                        if (currentT.Width != null)
                        {
                            cells.SetColumnWidth(dataI, Convert.ToDouble(currentT.Width));
                        }
                        dataI = dataI + ;
                    }

                }
            }

            // sheet.AutoFitColumns();
            MemoryStream fileStream = new MemoryStream();
            fileStream = workbook.SaveToStream();
            return fileStream.ToArray();

        }

        public static byte[] DataTableToExcel(DataTable dataTable, List<ExcelT> excelTs, bool isMuchHead, string title, int columnCount)
        {
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[];

            Cells cells = sheet.Cells;

            //插入标题
            Range rangeTitle = cells.CreateRange(, , , columnCount);

            //标题样式
            rangeTitle.RowHeight = ;
            int styleIndex = workbook.Styles.Add();
            Style rangeStyle = workbook.Styles[styleIndex];
            rangeStyle.ForegroundColor = Color.Yellow;
            rangeStyle.Pattern = BackgroundType.Solid;
            rangeStyle.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
            rangeStyle.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
            rangeStyle.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
            rangeStyle.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
            rangeStyle.Font.IsBold = true;
            rangeStyle.Font.Size = ;
            rangeStyle.HorizontalAlignment = TextAlignmentType.Center;
            rangeStyle.VerticalAlignment = TextAlignmentType.Center;

            Cell cellTitle = rangeTitle[, ];
            cellTitle.PutValue(title);
            cellTitle.Style.Font.Size = ;
            rangeTitle.Style = rangeStyle;
            rangeTitle.Merge();

            ;
            ;
             : ;
            var tCount = excelTs.Count;

            //头样式
            int headIndex = workbook.Styles.Add();
            Style headStyle = workbook.Styles[headIndex];
            headStyle.HorizontalAlignment = TextAlignmentType.Center;
            headStyle.VerticalAlignment = TextAlignmentType.Center;
            headStyle.Font.IsBold = true;
            //插入头
            ; h < tCount; h++)
            {
                ExcelT excelHead = excelTs[h];

                //有子表头的
                )
                {
                    var childCount = excelHead.ChildExcelT.Count;
                    Range csHeadRange = cells.CreateRange(rowIndex, colIndex, , childCount);
                    Cell rsCell = csHeadRange[, ];
                    rsCell.PutValue(excelHead.Head);
                    csHeadRange.Style = headStyle;
                    csHeadRange.Merge();
                    ;
                    foreach (var childT in excelHead.ChildExcelT)
                    {

                        cells[childRowIndex, colIndex].PutValue(childT.Head);
                        cells[childRowIndex, colIndex].Style = headStyle;
                        colIndex = colIndex + ;
                    }

                }
                else
                {
                    if (isMuchHead)
                    {
                        Range rsHeadRange = cells.CreateRange(rowIndex, colIndex, , );
                        Cell rsCell = rsHeadRange[, ];
                        rsCell.PutValue(excelHead.Head);
                        rsHeadRange.Style = headStyle;
                        rsHeadRange.Merge();
                        colIndex = colIndex + ;
                    }
                    //正常不含多表头的
                    else
                    {
                        cells[rowIndex, colIndex].PutValue(excelTs[h].Head);
                        cells[rowIndex, colIndex].Style = headStyle;
                        colIndex = colIndex + ;
                    }
                }

            }
            rowIndex = rowIndex + headRow;
            //插入数据
            var count = dataTable.Rows.Count;
            ; i < count; i++)
            {
                DataRow dr = dataTable.Rows[i];
                ;
                var rowValue = i + rowIndex;
                ; j < excelTs.Count; j++)
                {
                    var currentT = excelTs[j];
                    )
                    {
                        foreach (var childT in currentT.ChildExcelT)
                        {
                            cells[rowValue, dataI].PutValue(dr[childT.DataIndex]);
                            if (childT.Width != null)
                            {
                                cells.SetColumnWidth(dataI, Convert.ToDouble(childT.Width));
                            }
                            dataI = dataI + ;
                        }
                    }
                    else
                    {
                        cells[rowValue, dataI].PutValue(dr[currentT.DataIndex]);
                        if (currentT.Width != null)
                        {
                            cells.SetColumnWidth(dataI, Convert.ToDouble(currentT.Width));
                        }
                        dataI = dataI + ;
                    }

                }
            }
            sheet.AutoFitColumns();
            MemoryStream fileStream = new MemoryStream();
            fileStream = workbook.SaveToStream();
            return fileStream.ToArray();

        }

    }
}

调用实现实例:

        #region 导出费用
        public ActionResult ExcelCost(FeeMainSearch feeMainSearch)
        {
            //查看所有单位的费用
            var payMinList = MainFee.FindFeePayMainComplex(feeMainSearch);
            List<ExcelT> excelTs = new List<ExcelT>();
            //办案单位
            ExcelT unitT = new ExcelT();
            unitT.DataIndex = "UnitName";
            unitT.Head = "办案单位";
            unitT.Width = ;
            excelTs.Add(unitT);
            //案件编号
            ExcelT caseNumberT = new ExcelT();
            caseNumberT.DataIndex = "CaseNumber";
            caseNumberT.Head = "案件编号";
            caseNumberT.Width = ;
            excelTs.Add(caseNumberT);

            //应付费用
            ExcelT realRoomPayT = new ExcelT();
            realRoomPayT.DataIndex = "RealRoomPay";
            realRoomPayT.Head = "房间费用";
            realRoomPayT.Width = ;
            excelTs.Add(realRoomPayT);
            //应付餐费
            ExcelT realFoodFeeT = new ExcelT();
            realFoodFeeT.DataIndex = "RealFoodFee";
            realFoodFeeT.Head = "就餐费用";
            realFoodFeeT.Width = ;
            excelTs.Add(realFoodFeeT);

            //应付物品费用
            ExcelT realGoodFeeT = new ExcelT();
            realGoodFeeT.DataIndex = "RealGoodFee";
            realGoodFeeT.Head = "物品费用";
            realGoodFeeT.Width = ;
            excelTs.Add(realGoodFeeT);
            //应付药费
            ExcelT realMedicFeeT = new ExcelT();
            realMedicFeeT.DataIndex = "RealMedicFee";
            realMedicFeeT.Head = "医药费用";
            realMedicFeeT.Width = ;
            excelTs.Add(realMedicFeeT);

            //应付陪护费
            ExcelT realNurseFeeT = new ExcelT();
            realNurseFeeT.DataIndex = "RealNurseFee";
            realNurseFeeT.Head = "陪护费用";
            realNurseFeeT.Width = ;
            excelTs.Add(realNurseFeeT);
            //其他费用
            ExcelT otherFeeFeeT = new ExcelT();
            otherFeeFeeT.DataIndex = "OtherFee";
            otherFeeFeeT.Head = "其他费用";
            otherFeeFeeT.Width = ;
            excelTs.Add(otherFeeFeeT);
            //应付总计
            ExcelT realMainToalT = new ExcelT();
            realMainToalT.DataIndex = "RealMainToal";
            realMainToalT.Head = "费用总计";
            realMainToalT.Width = ;
            excelTs.Add(realMainToalT);
            //已支付费用
            ExcelT mainPayTotalT = new ExcelT();
            mainPayTotalT.DataIndex = "MainPayTotal";
            mainPayTotalT.Head = "已支付";
            excelTs.Add(mainPayTotalT);
            //已支付费用
            ExcelT noMainPayTotalT = new ExcelT();
            noMainPayTotalT.DataIndex = "NoMainPayTotal";
            noMainPayTotalT.Head = "未支付";
            noMainPayTotalT.Width = ;
            excelTs.Add(noMainPayTotalT);
            );
            return File(fileBytes, "application/ms-excel", Url.Encode("费用单.xls"));
        }
        #endregion

Excel 帮助类的更多相关文章

  1. excel工具类

    excel工具类 import com.iport.framework.util.ValidateUtil; import org.apache.commons.lang3.StringUtils; ...

  2. 导入导出Excel工具类ExcelUtil

    前言 前段时间做的分布式集成平台项目中,许多模块都用到了导入导出Excel的功能,于是决定封装一个ExcelUtil类,专门用来处理Excel的导入和导出 本项目的持久化层用的是JPA(底层用hibe ...

  3. MVC NPOI Linq导出Excel通用类

    之前写了一个模型导出Excel通用类,但是在实际应用中,可能不是直接导出模型,而是通过Linq查询后获取到最终结果再导出 通用类: public enum DataTypeEnum { Int = , ...

  4. asp.net(C#) Excel导出类 导出.xls文件

    ---恢复内容开始--- using Microsoft.Office.Interop.Excel; 针对office 2003需添加引用Microsoft   Excel   11.0   Obje ...

  5. NPOI导入导出EXCEL通用类,供参考,可直接使用在WinForm项目中

    以下是NPOI导入导出EXCEL通用类,是在别人的代码上进行优化的,兼容xls与xlsx文件格式,供参考,可直接使用在WinForm项目中,由于XSSFWorkbook类型的Write方法限制,Wri ...

  6. C#EXCEL 操作类--C#ExcelHelper操作类

    主要功能如下1.导出Excel文件,自动返回可下载的文件流 2.导出Excel文件,转换为可读模式3.导出Excel文件,并自定义文件名4.将数据导出至Excel文件5.将指定的集合数据导出至Exce ...

  7. Excel 操作类

    转载:http://www.cnblogs.com/fellowcheng/archive/2010/08/21/1805158.html ExcelHelper(Excel2007) Code hi ...

  8. 30 个 PHP 的 Excel 处理类

    下面的 PHP Excel 处理类中,包含 Excel 读写.导入导出等相关的类,列表如下: PHP Excel Reader classes 1. Read Excel Spreadsheets u ...

  9. 自己封装的poi操作Excel工具类

    自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...

  10. 29 个 PHP 的 Excel 处理类

    下面的 PHP Excel 处理类中,包含 Excel 读写.导入导出等相关的类,列表如下: PHP Excel Reader classes 1. Read Excel Spreadsheets u ...

随机推荐

  1. C语言的常用字符串操作函数(一)

    一直做的是单片机相关的程序设计,所以程序设计上更偏向底层,对于字符串的操作也仅限于液晶屏幕上的显示等工作,想提高下字符串操作的水平,而不是笨拙的数组替换等方式,翻看帖子发现C语言的字符串操作函数竟然这 ...

  2. sql存储过程——名称 ****不是有效的标识符

    转载自http://blog.csdn.net/xb12369/article/details/8202703 假设存储过程:proc_test create proc proc_test @Prod ...

  3. PHP验证码类

    通过PHP的GD库图像处理内容,设计一个验证码类Vcode.将该类声明在文件vcode.class.php中,并通过面向对象的特性将一些实现的细节封装在该类中.只要在创建对象时,为构造方法提供三个参数 ...

  4. Core Animation中的关键帧动画

    键帧动画就是在动画控制过程中开发者指定主要的动画状态,至于各个状态间动画如何进行则由系统自动运算补充(每两个关键帧之间系统形成的动画称为“补间动画”),这种动画的好处就是开发者不用逐个控制每个动画帧, ...

  5. tomcat各目录(文件)作用

    以tomcat7.0.50为例,主目录下有bin,conf,lib,logs,temp,webapps,work 7个文件夹 bin目录主要是用来存放tomcat的命令,主要有两大类,一类是以.sh结 ...

  6. PHP signal 信号

    最早写php时,发现在终端执行一个php文件,会一直等待程序执行完成以后,终端才能继续下面的操作,若不小心按了下Ctrl+C会导致php程序退出,闭避免这种情况发生,将会使用php的系统编程,即sig ...

  7. UVA 12083 POJ 2771 Guardian of Decency

    /* http://acm.hust.edu.cn/vjudge/contest/view.action?cid=71805#problem/C */ 性质: [1]二分图最大点独立数=顶点数-二分图 ...

  8. ios实现文字的自适应

    如果你是用xib搭的cell界面   那么cell上面的UIlabel就不能设置宽高   要选择上下左右自适应   并且label的行数设置为0  然后在tableView的代理方法 (UITable ...

  9. 缓存1 静态缓存-->读库保存成php文件 mkdir-->file_put_contents-->var_export -->include

    @mkdir()-->file_put_contents-->$data =  "<?php\nreturn ".var_export($setting, tru ...

  10. sort函数使用的基本知识

    STL中就自带了排序函数sortsort 对给定区间所有元素进行排序  要使用此函数只需用#include <algorithm> sort即可使用,语法描述为:sort(begin,en ...