简单演示一下创建一个Workbook对象,添加一个工作表,在工作表中添加一行一列:

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.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MemoryStream ms = RenderToExcel(GetTable());
string fileName = @"c:\2.xls";
SaveToFile(ms, fileName);
Response.Write("成功");
}
public MemoryStream RenderToExcel(DataTable table)
{
MemoryStream ms = new MemoryStream();

using (table)
{
IWorkbook workbook = new HSSFWorkbook();

ISheet sheet = workbook.CreateSheet();

IRow headerRow = sheet.CreateRow(0);

// handling header.
foreach (DataColumn column in table.Columns)
headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);//If Caption not set, returns the ColumnName value

// handling value.
int rowIndex = 1;

foreach (DataRow row in table.Rows)
{
IRow dataRow = sheet.CreateRow(rowIndex);

foreach (DataColumn column in table.Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
}

rowIndex++;
}

workbook.Write(ms);
ms.Flush();
ms.Position = 0;
}

return ms;
}
private void SaveToFile(MemoryStream ms, string fileName)
{
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
byte[] data = ms.ToArray();

fs.Write(data, 0, data.Length);
fs.Flush();

data = null;
}
}
private DataTable GetTable()
{
SqlHelperXhf xhf = new SqlHelperXhf();
string sql = "select calling_no '电话号码',starttime '时间' ,hname '楼盘名称' from [fc_freetel] a where hname<>'无'and a.id = (select min(id) from fc_freetel where calling_no=a.calling_no) order by hname,starttime desc";

return xhf.ExecuteDataTable(sql);

}
private DataTable GetTable(int hid)
{
SqlHelperXhf xhf = new SqlHelperXhf();
string sql = "select calling_no '电话号码',starttime '时间' ,hname '楼盘名称' from [fc_freetel] a where hid=@hid and a.id = (select min(id) from fc_freetel where calling_no=a.calling_no) order by hname,starttime desc";
SqlParameter paramer = new SqlParameter("@hid", hid);
return xhf.ExecuteDataTable(sql,paramer);

}
private DataTable GetTable(int hid1,int hid2,int hid3)
{
SqlHelperXhf xhf = new SqlHelperXhf();
string sql = "select calling_no '电话号码',starttime '时间' ,hname '楼盘名称' from [fc_freetel] a where hid=@hid1 or hid=@hid2 or hid=@hid3 and a.id = (select min(id) from fc_freetel where calling_no=a.calling_no) order by hname,starttime desc";
SqlParameter [] paramers =
{
new SqlParameter("@hid1", hid1),
new SqlParameter("@hid2", hid2),
new SqlParameter("@hid3", hid3)
};
return xhf.ExecuteDataTable(sql, paramers);

}
}

简单演示一下创建一个Workbook对象,添加一个工作表,在工作表中添加一行一列:

using NPOI.HSSF.UserModel;

using NPOI.SS.UserModel;
 
public class NPOIWrite
{
    void CreateSheet()
    {
        IWorkbook workbook = new HSSFWorkbook();//创建Workbook对象
        ISheet sheet = workbook.CreateSheet("Sheet1");//创建工作表
        IRow row = sheet.CreateRow(0);//在工作表中添加一行
        ICell cell = row.CreateCell(0);//在行中添加一列
        cell.SetCellValue("test");//设置列的内容
    }
}
相应的读取代码:
using System.IO;

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
 
public class NPOIRead
{
    void GetSheet(Stream stream)
    {
        IWorkbook workbook = new HSSFWorkbook(stream);//从流内容创建Workbook对象
        ISheet sheet = workbook.GetSheetAt(0);//获取第一个工作表
        IRow row = sheet.GetRow(0);//获取工作表第一行
        ICell cell = row.GetCell(0);//获取行的第一列
        string value = cell.ToString();//获取列的值
    }
}
 
 

使用NPOI导出

从DataTable读取内容来创建Workbook对象:

public static MemoryStream RenderToExcel(DataTable table)

{
    MemoryStream ms = new MemoryStream();
 
    using (table)
    {
        using (IWorkbook workbook = new HSSFWorkbook())
        {
            using (ISheet sheet = workbook.CreateSheet())
            {
                IRow headerRow = sheet.CreateRow(0);
 
                // handling header.
                foreach (DataColumn column in table.Columns)
                    headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);//If Caption not set, returns the ColumnName value
 
                // handling value.
                int rowIndex = 1;
 
                foreach (DataRow row in table.Rows)
                {
                    IRow dataRow = sheet.CreateRow(rowIndex);
 
                    foreach (DataColumn column in table.Columns)
                    {
                        dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
                    }
 
                    rowIndex++;
                }
 
                workbook.Write(ms);
                ms.Flush();
                ms.Position = 0;
            }
        }
    }
    return ms;
}

NPOI导入导出Excel (2)的更多相关文章

  1. NPOI导入导出EXCEL通用类,供参考,可直接使用在WinForm项目中

    以下是NPOI导入导出EXCEL通用类,是在别人的代码上进行优化的,兼容xls与xlsx文件格式,供参考,可直接使用在WinForm项目中,由于XSSFWorkbook类型的Write方法限制,Wri ...

  2. NPOI导入导出Excel

    .net mvc利用NPOI导入导出excel 注意:如何导出的提交方式ajax导出是失效的! 解决方案是:js处理l两个表单的提交  代码:  第一步. 在页面里面加入2个隐藏的iframe, 如下 ...

  3. .Net core NPOI导入导出Excel

    最近在想.net core NPOI 导入导出Excel,一开始感觉挺简单的,后来真的遇到很多坑.所以还是写一篇博客让其他人少走一些弯路,也方便忘记了再重温一遍.好了,多的不说,直接开始吧. 在.Ne ...

  4. Npoi导入导出Excel操作

    之前公司的一个物流商系统需要实现对订单的批量导入和导出,翻阅了一些资料,最后考虑使用NPOI实现这个需求. 在winform上面实现excel操作:http://www.cnblogs.com/Cal ...

  5. .net mvc利用NPOI导入导出excel

    1.导出Excel :首先引用NPOI包(Action一定要用FileResult) /// <summary> /// 批量导出需要导出的列表 /// </summary> ...

  6. NPOI 导入导出excel 支持 03 07

    因为微软的office成本太高了,所以开发项目的时候电脑上没安装office,而是安装了wps.但开发语言用的是C#,所以直接调用微软的office组件是很方便的,但一方面慢,一方面成本高,所以从网上 ...

  7. ASP.Net MVC利用NPOI导入导出Excel

    因近期项目遇到所以记录一下: 首先导出Excel: 首先引用NPOI包 http://pan.baidu.com/s/1i3Fosux (Action一定要用FileResult) /// <s ...

  8. Excel操作--使用NPOI导入导出Excel为DataTable

    1.ExcelHelper封装 namespace NPOI操作Excel { public class ExcelHelper { /// <summary> /// DataTable ...

  9. 使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中

    using System; using System.Collections.Generic; using System.Text; using System.IO; using NPOI.SS.Us ...

  10. NPOI导入导出EXCEL通用类,可直接使用在WinForm项目中

    由于XSSFWorkbook类型的Write方法限制,Write完成后就自动关闭流数据,所以无法很好的支持的Web模式,网上目前也未找到好的解决方案. 注意:若直接使用在WinForm项目中,必需先下 ...

随机推荐

  1. ZOJ3557 How Many Sets II( Lucas定理)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud How Many Sets II Time Limit: 2 Seconds    ...

  2. jquery节点查询

    jQuery.parent(expr)           //找父元素 jQuery.parents(expr)          //找到所有祖先元素,不限于父元素 jQuery.children ...

  3. FileZilla Server下载以及安装使用

    新版本filezilla server已经不能在windows xp和windows20003下使用了 下面是可以在xp和2003下使用的最后版本下载地址 http://pan.baidu.com/s ...

  4. Gradient Descent 和 Stochastic Gradient Descent(随机梯度下降法)

    Gradient Descent(Batch Gradient)也就是梯度下降法是一种常用的的寻找局域最小值的方法.其主要思想就是计算当前位置的梯度,取梯度反方向并结合合适步长使其向最小值移动.通过柯 ...

  5. js 简单倒计时插件和使用方法

    // 倒计时插件 (function (){ function countdown(config){ var startDate = config.start ? new Date(config.st ...

  6. Pythonic到底是什么玩意儿?

    http://blog.csdn.net/gzlaiyonghao/article/details/2762251 作者:Martijn Faassen 译者:赖勇浩(http://blog.csdn ...

  7. 禁用与启用Button点击

    //启用查询按钮 btnFpSelect.setClickable(true); //禁用查询按钮 btnFpSelect.setClickable(false);

  8. AsyncCTP &IdentityModel

    AsyncCTP.0.3.nupkg http://pan.baidu.com/s/1kVhPaYj Microsoft.IdentityModel.6.1.7600.16394  http://pa ...

  9. 关于iostream的效率问题

    前言    经常有人说iostream的速度慢,IO流比stdio的慢多了.但是有人测试过的,iostream的速度是超过stdio的. 测试结果 /* C */#include <stdio. ...

  10. 教程:30分钟学会Adobe Premiere

    原文地址:http://tieba.baidu.com/p/2785313831 视频教程地址