博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NPOI操作Excel导入导出
阅读量:5080 次
发布时间:2019-06-13

本文共 4484 字,大约阅读时间需要 14 分钟。

Made by Anby using System;using System.Collections.Generic;using System.Data;using System.IO;using System.Linq;using System.Web;using NPOI;using NPOI.HPSF;using NPOI.HSSF;using NPOI.HSSF.UserModel;using NPOI.POIFS;using NPOI.Util;using NPOI.SS.UserModel;public class DataTableRenderToExcel{    // // 讀取 Excel 資料流並轉換成 DataTable。    //DataTable table = DataTableRenderToExcel.RenderDataTableFromExcel(this.fuUpload.FileContent, 1, 0);    //this.gvExcel.DataSource = table;    //this.gvExcel.DataBind();\    DataTable table = new DataTable();      填充資料(由讀者自行撰寫)     產生 Excel 資料流。    //MemoryStream ms = DataTableRenderToExcel.RenderDataTableToExcel(table) as MemoryStream;     設定強制下載標頭。    //Response.AddHeader("Content-Disposition", string.Format("attachment; filename=Download.xls"));     輸出檔案。    //Response.BinaryWrite(ms.ToArray());    //ms.Close();    //ms.Dispose()    public static Stream RenderDataTableToExcel(DataTable SourceTable)    {        HSSFWorkbook workbook = new HSSFWorkbook();        MemoryStream ms = new MemoryStream();        ISheet sheet = workbook.CreateSheet();        IRow headerRow = sheet.CreateRow(0);                 // handling header.        foreach (DataColumn column in SourceTable.Columns)            headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);        // handling value.        int rowIndex = 1;        foreach (DataRow row in SourceTable.Rows)        {           IRow dataRow = sheet.CreateRow(rowIndex);            foreach (DataColumn column in SourceTable.Columns)            {                dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());            }            rowIndex++;        }        workbook.Write(ms);        ms.Flush();        ms.Position = 0;        sheet = null;        headerRow = null;        workbook = null;        return ms;    }    public static void RenderDataTableToExcel(DataTable SourceTable, string FileName)    {        MemoryStream ms = RenderDataTableToExcel(SourceTable) as MemoryStream;        FileStream fs = new FileStream(FileName, FileMode.Create, FileAccess.Write);        byte[] data = ms.ToArray();        fs.Write(data, 0, data.Length);        fs.Flush();        fs.Close();        data = null;        ms = null;        fs = null;    }    public static DataTable RenderDataTableFromExcel(Stream ExcelFileStream, string SheetName, int HeaderRowIndex)    {        HSSFWorkbook workbook = new HSSFWorkbook(ExcelFileStream);        ISheet sheet = workbook.GetSheet(SheetName);        DataTable table = new DataTable();        IRow headerRow = sheet.GetRow(HeaderRowIndex);        int cellCount = headerRow.LastCellNum;        for (int i = headerRow.FirstCellNum; i < cellCount; i++)        {            DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);            table.Columns.Add(column);        }        int rowCount = sheet.LastRowNum;        for (int i = (sheet.FirstRowNum + 1); i < sheet.LastRowNum; i++)        {            IRow row = sheet.GetRow(i);            DataRow dataRow = table.NewRow();            for (int j = row.FirstCellNum; j < cellCount; j++)                dataRow[j] = row.GetCell(j).ToString();        }        ExcelFileStream.Close();        workbook = null;        sheet = null;        return table;    }    public static DataTable RenderDataTableFromExcel(Stream ExcelFileStream, int SheetIndex, int HeaderRowIndex)    {        HSSFWorkbook workbook = new HSSFWorkbook(ExcelFileStream);        ISheet sheet = workbook.GetSheetAt(SheetIndex);        DataTable table = new DataTable();        IRow headerRow = sheet.GetRow(HeaderRowIndex);        int cellCount = headerRow.LastCellNum;        for (int i = headerRow.FirstCellNum; i < cellCount; i++)        {            DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);            table.Columns.Add(column);        }        int rowCount = sheet.LastRowNum;        for (int i = (sheet.FirstRowNum + 1); i < sheet.LastRowNum; i++)        {            IRow row = sheet.GetRow(i);            DataRow dataRow = table.NewRow();            for (int j = row.FirstCellNum; j < cellCount; j++)            {                if (row.GetCell(j) != null)                    dataRow[j] = row.GetCell(j).ToString();            }            table.Rows.Add(dataRow);        }        ExcelFileStream.Close();        workbook = null;        sheet = null;        return table;    }}

  

转载于:https://www.cnblogs.com/anbylau2130/archive/2013/05/09/3069921.html

你可能感兴趣的文章
与像素无关的dp单位与像素单位px之间的转换
查看>>
04.Spring Ioc 容器 - 刷新
查看>>
java8中接口default、static新特性,与抽象类区别
查看>>
ConcurrentHashMap
查看>>
函数节流-歪说js
查看>>
[NOIp 2015]子串
查看>>
windows php环境配置
查看>>
spring中@value注解需要注意
查看>>
Java中的自定义注解
查看>>
【题解】 Test 买水的ACX(套路)
查看>>
使用jackson来进行数组格式的json字符串转换成List。
查看>>
Mysql数据库概述
查看>>
BFC总结
查看>>
设计模式——动态代理前戏——何为静态代理
查看>>
Asp.net(C#) windows 服务{用于实现计划任务,事件监控等}
查看>>
java使用HttpServletRequest获取请求真实ip地址
查看>>
网络时钟服务器(网络授时服务器)在某机场空管系统中的应用
查看>>
Linux中终端和控制台区别
查看>>
『Python基础-4』字符串
查看>>
tp框架 使用ajax
查看>>