NPOI导入导出Excel (2)
简单演示一下创建一个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)的更多相关文章
- NPOI导入导出EXCEL通用类,供参考,可直接使用在WinForm项目中
以下是NPOI导入导出EXCEL通用类,是在别人的代码上进行优化的,兼容xls与xlsx文件格式,供参考,可直接使用在WinForm项目中,由于XSSFWorkbook类型的Write方法限制,Wri ...
- NPOI导入导出Excel
.net mvc利用NPOI导入导出excel 注意:如何导出的提交方式ajax导出是失效的! 解决方案是:js处理l两个表单的提交 代码: 第一步. 在页面里面加入2个隐藏的iframe, 如下 ...
- .Net core NPOI导入导出Excel
最近在想.net core NPOI 导入导出Excel,一开始感觉挺简单的,后来真的遇到很多坑.所以还是写一篇博客让其他人少走一些弯路,也方便忘记了再重温一遍.好了,多的不说,直接开始吧. 在.Ne ...
- Npoi导入导出Excel操作
之前公司的一个物流商系统需要实现对订单的批量导入和导出,翻阅了一些资料,最后考虑使用NPOI实现这个需求. 在winform上面实现excel操作:http://www.cnblogs.com/Cal ...
- .net mvc利用NPOI导入导出excel
1.导出Excel :首先引用NPOI包(Action一定要用FileResult) /// <summary> /// 批量导出需要导出的列表 /// </summary> ...
- NPOI 导入导出excel 支持 03 07
因为微软的office成本太高了,所以开发项目的时候电脑上没安装office,而是安装了wps.但开发语言用的是C#,所以直接调用微软的office组件是很方便的,但一方面慢,一方面成本高,所以从网上 ...
- ASP.Net MVC利用NPOI导入导出Excel
因近期项目遇到所以记录一下: 首先导出Excel: 首先引用NPOI包 http://pan.baidu.com/s/1i3Fosux (Action一定要用FileResult) /// <s ...
- Excel操作--使用NPOI导入导出Excel为DataTable
1.ExcelHelper封装 namespace NPOI操作Excel { public class ExcelHelper { /// <summary> /// DataTable ...
- 使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中
using System; using System.Collections.Generic; using System.Text; using System.IO; using NPOI.SS.Us ...
- NPOI导入导出EXCEL通用类,可直接使用在WinForm项目中
由于XSSFWorkbook类型的Write方法限制,Write完成后就自动关闭流数据,所以无法很好的支持的Web模式,网上目前也未找到好的解决方案. 注意:若直接使用在WinForm项目中,必需先下 ...
随机推荐
- 【转】C++中的位拷贝与值拷贝
[转]http://blog.csdn.net/liam1122/article/details/1966617 为了便于说明我们以String类为例: 首先定义String类,而并不实现其成员函数. ...
- 你好,C++(18) 到底要不要买这个西瓜?4.1.6 操作符之间的优先顺序
4.1.6 操作符之间的优先顺序 在表达一些比较复杂的条件判断时,在同一个表达式中,有时可能会存在多个操作符.比如,我们在判断要不要买某个西瓜时,不仅要判断它的总价(单价8.2元/斤,一共10.3斤) ...
- 简单实用 “易忘” 的SQL 语句语法,新老皆宜
--创建数据库 create database 数据库名 on primary ( name='数据库名_data', filename='数据库储存路径', size=数据库初始大小(MB), ...
- 本地windows下PHP连接远程oracle遇到的诸多问题
任务目的:本地windows下PHP连接远程服务器下的oracle. 必须必须 确定服务器的数据库版本,如果本地的驱动和对方服务器版本不一致,会导致许多报错. 已知的oracle版本 分为 32位的 ...
- Visual Studio的广告剧
一个热衷于code的developer,一个热衷于developer的girl,他们将发生怎样的故事? 第一集:<想做你的Code> 第二集:<让爱延长> 第三集:<幸福 ...
- (转载)Python装饰器学习
转载出处:http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html 这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方 ...
- Holes in the text Add problem to Todo list Problem code: HOLES
import sys def count_holes(letter): hole_2 = ['A', 'D', 'O', 'P', 'Q', 'R'] if letter == 'B': return ...
- Kafka笔记--监控系统KafkaOffsetMonitor
KafkaOffsetMonitor下载链接: http://download.csdn.net/detail/changong28/7930337github官方:https://github.co ...
- 不知道算不算另类的ASP.NET MVC4 Ajax分页
以往用Ajax来实现无刷新分页,用户一按F5,页数就记不住了,点了一个链接也是同一个问题,再想回退回来,就回到第一页了.上次看到一篇文章,说到window.location.hash的用途,于是萌生了 ...
- Maven, IntellJ Idea 配置注意点
1. Maven要自己安装一个: 2. Maven设置中,settings.xml和repository地址都配置成自己: 3. Enable Auto import 4. 找不到jar文件时,自己的 ...