用NPOI实现导入导出csv、xls、xlsx数据功能

系统 2311 0

直接上代码

首先定义一个接口

        
          1
        
        
          public
        
        
          interface
        
        
           ITransferData


        
        
          2
        
        
              {


        
        
          3
        
        
                  Stream GetStream(DataTable table);


        
        
          4
        
        
                  DataTable GetData(Stream stream);


        
        
          5
        
             }
      
View Code

如果需要直接操作文件的话,就自己在封装一次

然后定义csv类的具体实现

        
          public
        
        
          class
        
        
           CsvTransferData : ITransferData

    {

        
        
        
          private
        
        
           Encoding _encode;

        
        
        
          public
        
        
           CsvTransferData()

        {

            
        
        
          this
        
        ._encode = Encoding.GetEncoding(
        
          "
        
        
          utf-8
        
        
          "
        
        
          );

        }



        
        
        
          public
        
        
           Stream GetStream(DataTable table)

        {

            StringBuilder sb 
        
        = 
        
          new
        
        
           StringBuilder();

            
        
        
          if
        
         (table != 
        
          null
        
         && table.Columns.Count > 
        
          0
        
         && table.Rows.Count > 
        
          0
        
        
          )

            {

                
        
        
          foreach
        
         (DataRow item 
        
          in
        
        
           table.Rows)

                {

                    
        
        
          for
        
         (
        
          int
        
         i = 
        
          0
        
        ; i < table.Columns.Count; i++
        
          )

                    {

                        
        
        
          if
        
         (i > 
        
          0
        
        
          )

                        {

                            sb.Append(
        
        
          "
        
        
          ,
        
        
          "
        
        
          );

                        }

                        
        
        
          if
        
         (item[i] != 
        
          null
        
        
          )

                        {

                            sb.Append(
        
        
          "
        
        
          \"
        
        
          "
        
        ).Append(item[i].ToString().Replace(
        
          "
        
        
          \"
        
        
          "
        
        , 
        
          "
        
        
          \"\"
        
        
          "
        
        )).Append(
        
          "
        
        
          \"
        
        
          "
        
        
          );

                        }

                    }

                    sb.Append(
        
        
          "
        
        
          \n
        
        
          "
        
        
          );

                }

            }

            MemoryStream stream 
        
        = 
        
          new
        
        
           MemoryStream(_encode.GetBytes(sb.ToString()));

            
        
        
          return
        
        
           stream;

        }



        
        
        
          public
        
        
           DataTable GetData(Stream stream)

        {

            
        
        
          using
        
        
           (stream)

            {

                
        
        
          using
        
         (StreamReader input = 
        
          new
        
        
           StreamReader(stream, _encode))

                {

                    
        
        
          using
        
         (CsvReader csv = 
        
          new
        
         CsvReader(input, 
        
          false
        
        
          ))

                    {

                        DataTable dt 
        
        = 
        
          new
        
        
           DataTable();

                        
        
        
          int
        
         columnCount =
        
           csv.FieldCount;

                        
        
        
          for
        
         (
        
          int
        
         i = 
        
          0
        
        ; i < columnCount; i++
        
          )

                        {

                            dt.Columns.Add(
        
        
          "
        
        
          col
        
        
          "
        
         +
        
           i.ToString());

                        }



                        
        
        
          while
        
        
           (csv.ReadNextRecord())

                        {

                            DataRow dr 
        
        =
        
           dt.NewRow();

                            
        
        
          for
        
         (
        
          int
        
         i = 
        
          0
        
        ; i < columnCount; i++
        
          )

                            {

                                
        
        
          if
        
         (!
        
          string
        
        
          .IsNullOrWhiteSpace(csv[i]))

                                {

                                    dr[i] 
        
        =
        
           csv[i];

                                }

                            }

                            dt.Rows.Add(dr);

                        }

                        
        
        
          return
        
        
           dt;

                    }



                }

            }

        }

    }
        
      
View Code

这个需要引入命名空间LumenWorks.Framework.IO.Csv;
可以Nuget里面下

接着定义excel的实现类的基类

        
          public
        
        
          abstract
        
        
          class
        
        
           ExcelTransferData : ITransferData

    {

        
        
        
          protected
        
        
           IWorkbook _workBook;



        
        
        
          public
        
        
          virtual
        
        
           Stream GetStream(DataTable table)

        {

            
        
        
          var
        
         sheet =
        
           _workBook.CreateSheet();

            
        
        
          if
        
         (table != 
        
          null
        
        
          )

            {

                
        
        
          var
        
         rowCount =
        
           table.Rows.Count;

                
        
        
          for
        
         (
        
          int
        
         i = 
        
          0
        
        ; i < table.Rows.Count; i++
        
          )

                {

                    
        
        
          var
        
         row =
        
           sheet.CreateRow(i);

                    
        
        
          for
        
         (
        
          int
        
         j = 
        
          0
        
        ; j < table.Columns.Count; j++
        
          )

                    {

                        
        
        
          var
        
         cell =
        
           row.CreateCell(j);

                        
        
        
          if
        
         (table.Rows[i][j] != 
        
          null
        
        
          )

                        {

                            cell.SetCellValue(table.Rows[i][j].ToString());

                        }

                    }

                }

            }            

            MemoryStream ms 
        
        = 
        
          new
        
        
           MemoryStream();

            _workBook.Write(ms);

            
        
        
          return
        
        
           ms;

        }



        
        
        
          public
        
        
          virtual
        
        
           DataTable GetData(Stream stream)

        {

            
        
        
          using
        
        
           (stream)

            {

                
        
        
          var
        
         sheet = _workBook.GetSheetAt(
        
          0
        
        
          );

                
        
        
          if
        
         (sheet != 
        
          null
        
        
          )

                {

                    
        
        
          var
        
         headerRow = sheet.GetRow(
        
          0
        
        
          );

                    DataTable dt 
        
        = 
        
          new
        
        
           DataTable();

                    
        
        
          int
        
         columnCount =
        
           headerRow.Cells.Count;

                    
        
        
          for
        
         (
        
          int
        
         i = 
        
          0
        
        ; i < columnCount; i++
        
          )

                    {

                        dt.Columns.Add(
        
        
          "
        
        
          col_
        
        
          "
        
         +
        
           i.ToString());

                    }

                    
        
        
          var
        
         row =
        
           sheet.GetRowEnumerator();

                    
        
        
          while
        
        
           (row.MoveNext())

                    {

                        
        
        
          var
        
         dtRow =
        
           dt.NewRow();

                        
        
        
          var
        
         excelRow = row.Current 
        
          as
        
        
           IRow;

                        
        
        
          for
        
         (
        
          int
        
         i = 
        
          0
        
        ; i < columnCount; i++
        
          )

                        {

                            
        
        
          var
        
         cell =
        
           excelRow.GetCell(i);



                            
        
        
          if
        
         (cell != 
        
          null
        
        
          )

                            {

                                dtRow[i] 
        
        =
        
           GetValue(cell);

                            }

                        }

                        dt.Rows.Add(dtRow);

                    }

                    
        
        
          return
        
        
           dt;

                }

            }



            
        
        
          return
        
        
          null
        
        
          ;

        }





        
        
        
          private
        
        
          object
        
        
           GetValue(ICell cell)

        {

            
        
        
          object
        
         value = 
        
          null
        
        
          ;

            
        
        
          switch
        
        
           (cell.CellType)

            {

                
        
        
          case
        
        
           CellType.BLANK:

                    
        
        
          break
        
        
          ;

                
        
        
          case
        
        
           CellType.BOOLEAN:

                    value 
        
        = cell.BooleanCellValue ? 
        
          "
        
        
          1
        
        
          "
        
         : 
        
          "
        
        
          0
        
        
          "
        
        ; 
        
          break
        
        
          ;

                
        
        
          case
        
        
           CellType.ERROR:

                    value 
        
        = cell.ErrorCellValue; 
        
          break
        
        
          ;

                
        
        
          case
        
        
           CellType.FORMULA:

                    value 
        
        = 
        
          "
        
        
          =
        
        
          "
        
         + cell.CellFormula; 
        
          break
        
        
          ;

                
        
        
          case
        
        
           CellType.NUMERIC:

                    value 
        
        = cell.NumericCellValue.ToString(); 
        
          break
        
        
          ;

                
        
        
          case
        
        
           CellType.STRING:

                    value 
        
        = cell.StringCellValue; 
        
          break
        
        
          ;

                
        
        
          case
        
        
           CellType.Unknown:

                    
        
        
          break
        
        
          ;

            }

            
        
        
          return
        
        
           value;

        }



    }
        
      
View Code

这个需要下载NPOI 2.01版本 

下载地址 http://npoi.codeplex.com/releases/view/92382

接着实现2003版本

        
          public
        
        
          class
        
        
           XlsTransferData : ExcelTransferData

    {

        
        
        
          public
        
        
          override
        
        
           Stream GetStream(DataTable table)

        {

            
        
        
          base
        
        ._workBook = 
        
          new
        
        
           HSSFWorkbook();

            
        
        
          return
        
        
          base
        
        
          .GetStream(table);

        }



        
        
        
          public
        
        
          override
        
        
           DataTable GetData(Stream stream)

        {

            
        
        
          base
        
        ._workBook = 
        
          new
        
        
           HSSFWorkbook(stream);

            
        
        
          return
        
        
          base
        
        
          .GetData(stream);

        }

    }
        
      
View Code

接着实现2007版本

        
          public
        
        
          class
        
        
           XlsxTransferData : ExcelTransferData

    {



        
        
        
          public
        
        
          override
        
        
           Stream GetStream(DataTable table)

        {

            
        
        
          base
        
        ._workBook = 
        
          new
        
        
           XSSFWorkbook();

            
        
        
          return
        
        
          base
        
        
          .GetStream(table);

        }



        
        
        
          public
        
        
          override
        
        
           DataTable GetData(Stream stream)

        {

            
        
        
          base
        
        ._workBook = 
        
          new
        
        
           XSSFWorkbook(stream);

            
        
        
          return
        
        
          base
        
        
          .GetData(stream);

        }

    }
        
      
View Code

然后定义一个枚举实现一个简单的工厂

        
          public
        
        
          enum
        
        
           DataFileType

    {

        CSV,

        XLS,

        XLSX

    }



    
        
        
          public
        
        
          class
        
        
           TransferDataFactory

    {

        
        
        
          public
        
        
          static
        
         ITransferData GetUtil(
        
          string
        
        
           fileName)

        {

            
        
        
          var
        
         array = fileName.Split(
        
          '
        
        
          .
        
        
          '
        
        
          );

            
        
        
          var
        
         dataType = (DataFileType)Enum.Parse(
        
          typeof
        
        (DataFileType), array[array.Length - 
        
          1
        
        ], 
        
          true
        
        
          );

            
        
        
          return
        
        
           GetUtil(dataType);

        }



        
        
        
          public
        
        
          static
        
        
           ITransferData GetUtil(DataFileType dataType)

        {

            
        
        
          switch
        
        
           (dataType)

            {

                
        
        
          case
        
         DataFileType.CSV: 
        
          return
        
        
          new
        
        
           CsvTransferData();

                
        
        
          case
        
         DataFileType.XLS: 
        
          return
        
        
          new
        
        
           XlsTransferData();

                
        
        
          case
        
         DataFileType.XLSX: 
        
          return
        
        
          new
        
        
           XlsxTransferData();

                
        
        
          default
        
        : 
        
          return
        
        
          new
        
        
           CsvTransferData();

            }

        }



    }
        
      
View Code

客户端调用代码

        
          class
        
        
           Program

    {

        
        
        
          static
        
        
          void
        
         Main(
        
          string
        
        
          [] args)

        {

            
        
        
          var
        
         fileName = 
        
          @"
        
        
          C:/Users/ranrx/Desktop/data.xlsx
        
        
          "
        
        
          ;

            FileStream stream 
        
        = 
        
          new
        
        
           FileStream(fileName, FileMode.Open, FileAccess.Read);

            
        
        
          var
        
         util =
        
           TransferDataFactory.GetUtil(fileName);

            
        
        
          var
        
         data =
        
           util.GetData(stream);

            
        
        
          var
        
         mStream =
        
           util.GetStream(data);         



        }

    }
        
      
View Code

 

 

 

用NPOI实现导入导出csv、xls、xlsx数据功能


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论