using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using System.IO;
using NPOI.SS.Util;

namespace ExcelPlay
{
    public class Excel
    {
        public HSSFWorkbook hssfwork; //工作簿

/// <summary>
        /// 初始化Excel
        /// </summary>
        public Excel()
        {
            hssfwork = new HSSFWorkbook();  //创建工作簿
        }

/// <summary>
        /// 初始化打开Excel
        /// </summary>
        /// <param name="path"></param>
        public Excel(string path)
        {
            FileStream file = new FileStream(path, FileMode.Open);
            hssfwork = new HSSFWorkbook(file);  //创建工作簿
        }

/// <summary>
        /// 创建工作表
        /// </summary>
        /// <param name="ShellName"></param>
        public NPOI.SS.UserModel.ISheet CreateShell(string ShellName)
        {
            NPOI.SS.UserModel.ISheet sheet; //工作表
            sheet = hssfwork.CreateSheet(ShellName);
           
            return sheet;
        }

/// <summary>
        /// 写主题公司信息
        /// </summary>
        /// <param name="Document"></param>
        /// <param name="Summary"></param>
        public void WriteInfomation(string Document, string Summary)
        {
            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();  //创建summaryinformation文件  公司名
            dsi.Company = "mo";

SummaryInformation si = PropertySetFactory.CreateSummaryInformation();  //创建summaryinformation  主题
            si.Subject = "Example";

hssfwork.DocumentSummaryInformation = dsi;  //创建好的传递给工作簿
            hssfwork.SummaryInformation = si;
        }

/// <summary>
        /// 获取工作表
        /// </summary>
        /// <param name="LstName"></param>
        /// <returns></returns>
        public List<string> GetSheet(List<string> LstName)
        {
            for (int i = 0; i < hssfwork.NumberOfSheets; i++)
            {
                LstName.Add(hssfwork.GetSheetName(i));
            }
            return LstName;
        }

/// <summary>
        /// 储存工作簿
        /// </summary>
        /// <param name ="path"></param>
        public void SaveWorkbook(string path)
        {
            FileStream file = new FileStream(path, FileMode.Create);
            hssfwork.Write(file);
            file.Close();    // 写入输入流文件
        }

/// <summary>
        /// 写入数据
        /// </summary>
        /// <param name ="Row"></param>
        /// <param name ="Column"></param>
        /// <param name ="data"></param>
        /// <param name ="sheet"></param>
        public void WriteData(int Row, int Column, string data, NPOI.SS.UserModel.ISheet sheet)
        {
            sheet.CreateRow(Row).CreateCell(Column).SetCellValue(data);
        }

/// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="Row"></param>
        /// <param name="Column"></param>
        /// <param name="data"></param>
        /// <param name="sheet"></param>
        public void WriteData(int Row, int Column, double data, NPOI.SS.UserModel.ISheet sheet)
        {
            sheet.CreateRow(Row).CreateCell(Column).SetCellValue(data);
        }

/// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="Row"></param>
        /// <param name="Column"></param>
        /// <param name="data"></param>
        /// <param name="sheet"></param>
        public void WriteData(int Row, int Column, bool data, NPOI.SS.UserModel.ISheet sheet)
        {
            sheet.CreateRow(Row).CreateCell(Column).SetCellValue(data);
        }

/// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="Row"></param>
        /// <param name="Column"></param>
        /// <param name="data"></param>
        /// <param name="sheet"></param>
        /// <param name="Flag"></param>
        public void WriteData(int Row, int Column, DateTime data, NPOI.SS.UserModel.ISheet sheet,string Flag)
        {
            sheet.CreateRow(Row).CreateCell(Column).SetCellValue(data);
            setFormat(Row, Column, Flag, sheet);
        }

/// <summary>
        /// 写入数据
       /// </summary>
       /// <param name="Row"></param>
       /// <param name="data"></param>
       /// <param name="sheet"></param>
        public void WriteData(int Row,string[] data,NPOI.SS.UserModel.ISheet sheet)
        {
            int Column = 0;
            foreach (string str in data)
            {
                if (Column == 0)
                {
                    sheet.CreateRow(Row).CreateCell(Column).SetCellValue(str);
                }
                else
                {
                    sheet.GetRow(Row).CreateCell(Column).SetCellValue(str);
                }
                Column++;
            }
        }

/// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="Row"></param>
        /// <param name="data"></param>
        /// <param name="sheet"></param>
        public void WriteData(int Row, double[] data, NPOI.SS.UserModel.ISheet sheet)
        {
            int Column = 0;
            foreach (double str in data)
            {
                if (Column == 0)
                {
                    sheet.CreateRow(Row).CreateCell(Column).SetCellValue(str);
                }
                else
                {
                    sheet.GetRow(Row).CreateCell(Column).SetCellValue(str);
                }
                Column++;
            }
        }

/// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="Row"></param>
        /// <param name="data"></param>
        /// <param name="sheet"></param>
        public void WriteData(int Row, bool[] data, NPOI.SS.UserModel.ISheet sheet)
        {
            int Column = 0;
            foreach (bool str in data)
            {
                if (Column == 0)
                {
                    sheet.CreateRow(Row).CreateCell(Column).SetCellValue(str);
                }
                else
                {
                    sheet.GetRow(Row).CreateCell(Column).SetCellValue(str);
                }
                Column++;
            }
        }

/// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="Row"></param>
        /// <param name="data"></param>
        /// <param name="sheet"></param>
        /// <param name="Flag"></param>
        public void WriteData(int Row, DateTime[] data, NPOI.SS.UserModel.ISheet sheet,string Flag)
        {
            int Column = 0;
            foreach (DateTime str in data)
            {
                if (Column == 0)
                {
                    sheet.CreateRow(Row).CreateCell(Column).SetCellValue(str);
                }
                else
                {
                    sheet.GetRow(Row).CreateCell(Column).SetCellValue(str);
                }
                setFormat(Row, Column, Flag, sheet);
                Column++;
            }
        }

/// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="diff"></param>
        /// <param name="Column"></param>
        /// <param name="data"></param>
        /// <param name="sheet"></param>
        public void WriteData(bool diff,int Column, string[] data, NPOI.SS.UserModel.ISheet sheet)
        {
            int Row = 0;
            foreach (string str in data)
            {
                if (diff)
                {
                    sheet.CreateRow(Row).CreateCell(Column).SetCellValue(str);
                }
                else
                {
                    sheet.GetRow(Row).CreateCell(Column).SetCellValue(str);
                }
                Row++;
            }
        }

/// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="diff"></param>
        /// <param name="Column"></param>
        /// <param name="data"></param>
        /// <param name="sheet"></param>
        public void WriteData(bool diff,int Column, double[] data, NPOI.SS.UserModel.ISheet sheet)
        {
            int Row = 0;
            foreach (double str in data)
            {
                if (diff)
                {
                    sheet.CreateRow(Row).CreateCell(Column).SetCellValue(str);
                }
                else
                {
                    sheet.GetRow(Row).CreateCell(Column).SetCellValue(str);
                }
                Row++;
            }
        }

/// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="diff"></param>
        /// <param name="Column"></param>
        /// <param name="data"></param>
        /// <param name="sheet"></param>
        public void WriteData(bool diff, int Column, bool[] data, NPOI.SS.UserModel.ISheet sheet)
        {
            int Row = 0;
            foreach (bool str in data)
            {
                if (diff)
                {
                    sheet.CreateRow(Row).CreateCell(Column).SetCellValue(str);
                }
                else
                {
                    sheet.GetRow(Row).CreateCell(Column).SetCellValue(str);
                }
                Row++;
            }
        }

/// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="diff"></param>
        /// <param name="Column"></param>
        /// <param name="data"></param>
        /// <param name="sheet"></param>
        /// <param name="Flag"></param>
        public void WriteData(bool diff, int Column, DateTime[] data, NPOI.SS.UserModel.ISheet sheet,string Flag)
        {
            int Row = 0;
            foreach (DateTime str in data)
            {
                if (diff)
                {
                    sheet.CreateRow(Row).CreateCell(Column).SetCellValue(str);
                }
                else
                {
                    sheet.GetRow(Row).CreateCell(Column).SetCellValue(str);
                }
                Row++;
            }
        }

/// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="Row"></param>
        /// <param name="Column"></param>
        /// <param name="data"></param>
        /// <param name="sheet"></param>
        public void WriteData(int Row, int Column, string[,] data, NPOI.SS.UserModel.ISheet sheet)
        {
            int lengthRow = data.GetLength(0);
            int lengthColumn = data.GetLength(1);

for (int n = 0; n < lengthRow; n++)
            {
                for (int m = 0; m < lengthColumn; m++)
                {
                    if (m == 0)
                    {
                        sheet.CreateRow(Row + n).CreateCell(Column + m).SetCellValue(data[n, m]);
                    }
                    else
                    {
                        sheet.GetRow(Row + n).CreateCell(Column + m).SetCellValue(data[n, m]);
                    }

}
            }
        }

/// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="Row"></param>
        /// <param name="Column"></param>
        /// <param name="data"></param>
        /// <param name="sheet"></param>
        public void WriteData(int Row, int Column, double[,] data, NPOI.SS.UserModel.ISheet sheet)
        {
            int lengthRow = data.GetLength(0);
            int lengthColumn = data.GetLength(1);

for (int n = 0; n < lengthRow; n++)
            {
                for (int m = 0; m < lengthColumn; m++)
                {
                    if (m == 0)
                    {
                        sheet.CreateRow(Row + n).CreateCell(Column + m).SetCellValue(data[n, m]);
                    }
                    else
                    {
                        sheet.GetRow(Row + n).CreateCell(Column + m).SetCellValue(data[n, m]);
                    }
                }
            }
        }

/// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="Row"></param>
        /// <param name="Column"></param>
        /// <param name="data"></param>
        /// <param name="sheet"></param>
        public void WriteData(int Row, int Column, bool[,] data, NPOI.SS.UserModel.ISheet sheet)
        {
            int lengthRow = data.GetLength(0);
            int lengthColumn = data.GetLength(1);

for (int n = 0; n < lengthRow; n++)
            {
                for (int m = 0; m < lengthColumn; m++)
                {
                    if (m == 0)
                    {
                        sheet.CreateRow(Row + n).CreateCell(Column + m).SetCellValue(data[n, m]);
                    }
                    else
                    {
                        sheet.GetRow(Row + n).CreateCell(Column + m).SetCellValue(data[n, m]);
                    }
                }
            }
        }

/// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="Row"></param>
        /// <param name="Column"></param>
        /// <param name="data"></param>
        /// <param name="sheet"></param>
        /// <param name="Flag"></param>
        public void WriteData(int Row, int Column, DateTime[,] data, NPOI.SS.UserModel.ISheet sheet,string Flag)
        {
            int lengthRow = data.GetLength(0);
            int lengthColumn = data.GetLength(1);

for (int n = 0; n < lengthRow; n++)
            {
                for (int m = 0; m < lengthColumn; m++)
                {
                    if (m == 0)
                    {
                        sheet.CreateRow(Row + n).CreateCell(Column + m).SetCellValue(data[n, m]);
                    }
                    else
                    {
                        sheet.GetRow(Row + n).CreateCell(Column + m).SetCellValue(data[n, m]);
                    }
                    setFormat(Row, Column, Flag, sheet);
                }
            }
        }

/// <summary>
        /// 写入标签
        /// </summary>
        /// <param name="boll"></param>
        /// <param name="Row"></param>
        /// <param name="Column"></param>
        /// <param name="str"></param>
        /// <param name="author"></param>
        /// <param name="sheet"></param>
        /// <param name="dx1"></param>
        /// <param name="dy1"></param>
        /// <param name="dx2"></param>
        /// <param name="dy2"></param>
        /// <param name="row1"></param>
        /// <param name="col1"></param>
        /// <param name="row2"></param>
        /// <param name="col2"></param>
        public void WriteComment(bool boll,int Row, int Column, string str, string author, NPOI.SS.UserModel.ISheet sheet, int dx1, int dy1, int dx2, int dy2, int row1, int col1, int row2, int col2)
        {
            NPOI.SS.UserModel.IDrawing drawing = sheet.CreateDrawingPatriarch();
            NPOI.SS.UserModel.IComment comment = drawing.CreateCellComment(new HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2));

comment.String = new HSSFRichTextString(str);
            comment.Author = author;

if (boll)
            {
                comment.Visible = true;
            }

NPOI.SS.UserModel.ICell cell = sheet.CreateRow(Row).CreateCell(Column);
            cell.CellComment = comment;
        }

/// <summary>
        /// 设置格式
        /// </summary>
        /// <param name="Row"></param>
        /// <param name="Column"></param>
        /// <param name="Format"></param>
        /// <param name="sheet"></param>
        public void setFormat(int Row, int Column, string Format, NPOI.SS.UserModel.ISheet sheet)
        {
            NPOI.SS.UserModel.ICell cell = sheet.GetRow(Row).GetCell(Column);
            NPOI.SS.UserModel.ICellStyle cellStyle = hssfwork.CreateCellStyle();
            NPOI.SS.UserModel.IDataFormat format = hssfwork.CreateDataFormat();
            cellStyle.DataFormat = format.GetFormat(Format);
            cell.CellStyle = cellStyle;
        }

/// <summary>
        /// 设置格式
        /// </summary>
        /// <param name="boll"></param>
        /// <param name="Row"></param>
        /// <param name="Column"></param>
        /// <param name="Format"></param>
        /// <param name="sheet"></param>
        public void setFormat(bool boll, int Row, int Column, string Format, NPOI.SS.UserModel.ISheet sheet)
        {
            NPOI.SS.UserModel.ICell cell = sheet.GetRow(Row).GetCell(Column);
            NPOI.SS.UserModel.ICellStyle cellStyle = hssfwork.CreateCellStyle();

cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat(Format);  //内嵌写入
            cell.CellStyle = cellStyle;
        }

/// <summary>
        /// 合并区域
        /// </summary>
        /// <param name="Row"></param>
        /// <param name="Column"></param>
        /// <param name="sheet"></param>
        /// <param name="FirstRow"></param>
        /// <param name="FirstColumn"></param>
        /// <param name="LastRow"></param>
        /// <param name="LastColumn"></param>
        public void MargeRegion(int Row,int Column ,NPOI.SS.UserModel.ISheet sheet,int FirstRow,int FirstColumn,int LastRow,int LastColumn)
        {
            NPOI.SS.UserModel.ICell cell = sheet.CreateRow(Row).CreateCell(Column);
            sheet.AddMergedRegion(new CellRangeAddress(FirstRow, LastRow, FirstColumn, LastColumn));
        }

public object ReadData(int Row,int Column,NPOI.SS.UserModel.ISheet sheet,int checkd)
        {
            object data = null;
            switch (checkd)
            {
                case 0:
                    data = sheet.GetRow(Row).GetCell(Column).StringCellValue;
                    break;
                case 1:
                    data = sheet.GetRow(Row).GetCell(Column).DateCellValue;
                    break;
                case 2:
                    data = sheet.GetRow(Row).GetCell(Column).NumericCellValue;
                    break;
                case 3:
                    data = sheet.GetRow(Row).GetCell(Column).BooleanCellValue;
                    break;
            }
            return data;
            //short num = sheet.GetRow(2).LastCellNum;
        }

public List<List<string>> ReadData(NPOI.SS.UserModel.ISheet sheet)
        {
            List<string> data = new List<string>();
            List<List<string>> listdata = new List<List<string>>();
            int Rownum = sheet.PhysicalNumberOfRows;
            for (int i = 0; i < Rownum; i++)
            {
                int Column = sheet.GetRow(i).PhysicalNumberOfCells;
                for (int m = 0; m < Column; m++)
                {
                    data.Add(sheet.GetRow(i).GetCell(m).StringCellValue);
                }
                listdata.Add(data);
                data = new List<string>();
            }
            return listdata;
        }
 
    }
}

NPOI Excel类的更多相关文章

  1. NET npoi帮助类

    nuget添加npoi /// <summary> /// npoi帮助类 /// </summary> public static class NpoiHelper { // ...

  2. NPOI 帮助类

    NPOI 帮助类 代码实现了来自于互联网 using System; using System.Data; using System.IO; using System.Text; using NPOI ...

  3. 转载 NPOI Excel 单元格背景颜色对照表

    NPOI Excel 单元格颜色对照表,在引用了 NPOI.dll 后可通过 ICellStyle 接口的 FillForegroundColor 属性实现 Excel 单元格的背景色设置,FillP ...

  4. NPOI Excel 单元格背景颜色对照表

    NPOI Excel 单元格颜色对照表,在引用了 NPOI.dll 后可通过 ICellStyle 接口的 FillForegroundColor 属性实现 Excel 单元格的背景色设置,FillP ...

  5. 022医疗项目-模块二:药品目录的导入导出-对XSSF导出excel类进行封装

    资源全部来源于传智播客. 好的架构师写的程序,就算给刚入门的新手看,新手一看就知道怎么去用.所以我们要对XSSF导出excel类进行封装.这是架构师的工作,但我们也要知道. 我们写一个封装类: 这个类 ...

  6. <转载>NPOI Excel 单元格背景颜色对照表

    我转载地址:http://www.holdcode.com/web/details/117 NPOI Excel 单元格颜色对照表,在引用了 NPOI.dll 后可通过 ICellStyle 接口的 ...

  7. ASP.NET导出EXCEL类

    最新ASP.NET导出EXCEL类 说明:可以导出ASP.NET页面和DATAGRID(WebControl)数据,可以导出表单头 using System;using System.Data;usi ...

  8. C#利用NPOI导出Excel类(简单版)

    代码: using System.Data; using System.IO; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; namespac ...

  9. NPOI操作EXCEL 类代码

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using NPOI.SS. ...

随机推荐

  1. Can't get WebApplicationContext object from ContextRegistry.GetContext(): Resource handler for the 'web' protocol is not defined

    I'm stucked in configuring my web.config file under a web forms project in order to get an instance ...

  2. 2 个UserControl 的传值问题

    问题描述:有2个UserControl:UserControl1 里有一个Button,UserControl2 里面有一个TextBox,这2个控件都加载到了主窗体Form1 上.要求的是,点击 U ...

  3. openlayers 注册事件例子

    registerEvents:function() { this.events = new OpenLayers.Events(this, this.div, null, true); functio ...

  4. 在linux安装mysql,并设置远程访问

    1.查看系统有没有安装mysql. vpm -qa mysql 发现有删除:rpm -e mysql(rpm -e --nodeps mysql) 2.下载数据库 mysql-standard-5.0 ...

  5. Maven进价:eclipse中集成maven

    一.M2Eclipse插件 m2eclipse是一个在Eclipse中集成Maven的插件,有了该插件,用户可以方便的在Eclipse中执行Maven命令.创建Maven项目.修改POM文件等. 下载 ...

  6. WCF安全3-Transport与Message安全模式

    概述: WCF的安全传输主要涉及认证.消息一致性和机密性三个主题.WCF采用两种不同的机制来解决这三个涉及传输安全的问题,一般将它们成为不同的安全模式,即Transport安全模式和Message安全 ...

  7. 栈的图文解析 和 对应3种语言的实现(C/C++/Java)

    概要 本章会先对栈的原理进行介绍,然后分别通过C/C++/Java三种语言来演示栈的实现示例.注意:本文所说的栈是数据结构中的栈,而不是内存模型中栈.内容包括:1. 栈的介绍2. 栈的C实现3. 栈的 ...

  8. ASP.NET MVC3 在_ViewStart設定Layout後用RenderAction的注意事項

    ASP.NET MVC3 在_ViewStart設定Layout後用RenderAction的注意事項 3/24 TW MVC第一次活動圓滿的結束了,雖然是RC,但也來了不少願意聽我們分享的好朋友. ...

  9. django 快速实现注册

    前言 对于web开来说,用户登陆.注册.文件上传等是最基础的功能,针对不同的web框架,相关的文章非常多,但搜索之后发现大多都不具有完整性,对于想学习web开发的新手来说不具有很强的操作性:对于web ...

  10. .Net魔法堂:发个带附件的邮件

    一.前言   由于工作需要最近把邮件发送封装成WebService,现在把代码记录在此,以便日后查阅. 二.二话不说写代码 private void _SendMail(string form, st ...