1.下载NPOI.dll,并添加引用

2.ExcelHelper帮助类,以下为读写的参照方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
HSSFWorkbook hssfworkbook; 
#region  导入Excel 返回Table
public DataTable ImportExcelFile(string filePath)
{
    #region//初始化信息
    try
    {
        using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
        {
            hssfworkbook = new HSSFWorkbook(file);
        }
    }
    catch (Exception e)
    {
        throw e;
    }
    #endregion
 
    NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt(0);
    System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
    DataTable dt = new DataTable();
    for (int j = 0; j < (sheet.GetRow(0).LastCellNum); j++)
    {
        dt.Columns.Add(Convert.ToChar(((int)'A') + j).ToString());
    }
    while (rows.MoveNext())
    {
        HSSFRow row = (HSSFRow)rows.Current;
        DataRow dr = dt.NewRow();
        for (int i = 0; i < row.LastCellNum; i++)
        {
            NPOI.SS.UserModel.ICell cell = row.GetCell(i);
            if (cell == null)
            {
                dr[i] = null;
            }
            else
            {
                dr[i] = cell.ToString();
            }
        }
        dt.Rows.Add(dr);
    }
    return dt;
}
#endregion
 
#region 导出excel
//Datatable导出Excel
public   MemoryStream  GridToExcelByNPOI(DataTable dt)
{
    try
    {
        HSSFWorkbook workbook = new HSSFWorkbook();
        ISheet sheet = workbook.CreateSheet("Sheet1");
 
        ICellStyle HeadercellStyle = workbook.CreateCellStyle();
        HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
        HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
        HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
        HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
        HeadercellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
        //字体
        NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
        headerfont.Boldweight = (short)FontBoldWeight.Bold;
        HeadercellStyle.SetFont(headerfont);
 
 
        //用column name 作为列名
        int icolIndex = 0;
        IRow headerRow = sheet.CreateRow(0);
        foreach (DataColumn item in dt.Columns)
        {
            ICell cell = headerRow.CreateCell(icolIndex);
            cell.SetCellValue(item.ColumnName);
            cell.CellStyle = HeadercellStyle;
            icolIndex++;
        }
 
        ICellStyle cellStyle = workbook.CreateCellStyle();
 
        //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
        cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
        cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
        cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
        cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
        cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
 
 
        NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
        cellfont.Boldweight = (short)FontBoldWeight.Normal;
        cellStyle.SetFont(cellfont);
 
        //建立内容行
        int iRowIndex = 1;
        int iCellIndex = 0;
        foreach (DataRow Rowitem in dt.Rows)
        {
            IRow DataRow = sheet.CreateRow(iRowIndex);
            foreach (DataColumn Colitem in dt.Columns)
            {
 
                ICell cell = DataRow.CreateCell(iCellIndex);
                cell.SetCellValue(Rowitem[Colitem].ToString());
                cell.CellStyle = cellStyle;
                iCellIndex++;
            }
            iCellIndex = 0;
            iRowIndex++;
        }
 
        //自适应列宽度
        for (int i = 0; i < icolIndex; i++)
        {
            sheet.AutoSizeColumn(i);
        }
 
        //写Excel
        MemoryStream  ms = new MemoryStream ();
        workbook.Write(ms);
        ms.Flush();
        ms.Position = 0;
 
        return ms;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
#endregion

读写Excle,不用office环境的更多相关文章

  1. NPOI在无Office环境下,对Office文件的操作

    在做项目的时候,经常会遇到对 Office的操作,但有时候会没有Office环境,因此给大家介绍一个思路,在没有Office环境下,对Office的处理. NPOI,顾名思义,就是POI的.NET版本 ...

  2. 不用搭环境的10分钟AngularJS指令简易入门01(含例子)

    不用搭环境的10分钟AngularJS指令简易入门01(含例子) `#不用搭环境系列AngularJS教程01,前端新手也可以轻松入坑~阅读本文大概需要10分钟~` AngularJS的指令是一大特色 ...

  3. [SoapUI]怎样配置SoapUI运行的不同环境,并在Jenkins上面通过命令调用不用的环境

    配置SoapUI运行的不同环境 Groovy 脚本来控制environment 在Jenkins上面通过命令调用不用的环境 http://www.soapui.org/Test-Automation/ ...

  4. TestFlight的使用--再也不用担心环境打错了

    转赞请注明出处:http://www.cnblogs.com/zhanggui/p/7039651.html 一.前言 在iOS开发过程中,难免会遇到各种Bug.因此你会去解决所有的Bug,然后提交到 ...

  5. c#读excel 不用office组件

    某日笔者写了个导入导出excel的小程序,拿给别人用时,别人说程序有问题读不了. 究其原因发现原来是using Microsoft.Office.Interop.Excel;引用出错.发现他装的off ...

  6. 如何在任意文件下启动jupyter notebook,而不用担心环境配置问题

    网上看了很多帖子,说可以写一个bat文件,将bat文件放在你想启动jupyter notebook的地方.可是不行,不能解决我的问题!!!!!!!!!!! 网上是这样说的: ######这为引用### ...

  7. java 读写excle

    2014-04-16 20:38:20 java读写excel 晚上打算研究如何c来编写

  8. python读写excle

    我们可以通过python的一些模块进行excle中用例的读取,或者导出数据到excle 目录 1.安装模块 2.读excle 3.写excle 1.安装模块 python中有第三方模块可以进行excl ...

  9. java 读写 excle 完整版

    pom.xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</a ...

随机推荐

  1. 测试 windows live writer

    This is the first article written by the writer!   wenzhaoshanda

  2. (Oracle EBS)和标准用户有关的处理的API [Z]

    /* 和标准用户有关的处理的API. */ ---和用户处理有关的API FND_USER_PKG ---和用户密码处理有关的API fnd_web_sec ---和用户职责处理有关的API FND_ ...

  3. jquery中attr()与prop()函数用法实例详解(附用法区别)

    本文实例讲述了jQuery中attr()与prop()函数用法.分享给大家供大家参考,具体如下: 一.jQuery的attr()方法 jquery中用attr()方法来获取和设置元素属性,attr是a ...

  4. Unix环境下PS1变量的设置

    我的ps1命令提示符: export PS1="\[\e[31;1m\]\u @ \[\e[34;1m\]\h \[\e[36;1m\]\w \[\e[33;1m\]\t $ \[\e[37 ...

  5. Memcache 在win7x64中安装配置

    Memcached从0.2.0开始,要求PHP版本大于等于5.2.0. 环境:phpstudy集成环境 目标:实现php用memcache 下载:memcache for win 64 http:// ...

  6. tp可用的超强第三方图表类库-JpGraph

    日常开发中经常需要做图表,比如线状图.饼状图.柱状图等等,用PHP作图时需要使用复杂抽象的画图函数,或者借助一些网上下载的花柱形图.饼形图的类来实现,没有一个统一的chart类来实现图表的快速开发,非 ...

  7. arrayAccess的接口使用

    <?php //get the methods instance of ArrayAccess //get the properties instance of ArrayAccess $ref ...

  8. python列表推导和字典推导

    代码如下: list = ['aaa','bbb','ccc','ddd'] dict = {key:value for value,key in enumerate(list)} print(dic ...

  9. ActionResult 之HttpGet HttpPost

    GET一般用于获取和查询数据. 当浏览器发送HTTP GET 请求的时候,会找到使用HttpGet限定的对应Action. POST 一般用于更新数据. 当Action上面没有限定的时候,浏览器发送任 ...

  10. git 之别名配置

    在git操作中有很多命令我们自己可以起别名,以提高操作效率. 1. 配置方式 1)项目级别的配置,仅对当前项目生效(将写入到.git/config文件中)    $ git config --glob ...