zoukankan      html  css  js  c++  java
  • NPOI 利用DataTable导出Excel 2003和2007

    NPOI插件官网:http://npoi.codeplex.com

    引用插件

    具体代码:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.IO;
     7 using NPOI.XSSF.UserModel;//导出2007
     8 using NPOI.HSSF.UserModel;//导出2003
     9 using NPOI.SS.UserModel;
    10 using NPOI.SS.Util;
    11 using NPOI.HPSF;
    12 using System.Data;
    13 using System.Web;
    14 
    15 namespace MES.Common
    16 {
    17     public class ExcelExportHelper
    18     {
    19         public static void Export2003(DataTable dt, string filePath)
    20         {
    21             NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
    22             NPOI.SS.UserModel.ISheet sheet = book.CreateSheet(dt.TableName);
    23 
    24             NPOI.SS.UserModel.IRow row = sheet.CreateRow(0);
    25             for (int i = 0; i < dt.Columns.Count; i++)
    26             {
    27                 row.CreateCell(i).SetCellValue(dt.Columns[i].ColumnName);
    28             }
    29             for (int i = 0; i < dt.Rows.Count; i++)
    30             {
    31                 NPOI.SS.UserModel.IRow row2 = sheet.CreateRow(i + 1);
    32                 for (int j = 0; j < dt.Columns.Count; j++)
    33                 {
    34                     row2.CreateCell(j).SetCellValue(Convert.ToString(dt.Rows[i][j]));
    35                 }
    36             }
    37             // 写入到客户端  
    38             using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
    39             {
    40                 book.Write(ms);
    41                 using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
    42                 {
    43                     byte[] data = ms.ToArray();
    44                     fs.Write(data, 0, data.Length);
    45                     fs.Flush();
    46                 }
    47                 book = null;
    48             }
    49 
    50         }
    51 
    52         public static void Export2007(DataTable dt, string filePath)
    53         {
    54             XSSFWorkbook book = new XSSFWorkbook();
    55             ISheet sheet1 = book.CreateSheet("data");
    56             IRow dataRow = sheet1.CreateRow(0);
    57             foreach (DataColumn column in dt.Columns)
    58             {
    59                 dataRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
    60             }
    61             //填充内容
    62             for (int i = 0; i < dt.Rows.Count; i++)
    63             {
    64                 dataRow = sheet1.CreateRow(i + 1);
    65                 for (int j = 0; j < dt.Columns.Count; j++)
    66                 {
    67                     dataRow.CreateCell(j).SetCellValue(dt.Rows[i][j].ToString());
    68                 }
    69             }
    70             // 写入到客户端  
    71             using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
    72             {
    73                 book.Write(ms);
    74                 using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
    75                 {
    76                     byte[] data = ms.ToArray();
    77                     fs.Write(data, 0, data.Length);
    78                     fs.Flush();
    79                 }
    80                 book = null;
    81             }
    82         }
    83     }
    84 }
    View Code
  • 相关阅读:
    开发软件设计模型 visual studio UML
    to debug asp.net mvc4
    BeeFramework
    .net entity framework 泛型 更新与增加记录
    javascript debut trick, using the throw to make a interrupt(breakpoint) in your program
    C# dynamic
    webapi
    C# async / await
    NYoj 613 免费馅饼
    洛谷P1056:排座椅(贪心)
  • 原文地址:https://www.cnblogs.com/LiGengMing/p/5137048.html
Copyright © 2011-2022 走看看