ASP.Net使用NPOI导出Excel
2014-01-08 15:39
645 查看
ASP.Net使用NPOI导出Excel 采用一般处理程序进行处理
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using System.Text;
namespace MySEO.ashx
{
/// <summary>
/// ExportExcel 的摘要说明
/// </summary>
public class ExportExcel : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
WriteExcel(context.Response);
}
private void WriteExcel(HttpResponse response)
{
response.Clear();
response.Buffer = true;
response.ContentType = "ms-excel";
//attachment;让浏览器弹出下载对话框保存返回报文
//filename=是默认文件名,如果文件名中有中文等需要使用UrlEncode编码
string encodeFileName = HttpUtility.UrlEncode("过滤词.xls");
response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
response.AddHeader("Content-Disposition",
string.Format("attachment;filename=\"{0}\"", encodeFileName));
//1.创建一个新的Workbook
IWorkbook wk = new HSSFWorkbook();
//2.创建一个工作表Sheet
ISheet sheet = wk.CreateSheet("我的工作表");
//设置表格格式
ICellStyle style1 = wk.CreateCellStyle();//样式
IFont font1 = wk.CreateFont();//字体
//font1.FontHeightInPoints = 16;//设置字体大小
font1.Boldweight = (short)FontBoldWeight.BOLD;
style1.FillForegroundColor = HSSFColor.GREEN.index;// 设置背景色
style1.FillPattern = FillPatternType.SOLID_FOREGROUND;
style1.SetFont(font1);//样式里的字体设置具体的字体样式
style1.Alignment = HorizontalAlignment.CENTER;//文字水平对齐方式
style1.VerticalAlignment = VerticalAlignment.CENTER;//文字垂直对齐方式
int _max = 300;
int maxLength = 0;
int curLength = 0;
int colounwidth = 0;
string value = "";
IRow rowHeader = sheet.CreateRow(0);
//每行生成20个单元格
for (int k = 0; k < 20; k++)
{
value = "表头" + k.ToString();
curLength = Encoding.Default.GetByteCount(value);
maxLength = (maxLength < curLength ? curLength : maxLength);
colounwidth = 256 * maxLength;
sheet.SetColumnWidth(k, colounwidth);
//控制列宽
ICell cell = rowHeader.CreateCell(k);
cell.CellStyle = style1;
//应用表头格式
cell.SetCellValue(value);
}
maxLength = 0;
curLength = 0;
colounwidth = 0;
//循环创建30000行
for (int i = 1; i < _max; i++)
{
IRow row = sheet.CreateRow(i);
//每行生成20个单元格
for (int j = 0; j < 20; j++)
{
if (j % 2 == 0)
{
value = "单元格" + j.ToString();
}
else
{
value = "我是加长的单元格" + j.ToString();
}
curLength = Encoding.Default.GetByteCount(value);
maxLength = (maxLength < curLength ? curLength : maxLength);
colounwidth = 256 * maxLength;
sheet.SetColumnWidth(j, colounwidth);
row.CreateCell(j).SetCellValue(value);
}
}
wk.Write(response.OutputStream); //使用输出流方式输出
response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using System.Text;
namespace MySEO.ashx
{
/// <summary>
/// ExportExcel 的摘要说明
/// </summary>
public class ExportExcel : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
WriteExcel(context.Response);
}
private void WriteExcel(HttpResponse response)
{
response.Clear();
response.Buffer = true;
response.ContentType = "ms-excel";
//attachment;让浏览器弹出下载对话框保存返回报文
//filename=是默认文件名,如果文件名中有中文等需要使用UrlEncode编码
string encodeFileName = HttpUtility.UrlEncode("过滤词.xls");
response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
response.AddHeader("Content-Disposition",
string.Format("attachment;filename=\"{0}\"", encodeFileName));
//1.创建一个新的Workbook
IWorkbook wk = new HSSFWorkbook();
//2.创建一个工作表Sheet
ISheet sheet = wk.CreateSheet("我的工作表");
//设置表格格式
ICellStyle style1 = wk.CreateCellStyle();//样式
IFont font1 = wk.CreateFont();//字体
//font1.FontHeightInPoints = 16;//设置字体大小
font1.Boldweight = (short)FontBoldWeight.BOLD;
style1.FillForegroundColor = HSSFColor.GREEN.index;// 设置背景色
style1.FillPattern = FillPatternType.SOLID_FOREGROUND;
style1.SetFont(font1);//样式里的字体设置具体的字体样式
style1.Alignment = HorizontalAlignment.CENTER;//文字水平对齐方式
style1.VerticalAlignment = VerticalAlignment.CENTER;//文字垂直对齐方式
int _max = 300;
int maxLength = 0;
int curLength = 0;
int colounwidth = 0;
string value = "";
IRow rowHeader = sheet.CreateRow(0);
//每行生成20个单元格
for (int k = 0; k < 20; k++)
{
value = "表头" + k.ToString();
curLength = Encoding.Default.GetByteCount(value);
maxLength = (maxLength < curLength ? curLength : maxLength);
colounwidth = 256 * maxLength;
sheet.SetColumnWidth(k, colounwidth);
//控制列宽
ICell cell = rowHeader.CreateCell(k);
cell.CellStyle = style1;
//应用表头格式
cell.SetCellValue(value);
}
maxLength = 0;
curLength = 0;
colounwidth = 0;
//循环创建30000行
for (int i = 1; i < _max; i++)
{
IRow row = sheet.CreateRow(i);
//每行生成20个单元格
for (int j = 0; j < 20; j++)
{
if (j % 2 == 0)
{
value = "单元格" + j.ToString();
}
else
{
value = "我是加长的单元格" + j.ToString();
}
curLength = Encoding.Default.GetByteCount(value);
maxLength = (maxLength < curLength ? curLength : maxLength);
colounwidth = 256 * maxLength;
sheet.SetColumnWidth(j, colounwidth);
row.CreateCell(j).SetCellValue(value);
}
}
wk.Write(response.OutputStream); //使用输出流方式输出
response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
相关文章推荐
- c# asp.net NPOI 使用本地模板导出excel
- ASP.NET 中 NPOI & LinqToSql 使用模板 导出 Excel
- asp.net使用npoi读取excel模板并导出下载详解
- ASP.NET使用NPOI类库导出Excel(收藏)
- Asp.Net 使用Npoi导出Excel
- ASP.NET使用NPOI类库导出Excel
- ASP.NET使用NPOI类库导出Excel
- ASP.NET- 使用NPOI导入导出标准Excel
- ASP.NET使用NPOI类库导出Excel
- asp.net怎样使用NPOI 导出到excel和从excel导入到数据库
- asp.net使用npoi读取excel模板并导出下载详解
- ASP.NET使用NPOI加载Excel模板并导出下载
- ASP.NET使用NPOI加载Excel模板并导出下载
- c#.net 使用NPOI导入导出标准Excel (asp.net winform csharp)
- asp.net 使用NPOI实现导出Excel功能
- ASP.NET使用NPOI类库导出Excel
- Asp.Net使用Npoi导入导出Excel的方法
- Asp.Net使用Npoi导入导出Excel的方法
- Asp.net中利用NPOI做Excel导出功能
- ASP.NET AJAX Advance Tips & Tricks (10) 解决使用AJAX Extender时的页面导出(Word/Excel)问题(Extender control 'XXX'