zoukankan      html  css  js  c++  java
  • NPOIHelper.cs (NPOI 2.1.1)

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Data;
      6 using System.IO;
      7 using NPOI.XSSF.UserModel;
      8 using NPOI.SS.UserModel;
      9 
     10 namespace NetLib
     11 {
     12     public static class NPOIHelper
     13     {
     14         public static void ExportToFile(DataSet dataSet, string fileFullPath)
     15         {
     16             List<DataTable> dts = new List<DataTable>();
     17             foreach (DataTable dt in dataSet.Tables) dts.Add(dt);
     18             ExportToFile(dts, fileFullPath);
     19         }
     20         public static void ExportToFile(DataTable dataTable, string fileFullPath)
     21         {
     22             List<DataTable> dts = new List<DataTable>();
     23             dts.Add(dataTable);
     24             ExportToFile(dts, fileFullPath);
     25         }
     26         public static void ExportToFile(IEnumerable<DataTable> dataTables, string fileFullPath)
     27         {
     28             IWorkbook workbook = new XSSFWorkbook();
     29             int i = 0;
     30             foreach(DataTable dt in dataTables)
     31             {
     32                 string sheetName = string.IsNullOrEmpty(dt.TableName)
     33                     ? "Sheet " + (++i).ToString()
     34                     : dt.TableName;
     35                 ISheet sheet = workbook.CreateSheet(sheetName);
     36 
     37                 IRow headerRow = sheet.CreateRow(0);
     38                 for (int j = 0; j < dt.Columns.Count; j++)
     39                 {
     40                     string columnName = string.IsNullOrEmpty(dt.Columns[j].ColumnName)
     41                         ? "Column " + j.ToString()
     42                         : dt.Columns[j].ColumnName;
     43                     headerRow.CreateCell(j).SetCellValue(columnName);
     44                 }
     45 
     46                 for (int a = 0; a < dt.Rows.Count; a++)
     47                 {
     48                     DataRow dr = dt.Rows[a];
     49                     IRow row = sheet.CreateRow(a + 1);
     50                     for (int b = 0; b < dt.Columns.Count; b++)
     51                     {
     52                         row.CreateCell(b).SetCellValue(dr[b] != DBNull.Value ? dr[b].ToString() : string.Empty);
     53                     }
     54                 }
     55             }
     56 
     57             using (FileStream fs = File.Create(fileFullPath))
     58             {
     59                 workbook.Write(fs);
     60             }
     61         }
     62 
     63         public static List<DataTable> GetDataTablesFrom(string xlsxFile)
     64         {
     65             if (!File.Exists(xlsxFile))
     66                 throw new FileNotFoundException("文件不存在");
     67 
     68             List<DataTable> result = new List<DataTable>();
     69             Stream stream = new MemoryStream(File.ReadAllBytes(xlsxFile));
     70             IWorkbook workbook = new XSSFWorkbook(stream);
     71             for (int i = 0; i < workbook.NumberOfSheets; i++)
     72             {
     73                 DataTable dt = new DataTable();
     74                 ISheet sheet = workbook.GetSheetAt(i);
     75                 IRow headerRow = sheet.GetRow(0);
     76 
     77                 int cellCount = headerRow.LastCellNum;
     78                 for (int j = headerRow.FirstCellNum; j < cellCount; j++)
     79                 {
     80                     DataColumn column = new DataColumn(headerRow.GetCell(j).StringCellValue);
     81                     dt.Columns.Add(column);
     82                 }
     83 
     84                 int rowCount = sheet.LastRowNum;
     85                 for (int a = (sheet.FirstRowNum + 1); a < rowCount; a++)
     86                 {
     87                     IRow row = sheet.GetRow(a);
     88                     if (row == null) continue;
     89 
     90                     DataRow dr = dt.NewRow();
     91                     for (int b = row.FirstCellNum; b < cellCount; b++)
     92                     {
     93                         if (row.GetCell(b) == null) continue;
     94                         dr[b] = row.GetCell(b).ToString();
     95                     }
     96 
     97                     dt.Rows.Add(dr);
     98                 }
     99                 result.Add(dt);
    100             }
    101             stream.Close();
    102 
    103             return result;
    104         }
    105     }
    106 }

    NPOI 项目: http://npoi.codeplex.com/ 

    本地下载:http://files.cnblogs.com/bruceleeliya/NPOI2.1.1.zip

  • 相关阅读:
    Jython:java调用python文件之第三方包路径问题
    待研究的技术第二版
    mysql主从数据库不同步的2种解决方法(转)
    mysql分表和表分区详解
    mysql-binlog日志恢复数据库
    mysql delete删除记录数据库空间不减少问题解决方法
    mySQL 增量备份方案(转)
    mysql 查看 删除 日志操作总结(包括单独和主从mysql)
    Memcache的部署和使用(转)
    Linux下memcache的安装和启动(转)
  • 原文地址:https://www.cnblogs.com/bruceleeliya/p/4089727.html
Copyright © 2011-2022 走看看