使用NPOI访问、控制Excel

工作中经常使用Excel,很多重复的数据操作或者检查都丢给程序做是最好的。

最一开始选择使用Com组件来作业,但是效果不太理想,全是动态类型真的用起来有点累……

之后选择了OleDB进行excel操作,但是因为众所周知的“一列只能为一种类型”的原因,数据常常取不全,最终放弃了。

最终选择了NPOI作为解决方案,目前效果比较理想,写了个助手方法,方便日常使用。

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;

namespace Rainbow.Core
{
    /// <summary>
    /// Helper for NOPI to operate excel files.
    /// </summary>
    public static class NPOIHelper
    {
        #region public methods
        /// <summary>
        /// Get the IWorkbook of the dest excel file.
        /// </summary>
        /// <param name="filePath">The dest file path.</param>
        /// <exception cref="RainstormException"></exception>
        /// <returns></returns>
        static public IWorkbook GetDestWorkBook(string filePath)
        {
            CoreUtility.CheckExcelFile(filePath);

            IWorkbook workBook = null;
            FunctionExcuteHelper.ExcuteIOFunction(() =>
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    if (Path.GetExtension(filePath) == ".xls")
                        workBook = new HSSFWorkbook(fs);
                    else
                        workBook = new XSSFWorkbook(fs);
                }
            });
            return workBook;
        }
        /// <summary>
        /// Get the ISheet of the dest sheet.
        /// </summary>
        /// <param name="filePath">The dest file path.</param>
        /// <param name="sheetName">The dest sheet name.</param>
        /// <returns></returns>
        static public ISheet GetDestWorkSheet(string filePath, string sheetName)
        {
            IWorkbook workBook = GetDestWorkBook(filePath);

            ISheet workSheet = null;
            workSheet = GetDestWorkSheet(workBook, sheetName);

            return workSheet;
        }
        /// <summary>
        /// Get the ISheet of the dest sheet.
        /// </summary>
        /// <param name="workBook">The IWorkbook dest file.</param>
        /// <param name="sheetName">The dest sheet name.</param>
        /// <returns></returns>
        static public ISheet GetDestWorkSheet(IWorkbook workBook, string sheetName)
        {
            ISheet workSheet = null;
            workSheet = workBook.GetSheet(sheetName);

            if (workSheet == null)
                throw new Exception(string.Format("There is no sheet named <{0}> in the excel.", sheetName));

            return workSheet;
        }
        /// <summary>
        /// Read dest sheet as a DataTable.
        /// The column is named with ‘F1 F2 ...‘
        /// </summary>
        /// <param name="filePath">The dest file path.</param>
        /// <param name="sheetName">The dest sheet name.</param>
        /// <param name="startRow">Start line number.</param>
        /// <returns></returns>
        static public DataTable ReadSheetAsDataTable(string filePath, string sheetName, int startRow = 0)
        {
            ISheet sheet = GetDestWorkSheet(filePath, sheetName);
            return GetDataTableFromISheet(sheet, startRow);
        }
        /// <summary>
        /// Read dest excel as a DataTable.
        /// The tables are named with sheet name.
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        static public DataSet ReadExcelAsDataSet(string filePath)
        {
            string fileName = System.IO.Path.GetFileNameWithoutExtension(filePath);
            DataSet ds = new DataSet(fileName);

            IWorkbook workBook = GetDestWorkBook(filePath);
            for (int i = 0; i < workBook.NumberOfSheets; i++)
                ds.Tables.Add(GetDataTableFromISheet(workBook.GetSheetAt(i)));
            return ds;
        }
        /// <summary>
        /// Get the string value of the ICell.
        /// </summary>
        /// <param name="cell">Dest ICell.</param>
        /// <returns></returns>
        static public string GetDestCellStringValue(ICell cell)
        {
            if (cell == null)
            {
                return string.Empty;
            }

            string result = string.Empty;
            switch (cell.CellType)
            {
                case CellType.String:
                    result = cell.StringCellValue;
                    break;
                case CellType.Numeric:
                    if (DateUtil.IsCellDateFormatted(cell))
                        result = cell.DateCellValue.ToShortDateString();
                    else
                        result = cell.NumericCellValue.ToString();
                    break;
                default:
                    result = string.Empty;
                    break;
            }
            return result;
        }
        /// <summary>
        /// Set dest cell a string value.
        /// </summary>
        /// <typeparam name="TValue">string ,int or any Type overrided ToString().</typeparam>
        /// <param name="row"></param>
        /// <param name="cellPosition"></param>
        /// <param name="value"></param>
        static public void SetDestCellStringValue<TValue>(IRow row, int cellPosition, TValue value)
        {
            if (row == null)
                throw new Exception("The row is a NULL row.");

            if (row.GetCell(cellPosition) == null)
                row.CreateCell(cellPosition).SetCellValue(value.ToString());
            else
                row.GetCell(cellPosition).SetCellValue(value.ToString());
        }
        /// <summary>
        /// Set dest cell a string value and dest style.
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="row"></param>
        /// <param name="cellPosition"></param>
        /// <param name="style"></param>
        /// <param name="value"></param>
        static public void SetDestCellStringValueAndStyle<TValue>(IRow row, int cellPosition, ICellStyle style, TValue value)
        {
            SetDestCellStringValue<TValue>(row, cellPosition, value);
            ICell cell = row.GetCell(cellPosition);
            cell.CellStyle = style;
        }
        /// <summary>
        /// Get all sheet names as a list.
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        static public List<string> GetAllSheetNames(string filePath)
        {
            return GetAllSheetNames(GetDestWorkBook(filePath));
        }
        /// <summary>
        /// Get all sheet names as a list.
        /// </summary>
        /// <param name="workBook"></param>
        /// <returns></returns>
        static public List<string> GetAllSheetNames(IWorkbook workBook)
        {
            List<string> result = new List<string>();

            for (int i = 0; i < workBook.NumberOfSheets; i++)
                result.Add(workBook.GetSheetName(i));

            return result;
        }

        /// <summary>
        /// Save workBook to dest excel file.
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="workBook"></param>
        static public void SaveWorkBookToExcelFile(IWorkbook workBook, string filePath)
        {
            CoreUtility.CheckExcelFile(filePath);

            FunctionExcuteHelper.ExcuteIOFunction(() =>
                {
                    using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite))
                        workBook.Write(fs);
                });
        }
        /// <summary>
        /// Convert a DataSet to a excel file.
        /// </summary>
        /// <param name="dataSet"></param>
        /// <param name="filePath"></param>
        static public void DataSetToExcel(DataSet dataSet, string filePath)
        {
            CoreUtility.CheckExcelFile(filePath);
            var ext = Path.GetExtension(filePath);

            FunctionExcuteHelper.ExcuteIOFunction(() =>
                {
                    var workbook = DataSetToWorkBook(dataSet, ext);

                    using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                    {
                        workbook.Write(fs);
                    }
                });
        }
        /// <summary>
        /// Read dest ISheet as a DataTable.
        /// The column is named with ‘F1 F2 ...‘
        /// </summary>
        /// <param name="workSheet"></param>
        /// <param name="startRow"></param>
        /// <returns></returns>
        static public DataTable GetDataTableFromISheet(ISheet workSheet, int startRow = 0)
        {
            if (workSheet == null)
                throw new Exception("Dest work sheet does not exists.");

            DataTable result = new DataTable(workSheet.SheetName);

            int columnNumber = GuessSheetColumnNumber(workSheet);

            for (int i = 0; i < columnNumber; i++)
                result.Columns.Add(string.Format("F{0}", i + 1), typeof(string));

            for (int i = startRow; i <= workSheet.LastRowNum; i++)
            {
                IRow row = workSheet.GetRow(i);
                if (row == null)
                    continue;
                result.Rows.Add(ConvertIRowToDataRow(row, result));
            }

            return result;
        }
        #endregion

        #region private methods

        static private int GuessSheetColumnNumber(ISheet workSheet)
        {
            IRow headerRow = workSheet.GetRow(workSheet.FirstRowNum);
            IRow middleRow = workSheet.GetRow(workSheet.LastRowNum / 2);
            IRow lastRow = workSheet.GetRow(workSheet.LastRowNum);

            int result = 0;
            result = Math.Max(headerRow.LastCellNum, middleRow.LastCellNum);
            result = Math.Max(result, lastRow.LastCellNum);
            return result;
        }

        static private DataRow ConvertIRowToDataRow(IRow row, DataTable dataTable)
        {
            DataRow dr = dataTable.NewRow();

            for (int i = 0; i < dataTable.Columns.Count; i++)
                dr[i] = GetDestCellStringValue(row.GetCell(i));

            return dr;
        }

        static private void DataTableToWorkSheet(DataTable dataTable, ISheet sheet)
        {
            IRow headerRow = sheet.CreateRow(0);
            //Set the header
            foreach (DataColumn dc in dataTable.Columns)
                headerRow.CreateCell(dc.Ordinal).SetCellValue(dc.ColumnName);

            //Set the content
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                IRow row = sheet.CreateRow(i + 1);
                for (int j = 0; j < dataTable.Columns.Count; j++)
                    row.CreateCell(j).SetCellValue(dataTable.Rows[i][j].ToString());
            }
        }

        static private IWorkbook DataSetToWorkBook(DataSet dataSet, string extension)
        {
            IWorkbook workBook = null;
            if (extension == ".xls")
                workBook = new HSSFWorkbook();
            else
                workBook = new XSSFWorkbook();

            try
            {
                foreach (DataTable dt in dataSet.Tables)
                {
                    ISheet sheet = null;
                    sheet = workBook.CreateSheet(dt.TableName);
                    DataTableToWorkSheet(dt, sheet);
                }
            }
            catch (Exception)
            {
                throw new Exception(
                    string.Format("Error while converting DataSet <{0}> to WorkBook.", dataSet.DataSetName));
            }

            return workBook;
        }
        #endregion
    }
}

NPOIHelper

时间: 2024-10-12 21:45:54

使用NPOI访问、控制Excel的相关文章

VFP控制Excel操作集

◆访问EXCEL:ExcelSheet = GetObject('','Excel.Sheet')返回结果为类,则成功.例:ExcelSheet = GetObject('','Excel.Sheet')If Type("ExcelSheet")#'O' Then=MessageBox( "访问Excel失败!请检查你的系统是否正确安装 Excel 软件!",48,"Excel不正常")ReturnEndIf ◆创建EXCEL实例:oExcel=

PB控制Excel表格

PB下全面控制Excel 红色部分未验证 Excel对象控制 创建Excel对象 OLEObject ExcelServer ExcelServer = Create OLEObject 连接Excel ExcelServer.ConnectToObject("",  "Excel.Application") //如果Excel还没有打开,则新建. ExcelServer.ConnectToNewObject("Excel.Application"

用Delphi将数据导入到Excel并控制Excel

一.调用Excel的方法:一般情况下有两种方法调用Excel:1.  直接使用Delphi自带的组件:在Form中分别放入ExcelApplication, ExcelWorkbook和ExcelWorksheet.2.动态创建Excel文件:首先创建 Excel 对象,使用ComObj,Excel2000:var ExcelApp: Variant;ExcelApp := CreateOleObject( 'Excel.Application' ); 二.导入数据:在程序中,我们可以将查询到的

Delphi中控制Excel(转载)

用Delphi从数据库中取得资料,然后导出到Excel中做成报表是个不错的选择,因为Excel强大的报表功能那可是没话说前提Delphi中要 uses comobj;var Excel:Variant;一.Excel操作1.创建Excel对象Excel := CreateOleObject( Excel.Application );2.显示ExcelExcel.Visible:=true;3.更改标题Excel.Caption:=Excel的标题;4.退出ExcelExcel.Quit;二.工作

NPOI DataGridView导出EXCEL

NPOI 官方网站 http://npoi.codeplex.com/ 加载NPOI.DLL ,引用这两个命名空间 using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; #region NPOI DataGridView 导出 EXCEL /// <summary> /// NPOI DataGridView 导出 EXCEL /// </summary> /// <param name="fileName"

NPOI导入导出Excel

.net mvc利用NPOI导入导出excel 1.导出Excel :首先引用NPOI包,从这里下载>download(Action一定要用FileResult) /// <summary> /// 批量导出需要导出的列表 /// </summary> /// <returns></returns> public FileResult ExportStu2() { //获取list数据 var checkList = (from oc in db.Or

.Net core 使用NPOI 直接导入Excel到数据库(即不先将Excel保存到服务器再读取文件到数据库)

原文:.Net core 使用NPOI 直接导入Excel到数据库(即不先将Excel保存到服务器再读取文件到数据库) 1 /// <summary> 2 /// 导入信息 3 /// </summary> 4 /// <param name="file"></param> 5 /// <returns></returns> 6 /// /Public/PublicPool/ImportCustomer 7 pub

asp.net Mvc Npoi 导出导入 excel

因近期项目遇到所以记录一下: 首先导出Excel : 首先引用NPOI包 http://pan.baidu.com/s/1i3Fosux (Action一定要用FileResult) /// <summary> /// 批量导出本校第一批派位学生 /// </summary> /// <returns></returns> public FileResult ExportStu2() { string schoolname = "401"

.net mvc利用NPOI导入导出excel

1.导出Excel : 首先引用NPOI包,从这里下载>download (Action一定要用FileResult) /// <summary> /// 批量导出需要导出的列表 /// </summary> /// <returns></returns> public FileResult ExportStu2() { //获取list数据 var checkList = (from oc in db.OrganizeCustoms join o i