NPOI.dll学习
NPOI 是 POI 项目的 .NET 版本。POI是一个开源的Java读写Excel、WORD等微软OLE2组件文档的项目。
简介 编辑
优势 编辑
构成 编辑
|
NPOI.POIFS
|
OLE2/ActiveX文档属性读写库
|
|
NPOI.DDF
|
Microsoft Office Drawing读写库
|
|
NPOI.HPSF
|
OLE2/ActiveX文档读写库
|
|
NPOI.HSSF
|
Microsoft Excel BIFF(Excel 97-2003)格式读写库
|
|
NPOI.SS
|
Excel公用接口及Excel公式计算引擎
|
|
NPOI.Util
|
基础类库,提供了很多实用功能,可用于其他读写文件格式项目的开发
|
| Assembly名称 | 模块/命名空间 | 说明 |
| NPOI.DLL |
NPOI.POIFS
|
OLE2/ActiveX文档属性读写库
|
| NPOI.DLL |
NPOI.DDF
|
微软Office Drawing读写库
|
| NPOI.DLL |
NPOI.HPSF
|
OLE2/ActiveX文档读写库
|
| NPOI.DLL |
NPOI.HSSF
|
微软Excel BIFF(Excel 97-2003, doc)格式读写库
|
| NPOI.DLL |
NPOI.SS
|
Excel公用接口及Excel公式计算引擎
|
| NPOI.DLL |
NPOI.Util
|
基础类库,提供了很多实用功能,可用于其他读写文件格式项目的开发
|
| NPOI.OOXML.DLL | NPOI.XSSF | Excel 2007(xlsx)格式读写库 |
| NPOI.OOXML.DLL | NPOI.XWPF | Word 2007(docx)格式读写库 |
| NPOI.OpenXml4Net.DLL | NPOI.OpenXml4Net | OpenXml底层zip包读写库 |
| NPOI.OpenXmlFormats.DLL | NPOI.OpenXmlFormats | 微软Office OpenXml对象关系库 |
项目近况: 编辑
VS2005/VS2008,基于.NET 2.0 (SP1)
VS2003,基于.NET 1.1 (仅更新到1.2.1版本,之后不再支持.NET 1.1)
medium trust environment in ASP.NET
Npoi 简介
1.整个Excel表格叫做工作表:WorkBook(工作薄),包含的叫页(工作表):Sheet;行:Row;单元格Cell。
2.Npoi 下载地址:http://npoi.codeplex.com/releases/view/38113
3.Npoi 学习系列教程推荐:http://www.cnblogs.com/tonyqus/archive/2009/04/12/1434209.html
4.忘了告诉大家npoi是做什么的了,npoi 能够读写几乎所有的Office 97-2003文件格式,至少能够支持Word, PowerPoint, Excel, Visio的格式。
使用Npoi创建一个简单的xls文件

//创建xls文件
private void button1_Click(object sender, EventArgs e)
{
//创建工作薄
HSSFWorkbook wk = new HSSFWorkbook();
//创建一个名称为mySheet的表
ISheet tb = wk.CreateSheet("mySheet");
//创建一行,此行为第二行
IRow row = tb.CreateRow(1);
for (int i = 0; i < 20; i++)
{
ICell cell = row.CreateCell(i); //在第二行中创建单元格
cell.SetCellValue(i);//循环往第二行的单元格中添加数据
}
using (FileStream fs = File.OpenWrite(@"c:/myxls.xls")) //打开一个xls文件,如果没有则自行创建,如果存在myxls.xls文件则在创建是不要打开该文件!
{
wk.Write(fs); //向打开的这个xls文件中写入mySheet表并保存。
MessageBox.Show("提示:创建成功!");
}
}

使用Npoi读取一个简单的xls文件

//读取xls文件
private void button2_Click(object sender, EventArgs e)
{ StringBuilder sbr = new StringBuilder();
using (FileStream fs = File.OpenRead(@"c:/myxls.xls")) //打开myxls.xls文件
{
HSSFWorkbook wk = new HSSFWorkbook(fs); //把xls文件中的数据写入wk中
for (int i = 0; i < wk.NumberOfSheets; i++) //NumberOfSheets是myxls.xls中总共的表数
{
ISheet sheet = wk.GetSheetAt(i); //读取当前表数据
for (int j = 0; j <= sheet.LastRowNum; j++) //LastRowNum 是当前表的总行数
{
IRow row = sheet.GetRow(j); //读取当前行数据
if (row != null)
{
sbr.Append("-------------------------------------\r\n"); //读取行与行之间的提示界限
for (int k = 0; k <= row.LastCellNum; k++) //LastCellNum 是当前行的总列数
{
ICell cell = row.GetCell(k); //当前表格
if (cell != null)
{
sbr.Append(cell.ToString()); //获取表格中的数据并转换为字符串类型
}
}
}
}
}
}
sbr.ToString();
using (StreamWriter wr = new StreamWriter(new FileStream(@"c:/myText.txt", FileMode.Append))) //把读取xls文件的数据写入myText.txt文件中
{
wr.Write(sbr.ToString());
wr.Flush();
} }

使用Npoi创建一个常用的xls文件

//创建一个常用的xls文件
private void button3_Click(object sender, EventArgs e)
{
IWorkbook wb = new HSSFWorkbook();
//创建表
ISheet sh = wb.CreateSheet("zhiyuan");
//设置单元的宽度
sh.SetColumnWidth(0, 15 * 256);
sh.SetColumnWidth(1, 35 * 256);
sh.SetColumnWidth(2, 15 * 256);
sh.SetColumnWidth(3, 10 * 256);
int i = 0;
#region 练习合并单元格
sh.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 3));
//CellRangeAddress()该方法的参数次序是:开始行号,结束行号,开始列号,结束列号。 IRow row0 = sh.CreateRow(0);
row0.Height = 20 * 20;
ICell icell1top0 = row0.CreateCell(0);
icell1top0.CellStyle = Getcellstyle(wb, stylexls.头);
icell1top0.SetCellValue("标题合并单元");
#endregion
i++;
#region 设置表头
IRow row1 = sh.CreateRow(1);
row1.Height = 20 * 20; ICell icell1top = row1.CreateCell(0);
icell1top.CellStyle = Getcellstyle(wb, stylexls.头);
icell1top.SetCellValue("网站名"); ICell icell2top = row1.CreateCell(1);
icell2top.CellStyle = Getcellstyle(wb, stylexls.头);
icell2top.SetCellValue("网址"); ICell icell3top = row1.CreateCell(2);
icell3top.CellStyle = Getcellstyle(wb, stylexls.头);
icell3top.SetCellValue("百度快照"); ICell icell4top = row1.CreateCell(3);
icell4top.CellStyle = Getcellstyle(wb, stylexls.头);
icell4top.SetCellValue("百度收录");
#endregion using(FileStream stm=File.OpenWrite(@"c:/myMergeCell.xls"))
{
wb.Write(stm);
MessageBox.Show("提示:创建成功!");
}
} #region 定义单元格常用到样式的枚举
public enum stylexls
{
头,
url,
时间,
数字,
钱,
百分比,
中文大写,
科学计数法,
默认
}
#endregion #region 定义单元格常用到样式
static ICellStyle Getcellstyle(IWorkbook wb, stylexls str)
{
ICellStyle cellStyle = wb.CreateCellStyle(); //定义几种字体
//也可以一种字体,写一些公共属性,然后在下面需要时加特殊的
IFont font12 = wb.CreateFont();
font12.FontHeightInPoints = 10;
font12.FontName = "微软雅黑"; IFont font = wb.CreateFont();
font.FontName = "微软雅黑";
//font.Underline = 1;下划线 IFont fontcolorblue = wb.CreateFont();
fontcolorblue.Color = HSSFColor.OLIVE_GREEN.BLUE.index;
fontcolorblue.IsItalic = true;//下划线
fontcolorblue.FontName = "微软雅黑"; //边框
cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.DOTTED;
cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.HAIR;
cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.HAIR;
cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.DOTTED;
//边框颜色
cellStyle.BottomBorderColor = HSSFColor.OLIVE_GREEN.BLUE.index;
cellStyle.TopBorderColor = HSSFColor.OLIVE_GREEN.BLUE.index; //背景图形,我没有用到过。感觉很丑
//cellStyle.FillBackgroundColor = HSSFColor.OLIVE_GREEN.BLUE.index;
//cellStyle.FillForegroundColor = HSSFColor.OLIVE_GREEN.BLUE.index;
cellStyle.FillForegroundColor = HSSFColor.WHITE.index;
// cellStyle.FillPattern = FillPatternType.NO_FILL;
cellStyle.FillBackgroundColor = HSSFColor.BLUE.index; //水平对齐
cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.LEFT; //垂直对齐
cellStyle.VerticalAlignment = VerticalAlignment.CENTER; //自动换行
cellStyle.WrapText = true; //缩进;当设置为1时,前面留的空白太大了。希旺官网改进。或者是我设置的不对
cellStyle.Indention = 0; //上面基本都是设共公的设置
//下面列出了常用的字段类型
switch (str)
{
case stylexls.头:
// cellStyle.FillPattern = FillPatternType.LEAST_DOTS;
cellStyle.SetFont(font12);
break;
case stylexls.时间:
IDataFormat datastyle = wb.CreateDataFormat(); cellStyle.DataFormat = datastyle.GetFormat("yyyy/mm/dd");
cellStyle.SetFont(font);
break;
case stylexls.数字:
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00");
cellStyle.SetFont(font);
break;
case stylexls.钱:
IDataFormat format = wb.CreateDataFormat();
cellStyle.DataFormat = format.GetFormat("¥#,##0");
cellStyle.SetFont(font);
break;
case stylexls.url:
fontcolorblue.Underline = 1;
cellStyle.SetFont(fontcolorblue);
break;
case stylexls.百分比:
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
cellStyle.SetFont(font);
break;
case stylexls.中文大写:
IDataFormat format1 = wb.CreateDataFormat();
cellStyle.DataFormat = format1.GetFormat("[DbNum2][$-804]0");
cellStyle.SetFont(font);
break;
case stylexls.科学计数法:
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00E+00");
cellStyle.SetFont(font);
break;
case stylexls.默认:
cellStyle.SetFont(font);
break;
}
return cellStyle; }
#endregion

提示:1.以上使用npoi版本为1.2.5版本,版本目前属于最高版本,跟以前版本的使用是有些差别的。
2.使用以上代码,需要添加两个npoi的dll。Ionic.Zip.dll,NPOI.dll
错误提示:The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
错误原因:
- HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls
- XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx
解决方案:(引用npoi2.0)

ISheet sheet;
FileStream fs = null;
try
{
fs = new FileStream(txtPath.Text, FileMode.Open, FileAccess.Read);
HSSFWorkbook wk = new HSSFWorkbook(fs);
sheet = wk.GetSheet(sheetName);
}
catch
{
fs = new FileStream(txtPath.Text, FileMode.Open, FileAccess.Read);
XSSFWorkbook wk = new XSSFWorkbook(fs);
sheet = wk.GetSheet(sheetName);
}
finally
{
fs.Close();
fs.Dispose();
}
if (sheet.LastRowNum < 1)
{
MessageBox.Show("表内容不能为空");
return;
}
for (int j = 1; j <= sheet.LastRowNum; j++)

NPOI.dll学习的更多相关文章
- 使用npoi.dll导出数据到excel
.net数据导出excel数据有多种方法,最常用的就是使用office组件,但随之而来的问题也很棘手,又要调权限又要确定是否安装office很是麻烦,最近一个项目中也有数据导出功能,随使用excel模 ...
- NPOI.dll 用法。单元格,样式,字体,颜色,行高,宽度。读写excel
NPOI.dll 用法.单元格,样式,字体,颜色,行高,宽度.读写excel 转载:http://yuncode.net/code/c_531e679b3896495 view source prin ...
- NPOI.dll 在哪里?
一.问题 NPOI下载后找不到网上人家说的几个DLL https://bbs.csdn.net/topics/392510552 二.答案: 1.VS2015引用NPOI2.4.1和NuGet的安装方 ...
- 转载 NPOI.dll 用法。单元格,样式,字体,颜色,行高,宽度。读写excel
我用的版本是1.25的.每个版本用法有一点不同 using System; using System.Collections.Generic; using System.ComponentModel; ...
- dll学习
Dll:动态链接库 动态链接库(dll)是包含共享函数库的二进制文件,可以被多个应用程序同时使用.建立应用程序的可执行文件时,不必将DLL连接到应用程序中,而是在运行时动态装载DLL,装载时DLL被映 ...
- DLL学习笔记一(DLL导入导出)
创建DLL: 先声明导出函数:使用__declspec(dllexport) #include"DLLSample.h" #ifndef _DLL_SAMPLE_H #define ...
- Dll学习(二)__declspec用法详解
__declspec用于指定所给定类型的实例的与Microsoft相关的存储方式.其它的有关存储方式的修饰符如static与extern等是C和 C++语言的ANSI规范,而__declspec是一种 ...
- dll 学习(一)
DLL(Dynamic Link Library)的概念,你可以简单的把DLL看成一种仓库,它提供给你一些可以直接拿来用的变量.函数或类.在仓库的发展史上经历了"无库-静态链接库-动态链接库 ...
- Dll学习三_Dll 相互间以及主程序间的数据共享——测试未通过,应该用内存映射
测试环境:XP,DELPHI XE 验证通过结构:主程序+一个Dll窗体 共享方式原理:通过主程序与各Dll定义相同的参数结构体,由主程序实例化该结构体,对于各Dll间的共享,通过传主程序实例化的结构 ...
随机推荐
- Ibatis.net防Sql注入
sql注入是一个古老的话题了,但经常会被我们忽略.尤其是使用了ibatis.net之后. Ibatis.net框架对sql注入问题已经做了很好的防护,但经常由于开发人员使用不当,会造成sql的注入隐患 ...
- CAF(C++ actor framework)(序列化之结构体,任意嵌套STL)(一)
User-Defined Data Types in Messages(用户自定义类型)All user-defined types must be explicitly “announced” so ...
- windows github 搭建与使用
git/github使用以下是全部在命令行使用(windows/github) 注册账户以及创建仓库先在github建立账号和创建仓库,此时为空的仓库 配置git下载并安装 git windows版本 ...
- 数据库(MSSQLServer,Oracle,DB2,MySql)常见语句以及问题
创建数据库表 create table person ( FName varchar(), FAge int, FRemark varchar(), primary key(FName) ) 基本sq ...
- 大型网站用什么技术比较好,JSP,PHP,ASP.NET
大型网站,我建议要考虑的问题: 首先讨论一下大型网站需要注意和考虑的问题. 数据库海量数据处理:负载量不大的情况下select.delete和update是响应很迅速的,最多加几个索引就可以搞定,但千 ...
- ASP.NET工具
每个开发人员现在应该下载的十种必备工具 发布日期: 7/20/2004 | 更新日期: 7/20/2004 本文自发布以来已经增加了新信息. 请参阅下面的编辑更新. 本文讨论: • 用于编写单元测试的 ...
- 1062 Talent and Virtue (25)
/* L (>=60), the lower bound of the qualified grades -- that is, only the ones whose grades of ta ...
- (转)《深入理解java虚拟机》学习笔记5——Java Class类文件结构
Java语言从诞生之时就宣称一次编写,到处运行的跨平台特性,其实现原理是源码文件并没有直接编译成机器指令,而是编译成Java虚拟机可以识别和运行的字节码文件(Class类文件,*.class),字节码 ...
- 元类metaClass
metaClass 实现动态改变对象的能力,这点特别像python(metaClass),Python中类(不是元类)的概念借鉴于Smalltalk groovy demo: class Person ...
- git操作技巧(转载)
转载自:https://segmentfault.com/q/1010000000181403 git支持很多种工作流程,我们采用的一般是这样,远程创建一个主分支,本地每人创建功能分支,日常工作流程如 ...