简介:分析一个NOPI的使用手册,可以帮助我们在更好的运用NOPI操作office
有些客户导出或者导入时,对EXCEL的格式要求很高,就需要用到NOPI。代码样例:
Imports System Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.IO Imports NPOI.HSSF.UserModel Imports NPOI.HPSF Imports NPOI.POIFS.FileSystem Imports NPOI.SS.UserModel Partial Public Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) End Sub '点击按钮后输出文件数据流 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Dim filename As String = "test.xls" Response.ContentType = "application/vnd.ms-excel" Response.AddHeader("Content-Disposition", String.Format("attachment;filename={0}", filename)) Response.Clear() '调用初始化excel文件 InitializeWorkbook() '生成数据 GenerateData() '输出excel文件流 Response.BinaryWrite(WriteToStream().GetBuffer()) Response.[End]() End Sub '定义workbook Private hssfworkbook As HSSFWorkbook ' Private Function WriteToStream() As MemoryStream 'Write the stream data of workbook to the root directory Dim file As New MemoryStream() hssfworkbook.Write(file) Return file End Function '生成数据 Private Sub GenerateData() '生成一个sheet Dim sheet1 As ISheet = hssfworkbook.CreateSheet("Sheet1") '循环向sheet里面添加数据 sheet1.CreateRow(0).CreateCell(0).SetCellValue("This is a Sample") Dim x As Integer = 1 For i As Integer = 1 To 15 Dim row As IRow = sheet1.CreateRow(i) For j As Integer = 0 To 14 row.CreateCell(j).SetCellValue(System.Math.Max(System.Threading.Interlocked.Increment(x), x - 1)) Next Next End Sub '初始化一个Workbook(活动工作表) Private Sub InitializeWorkbook() hssfworkbook = New HSSFWorkbook() '''/create a entry of DocumentSummaryInformation Dim dsi As DocumentSummaryInformation = PropertySetFactory.CreateDocumentSummaryInformation() dsi.Company = "NPOI Team" hssfworkbook.DocumentSummaryInformation = dsi '''/create a entry of SummaryInformation Dim si As SummaryInformation = PropertySetFactory.CreateSummaryInformation() si.Subject = "NPOI SDK Example" hssfworkbook.SummaryInformation = si End Sub End Class
http://files.cnblogs.com/files/KingUp/npoi%E5%AE%9E%E4%BE%8B.rar