您的位置:首页 > 其它

NPOI使用记录

2015-11-01 13:33 211 查看
使用NPOI时,从官网下载的安装包,当时看里面有好几个DLL,觉得没啥用,就只导入了NPOI.dll

当使用的时候发现,还是需要全部导入进去,避免有一些东西引用不了,操作不了

其次,关于workbook的对象创建,如果是新版的,则用XSSF创建,若是xls的,则用HSSF来创建

另外还有一个情况,是NPOI的版本问题,版本不一样,IWorkbook的对象也不一样的,具体的忘记了。

另外,我使用的版本是NPOI 2.1.3

借此记录使用该动态库遇到的问题,避免下次再次遇到。

另外附上写的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;

public partial class ChildrenPageFolder_Test_Page_NPOI : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string fileName = @"F:\PGIS_CODE\PGIS_114\ChildrenPageFolder\Test_Page\abcdefg.xls";

FileStream fs = File.OpenRead(fileName);
// 此处可以判断文件的后缀名,判断调用不同的Workbook对象
IWorkbook workbook = new HSSFWorkbook(fs);
fs.Close();

ISheet sheet = workbook.GetSheetAt(0);
for (int i = 0; i <= sheet.LastRowNum; i++)
{
foreach (ICell cell in sheet.GetRow(i).Cells)
{
cell.SetCellType(CellType.String);
cell.SetCellValue("0");
}
}

// 将xls文件进行输出
MemoryStream ms = new MemoryStream();
workbook.Write(ms);
Context.Response.ClearContent();
Context.Response.ContentType = "application/octet-stream";
Context.Response.AddHeader("Content-Disposition", "attachment;filename=abcdefg.xls");
Context.Response.BinaryWrite(ms.ToArray());
Context.Response.Flush();

// 将xls文件保存到本地目录
//FileStream fs2 = File.Create(@"D:\123.xls");
//workbook.Write(fs2);
//fs2.Close();

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: