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项目中,必需先下 ...
随机推荐
- destoon系统商城加淘宝客按钮方法
destoon系统很多喜欢运营B2B的站长都在用,其中的商城模块常常被用来做淘宝客,其中的难点是如何把购买按钮做成淘宝客地址,这个问题的修改在论坛上被叫价50元,下面小编把这个实用的方法分享下,希望对 ...
- GO求平均值
package main import "fmt" func main(){ sum:=0.0 avg:=0.0 xs:=" switch len(xs){ : avg= ...
- epoll函数及三种I/O复用函数的对比
epoll函数 #include <sys/epoll.h>int epoll_create(int size)int epoll_ctl(int epfd, int op, int fd ...
- Javascript 原型注意事项
function abc() {} abc.prototype.xx = { name: "keatkeat" } var x = new abc(); x.xx.name = & ...
- 这样就算会了PHP么?-7
循环之类的例子 <script language="javascript"> function calculate(a, b) { return a * b; } do ...
- FJ省队集训DAY2 T1
思路:转换成n条三维空间的直线,求最大的集合使得两两有交点. 有两种情况:第一种是以某2条直线为平面,这时候只要统计这个平面上有几条斜率不同的直线就可以了 还有一种是全部交于同一点,这个也只要判断就可 ...
- Linux Shell逻辑运算符和表达式详解
Shell 逻辑运算符涉及以下几种类型,只要适当选择,可以解决我们很多复杂的判断,达到事半功倍效果. 一.逻辑判断1.关于文件与目录的逻辑判断-f 常用.判断『文件』是否为普通文件,比如: if [ ...
- js深入研究之Person类案例
<script type="text/javascript"> /* 定义一个Person类 */ function Person(name, age) { this. ...
- javascript笔记7之对象数组
/* var box = new Array(); //声明一个数组,空数组 alert(typeof box); //数组属于object类型 var box = new Array('李炎恢', ...
- c语言结构体数组定义的三种方式
struct dangdang { ]; ]; ]; int num; int bugnum; ]; ]; double RMB; int dangdang;//成员名可以和类名同名 }ddd[];/ ...