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. PHP学习笔记四【类型运算】

    <?php //类型运算符 class Dog { } class Cat { } $a=new Cat; var_dump($a instanceof Cat); //在实际开发中,判断某一个 ...

  2. OC学习中遇到的问题总结

    1.不要在初始化的时候用self.方法,因为在初始化的时候self.,此时系统还没有alloc开辟空间,这样做违背了系统流程. 2.善于运用宏定义来控制程序中的变量,这样程序可修改性高. 3.obje ...

  3. 智能电视TV开发---如何实现程序省电

    对于很多使用智能手机的用户来,很多抱怨手机耗电太快,很多人买手机的时候卖家都是推荐买两块电池,还有如果用户留心的话,在买手机的网页上,卖家会显示播放视频多长时间,听音乐多长时间,待机多长时间,不过看的 ...

  4. maven命令/依赖/聚合

    一,编写pom.xml   首先我们看一下pom.xml   <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns ...

  5. PendingIntent概述

    一.定义 PendingIntent表示待定.等待.即将发生的意思.Intent表示的是立刻发生. PendingIntent的主要方法: int requestCode:表示请求码,跟intent是 ...

  6. Moodle的qq登录版块的使用

    在这篇Moodle的qq登录(QQ登陆)版块的使用教程中,我们假定你已经有了一个有域名,外网能访问的Moodle2.4+网站,并且数据库使用的是mysql. 我们将提供Moodle的QQ登录版块的下载 ...

  7. c++中在一个类中定义另一个只有带参数构造函数的类的对象

    c++中在一个类中定义另一个只有带参数构造函数的类的对象,编译通不过 #include<iostream> using namespace std; class A { public:  ...

  8. python 数据类型之数值型

    1.在python在数值的类型大概包括int float complex decimal 1.int 在3.x中它不在有最大值的限制. 2.float它更加连近于C语言中的double 3.compl ...

  9. 如何让旧版IE浏览器认识HTML5元素

    <!--[if lt IE 9]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js" ...

  10. 多级列表——ExpandableListView

    ExpandableListView控件提供的是一个多级列表(一般是两级),我们先来看一下效果图,如图4.18所示为头部列表,单击其中的每一项下面会显示第二级列表,如图4.19所示. 从图4.18和图 ...