NPOI 读写Excel

实例功能概述:

1、支持Excel2003以及2007

2、支持Excel读取到DataTable(TableToExcel)

3、支持DataTable导出到Excel(TableToExcel)

4、支持WPF DataGrid导出到Excel(SelectedRowToExcel,AllRowToExcel)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Win32;
using Microsoft.Windows.Controls;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace LT.Utility
{
    /// <summary>
    /// FileName: NpoiExcelHelper.cs
    /// CLRVersion: 4.0.30319.18052
    /// Author: ZhangLei
    /// Corporation: Litu
    /// Description:Npoi Excel操作类封装
    /// DateTime: 2016/1/24 23:01:21
    /// </summary>
    public class NpoiExcelHelper
    {
        #region Public Interface

        /// <summary>
        /// 读取Excel到DataTable<see cref="System.Data.DataTable"/>
        /// </summary>
        /// <param name="file">Excel路径,如"E:\hello.xls"</param>
        /// <returns></returns>
        public static DataTable ExcelToTable(string file)
        {
            var type = GetFileType(file);
            if ( type== FileType.Excel2003)
            {
                return ExcelToTableForXLS(file);
            }
            else if (type == FileType.Excel2007)
            {
                return ExcelToTableForXLSX(file);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// DataTable 导出到 Excel
        /// </summary>
        /// <param name="dt"></param>
        public static void TableToExcel(DataTable dt)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Excel 2003(*.xls)|*.xls|Excel 2007(*.xlsx)|*.xlsx";
            saveFileDialog.FilterIndex = 1;
            if (saveFileDialog.ShowDialog() == true)
            {
                if (saveFileDialog.FilterIndex == 1)
                {
                    TableToExcelForXLS(dt,saveFileDialog.FileName);
                }
                else
                {
                    TableToExcelForXLSX(dt, saveFileDialog.FileName);
                }
            }
        }

        /// <summary>
        /// DataTable 导出到 Excel
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="file">Excel路径,如"E:\hello.xls"</param>
        public static void TableToExecel(DataTable dt, string file)
        {
            var type = GetFileType(file);
            if (type == FileType.Excel2003)
            {
                TableToExcelForXLS(dt, file);
            }
            else if (type == FileType.Excel2007)
            {
                TableToExcelForXLSX(dt, file);
            }
            else
            {
                throw new  Exception("暂不支持此种文件类型的输出");
            }
        }

        /// <summary>
        /// WPF DataGrid选中行数据导出到Excel
        /// </summary>
        /// <param name="dataGrid"></param>
        public static void SelectedRowToExcel(DataGrid dataGrid)
        {
            var dataView = dataGrid.ItemsSource as DataView;
            if (dataView != null)
            {
                DataTable dt = dataView.ToTable();
                dt.Rows.Clear();
                int sRowCount = dataGrid.SelectedItems.Count;
                int col = dataGrid.Columns.Count;

                var sItems = dataGrid.SelectedItems;
                DataRow newRow = null;
                DataRowView drv = null;
                for (int i = 0; i < sRowCount; i++)
                {
                    newRow = dt.NewRow();
                    for (int j = 0; j < col; j++)
                    {
                        drv = sItems[i] as DataRowView;
                        newRow[j] = drv[j];
                    }
                    dt.Rows.Add(newRow);
                }
                TableToExcel(dt);
            }
            else
            {
                throw new Exception("DataGrid 数据源为null,无法导出到Excel");
            }
        }

        /// <summary>
        /// WPF DataGrid全部数据导出到Excel
        /// </summary>
        /// <param name="dataGrid"></param>
        public static void AllRowToExcel(DataGrid dataGrid)
        {
            var dataView = dataGrid.ItemsSource as DataView;
            if (dataView != null)
            {
                DataTable dt = dataView.ToTable();
                TableToExcel(dt);
            }
            else
            {
                throw new Exception("DataGrid 数据源为null,无法导出到Excel");
            }
        }
        #endregion

        #region Common Function

        private static FileType GetFileType(string file)
        {
            string extention = Path.GetExtension(file).ToLower();
           if(extention.Equals(".xls"))
            {
               return FileType.Excel2003;
            }
           else if (extention.Equals(".xlsx"))
           {
               return FileType.Excel2007;
           }
           else
           {
               return FileType.Other;
           }
        }

        enum FileType
        {
            Excel2003,
            Excel2007,
            Other
        }
        #endregion

        #region Excel2003
        /// <summary>
        /// 将Excel文件中的数据读出到DataTable中(xls)
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        private static DataTable ExcelToTableForXLS(string file)
        {
            DataTable dt = new DataTable();
            using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
            {

                HSSFWorkbook hssfworkbook = new HSSFWorkbook(fs);
                ISheet sheet = hssfworkbook.GetSheetAt(0);

                //表头
                IRow header = sheet.GetRow(sheet.FirstRowNum);
                List<int> columns = new List<int>();
                for (int i = 0; i < header.LastCellNum; i++)
                {
                    object obj = GetValueTypeForXLS(header.GetCell(i) as HSSFCell);
                    if (obj == null || obj.ToString() == string.Empty)
                    {
                        dt.Columns.Add(new DataColumn("Columns" + i.ToString()));
                        //continue;
                    }
                    else
                        dt.Columns.Add(new DataColumn(obj.ToString()));
                    columns.Add(i);
                }
                //数据
                for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
                {
                    DataRow dr = dt.NewRow();
                    bool hasValue = false;
                    foreach (int j in columns)
                    {
                        dr[j] = GetValueTypeForXLS(sheet.GetRow(i).GetCell(j) as HSSFCell);
                        if (dr[j] != null && dr[j].ToString() != string.Empty)
                        {
                            hasValue = true;
                        }
                    }
                    if (hasValue)
                    {
                        dt.Rows.Add(dr);
                    }
                }
            }
            return dt;
        }

        /// <summary>
        /// 将DataTable数据导出到Excel文件中(xls)
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="file"></param>
        private static void TableToExcelForXLS(DataTable dt, string file)
        {
            NPOI.HSSF.UserModel.HSSFWorkbook hssfworkbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
            ISheet sheet = hssfworkbook.CreateSheet("Test");

            //表头
            IRow row = sheet.CreateRow(0);
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                ICell cell = row.CreateCell(i);
                cell.SetCellValue(dt.Columns[i].ColumnName);
            }

            //数据
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                IRow row1 = sheet.CreateRow(i + 1);
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    ICell cell = row1.CreateCell(j);
                    cell.SetCellValue(dt.Rows[i][j].ToString());
                }
            }

            //转为字节数组
            MemoryStream stream = new MemoryStream();
            hssfworkbook.Write(stream);
            var buf = stream.ToArray();

            //保存为Excel文件
            using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write))
            {
                fs.Write(buf, 0, buf.Length);
                fs.Flush();
            }
        }

        /// <summary>
        /// 获取单元格类型(xls)
        /// </summary>
        /// <param name="cell"></param>
        /// <returns></returns>
        private static object GetValueTypeForXLS(HSSFCell cell)
        {
            if (cell == null)
                return null;
            switch (cell.CellType)
            {
                case CellType.Blank: //BLANK:
                    return null;
                case CellType.Boolean: //BOOLEAN:
                    return cell.BooleanCellValue;
                case CellType.Numeric: //NUMERIC:
                    return cell.NumericCellValue;
                case CellType.String: //STRING:
                    return cell.StringCellValue;
                case CellType.Error: //ERROR:
                    return cell.ErrorCellValue;
                case CellType.Formula: //FORMULA:
                default:
                    return "=" + cell.CellFormula;
            }
        }
        #endregion

        #region Excel2007及以上版本
        /// <summary>
        /// 将Excel文件中的数据读出到DataTable中(xlsx)
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        private static DataTable ExcelToTableForXLSX(string file)
        {
            DataTable dt = new DataTable();
            using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
            {
                //NPOI.XSSF.UserModel

                XSSFWorkbook xssfworkbook = new XSSFWorkbook(fs);
                ISheet sheet = xssfworkbook.GetSheetAt(0);

                //表头
                IRow header = sheet.GetRow(sheet.FirstRowNum);
                List<int> columns = new List<int>();
                for (int i = 0; i < header.LastCellNum; i++)
                {
                    object obj = GetValueTypeForXLSX(header.GetCell(i) as XSSFCell);
                    if (obj == null || obj.ToString() == string.Empty)
                    {
                        dt.Columns.Add(new DataColumn("Columns" + i.ToString()));
                        //continue;
                    }
                    else
                        dt.Columns.Add(new DataColumn(obj.ToString()));
                    columns.Add(i);
                }
                //数据
                for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
                {
                    DataRow dr = dt.NewRow();
                    bool hasValue = false;
                    foreach (int j in columns)
                    {
                        dr[j] = GetValueTypeForXLSX(sheet.GetRow(i).GetCell(j) as XSSFCell);
                        if (dr[j] != null && dr[j].ToString() != string.Empty)
                        {
                            hasValue = true;
                        }
                    }
                    if (hasValue)
                    {
                        dt.Rows.Add(dr);
                    }
                }
            }
            return dt;
        }

        /// <summary>
        /// 将DataTable数据导出到Excel文件中(xlsx)
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="file"></param>
        private static void TableToExcelForXLSX(DataTable dt, string file)
        {

            XSSFWorkbook xssfworkbook = new XSSFWorkbook();
            ISheet sheet = xssfworkbook.CreateSheet("Test");

            //表头
            IRow row = sheet.CreateRow(0);
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                ICell cell = row.CreateCell(i);
                cell.SetCellValue(dt.Columns[i].ColumnName);
            }

            //数据
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                IRow row1 = sheet.CreateRow(i + 1);
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    ICell cell = row1.CreateCell(j);
                    cell.SetCellValue(dt.Rows[i][j].ToString());
                }
            }

            //转为字节数组
            MemoryStream stream = new MemoryStream();
            xssfworkbook.Write(stream);
            var buf = stream.ToArray();

            //保存为Excel文件
            using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write))
            {
                fs.Write(buf, 0, buf.Length);
                fs.Flush();
            }
        }

        /// <summary>
        /// 获取单元格类型(xlsx)
        /// </summary>
        /// <param name="cell"></param>
        /// <returns></returns>
        private static object GetValueTypeForXLSX(XSSFCell cell)
        {
            if (cell == null)
                return null;
            switch (cell.CellType)
            {
                case CellType.Blank: //BLANK:
                    return null;
                case CellType.Boolean: //BOOLEAN:
                    return cell.BooleanCellValue;
                case CellType.Numeric: //NUMERIC:
                    return cell.NumericCellValue;
                case CellType.String: //STRING:
                    return cell.StringCellValue;
                case CellType.Error: //ERROR:
                    return cell.ErrorCellValue;
                case CellType.Formula: //FORMULA:
                default:
                    return "=" + cell.CellFormula;
            }
        }
        #endregion

    }  

}

  完整类文件及依赖库下载

时间: 2024-10-12 11:52:00

NPOI 读写Excel的相关文章

npoi读写excel

npoi读取excel方法: 首先下载:npoi.dll 添加引用: //读写excel需要的组建using NPOI.HPSF;using NPOI.HSSF.UserModel;using NPOI.POIFS.FileSystem;using NPOI.HSSF.Util;using NPOI.SS.UserModel;using NPOI.XSSF.UserModel;using NPOI.OpenXml4Net; IWorkbook workbook = null;//全局workbo

C#使用NPOI读写excel

个人比较习惯用NPOI操作excel,方便易理解.在宇宙第一IDE(笑)——VS2017中插入NPOI就很方便: 首先安装NPOI: 然后在.cs文件中加入如下引用: using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using NPOI.HSSF.UserModel; XSSF是用于.xlsx(2007以后版本) HSSF是用于.xls(2007以前版本) 同时我的代码中要用到Datatable,用于存储表格数据 读写文件需要IO usin

NPOI读写Excel表格、Word文档

NPOI是从POI移植过来的.NET版本,专门对Word.Excel进行读写操作的一个开源项目 下面就试着怎么用我们的C#来进行读写操作,我现在用的Office版本是2016 我们首先通过Nuget把NPOI引入到我们的项目中 1.读取Excel表格 using(FileStream fs=new FileStream(@"C:\Users\BIDIANQING\Desktop\1.xlsx", FileMode.Open,FileAccess.Read)) { XSSFWorkboo

NPOI读写Excel——补充

using System; using System.Collections.Generic; using System.Text; using System.Data; using System.IO; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; namespace Common.Excel { public static class NPOIHandler { public static DataSet ExcelToDataSet

NPOI读写Excel sheet操作

QueryInfo dataInfo = new QueryInfo(); dataInfo.CustomSQL = $@" select t1.name name,t1.url url from sys_menu t1 start with t1.parent_id = ( select t2.id from sys_menu t2 where t2.name ='交易源数据' ) connect by t1.parent_id=t1.id "; var descpsInfo = n

NPOI 导入Excel和读取Excel

1.整个Excel表格叫做工作表:WorkBook(工作薄),包含的叫页(工作表):Sheet:行:Row:单元格Cell. 2.NPOI是POI的C#版本,NPOI的行和列的index都是从0开始 3.POI读取Excel有两种格式一个是HSSF,另一个是XSSF. HSSF和XSSF的区别如下: HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. XSSF is the

C# 使用 NPOI 库读写 Excel 文件(转载)

时间:2014-08-04 13:43:57                         阅读:3411                         评论:0                         收藏:0                         [点我收藏+] NPOI 是开源的 POI 项目的.NET版,可以用来读写Excel,Word,PPT文件.在处理Excel文件上,NPOI 可以同时兼 容xls 和 xlsx.官网提供了一份Examples,给出了很多应用场

C# 中 NPOI 库读写 Excel 文件的方法【摘】

原作:淡水网志 NPOI 是开源的 POI 项目的.NET版,可以用来读写Excel,Word,PPT文件.在处理Excel文件上,NPOI 可以同时兼容 xls 和 xlsx.官网提供了一份 Examples,给出了很多应用场景的例子,打包好的二进制文件类库,也仅有几MB,使用非常方便. 读Excel NPOI使用HSSFWorkbook类来处理xls,XSSFWorkbook类来处理xlsx,它们都继承接口IWorkbook,因此可以通过IWorkbook来统一处理xls和xlsx格式的文件

Npoi简单读写Excel

什么是NPOI ? 简而言之,NPOI就是可以在没有Office的情况下对Word或Excel文档进行读写等操作. 使用方式 : 1.准备NPOI的dll文件 下载链接:https://npoi.codeplex.com/releases 2.将下载的dll文件引入项目中 3.引用命名空间     须知: 1.Excel表格分为:WorkBook(工作薄)-> Sheet(工作表) -> 行:Row 单元格:Cell. 2.NPOI是POI的C#版本,NPOI的行和列的index都是从0开始