NPOI从数据库中导出Excel
2010-11-21 21:18
197 查看
using System;
using System.Collections.Generic;
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.DDF;
using System.Data;
using System.Data.SqlClient;
using 导出Excel.DataSet导出ExcelTableAdapters;
namespace 导出Excel
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string dataDir = AppDomain.CurrentDomain.BaseDirectory;
if (dataDir.EndsWith(@"/bin/Debug/")
|| dataDir.EndsWith(@"/bin/Release/"))
{
dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
}
int i=0;
context.Response.ContentType = "application/x-excel";
string filename = HttpUtility.UrlEncode("1.xls");
context.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
HSSFWorkbook book = new HSSFWorkbook();
HSSFSheet sheet = book.CreateSheet();
T_userTableAdapter adapter = new T_userTableAdapter();
导出Excel.DataSet导出Excel.T_userDataTable table = adapter.GetData();
HSSFRow row2 = sheet.CreateRow(i++);
row2.CreateCell(0, HSSFCell.CELL_TYPE_STRING).SetCellValue("编号");
row2.CreateCell(1, HSSFCell.CELL_TYPE_STRING).SetCellValue("姓名");
adapter.Connection.Open();
foreach (导出Excel.DataSet导出Excel.T_userRow row1 in table)
{
HSSFRow row = sheet.CreateRow(i++);
row.CreateCell(0, HSSFCell.CELL_TYPE_STRING).SetCellValue(row1.Id);
row.CreateCell(1, HSSFCell.CELL_TYPE_STRING).SetCellValue(row1.Name);
}
adapter.Connection.Close();
book.Write(context.Response.OutputStream);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
using System.Collections.Generic;
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.DDF;
using System.Data;
using System.Data.SqlClient;
using 导出Excel.DataSet导出ExcelTableAdapters;
namespace 导出Excel
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string dataDir = AppDomain.CurrentDomain.BaseDirectory;
if (dataDir.EndsWith(@"/bin/Debug/")
|| dataDir.EndsWith(@"/bin/Release/"))
{
dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
}
int i=0;
context.Response.ContentType = "application/x-excel";
string filename = HttpUtility.UrlEncode("1.xls");
context.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
HSSFWorkbook book = new HSSFWorkbook();
HSSFSheet sheet = book.CreateSheet();
T_userTableAdapter adapter = new T_userTableAdapter();
导出Excel.DataSet导出Excel.T_userDataTable table = adapter.GetData();
HSSFRow row2 = sheet.CreateRow(i++);
row2.CreateCell(0, HSSFCell.CELL_TYPE_STRING).SetCellValue("编号");
row2.CreateCell(1, HSSFCell.CELL_TYPE_STRING).SetCellValue("姓名");
adapter.Connection.Open();
foreach (导出Excel.DataSet导出Excel.T_userRow row1 in table)
{
HSSFRow row = sheet.CreateRow(i++);
row.CreateCell(0, HSSFCell.CELL_TYPE_STRING).SetCellValue(row1.Id);
row.CreateCell(1, HSSFCell.CELL_TYPE_STRING).SetCellValue(row1.Name);
}
adapter.Connection.Close();
book.Write(context.Response.OutputStream);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
相关文章推荐
- NPOI从数据库中导出数据到excel中
- NPOI从数据库中导出到Excel
- 如何使用NPOI 导出到excel和导入excel到数据库
- NPOI从数据库中调取数据直接导出到EXCEL中
- C# .Net :Excel NPOI导入导出操作教程之将Excel文件读取并写到数据库表,示例分享
- NPOI从数据库中导出数据到Excel
- NPOI从数据库中调取数据直接导出到EXCEL中
- asp.net怎样使用NPOI 导出到excel和从excel导入到数据库
- NPOI 操作数据库中数据的导入导出(Excel.xls文件) 和null数据的处理。
- C# 使用NPOI 导出Excel
- C# 使用NPOI 实现Excel的简单导入导出
- NPOI 导出Excel
- 从数据库导出word,excel
- 数据库导出Excel
- 如何快速导出数据库为EXCEL
- NPOI导出EXCEL报_服务器无法在发送 HTTP 标头之后追加标头
- Excel导入导出数据库小工具 附全部源代码(1月19日最新修改)
- 将excel中数据导入到sql数据库,,从数据库导出到excel中
- C#数据库数据导入导出系列之三 数据库导出到Excel下
- C#中数据库数据如何导出至Excel表格