NPOI 生成Excel 的——常用
来源:互联网 发布:关口知宏评价中国 编辑:程序博客网 时间:2024/05/16 02:44
把自己常用的整理了出来,(*^__^*) 嘻嘻……
有关文件输出文件名乱码的问题
有设置Excel中的一些样式问题
有直接保存Excel的
更多NPOI的实例说明:http://www.cnblogs.com/tonyqus/archive/2009/04/12/1434209.html 这个可是别个大虾滴。
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.IO;
- using NPOI.SS.UserModel;
- using NPOI.HSSF.UserModel;
- using NPOI.HSSF.Util;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- DownLoadExcel();
- }
- /// <summary>
- /// 下载Excel
- /// </summary>
- public void DownLoadExcel()
- {
- /*
- * ①:输出文档
- */
- string fileName = DateTime.Now.ToString("yyyyMMdd") +"测试.xls";
- string UserAgent = Request.ServerVariables["http_user_agent"].ToLower();
- // Firfox和IE下输出中文名显示正常
- if (UserAgent.IndexOf("firefox") == -1)
- {
- fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
- }
- Response.ContentType = "application/vnd.ms-excel;charset=UTF-8";
- Response.AddHeader("Content-Disposition",string.Format("attachment;filename={0}", fileName));
- Response.Clear();
- //写入内容到Excel
- HSSFWorkbook hssfworkbook = writeToExcel();
- //将Excel内容写入到流中
- MemoryStream file = new MemoryStream();
- hssfworkbook.Write(file);
- //输出
- Response.BinaryWrite(file.GetBuffer());
- Response.End();
- /*
- * ②:将文档保存到指定路径
- */
- string destFileName = @"D:\test.xls";
- HSSFWorkbook hssfworkbook2 = writeToExcel();
- MemoryStream msfile = new MemoryStream();
- hssfworkbook.Write(msfile);
- System.IO.File.WriteAllBytes(destFileName, msfile.ToArray());
- }
- /// <summary>
- /// 写入内容到Excel中
- /// </summary>
- /// <returns></returns>
- private HSSFWorkbook writeToExcel()
- {
- //string template = "模板路径.xls";
- //FileStream file = new FileStream(template, FileMode.Open, FileAccess.Read);
- //HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);// 创建对象
- //HSSFSheet excelSheet = (HSSFSheet)hssfworkbook.GetSheetAt(0);// 获得sheet
- HSSFWorkbook hssfworkbook = new HSSFWorkbook();
- HSSFSheet excelSheet = (HSSFSheet)hssfworkbook.CreateSheet("sheet1");
- Row row0 = excelSheet.CreateRow(0);
- Cell cell0 = CreateCell(0, row0);
- cell0.SetCellValue("NUM");
- cell0.CellStyle = GetCellStyle(hssfworkbook, CellBorderType.THIN, CellBorderType.THIN, CellBorderType.THIN, CellBorderType.THIN, HSSFColor.LIGHT_YELLOW.index,"#,##0");
- for (int i = 100, j = 1; i < 10000; i++, j++)
- {
- Row row = CreateRow(j, excelSheet);
- Cell cell = CreateCell(0, row);
- cell.SetCellValue(i);
- cell.CellStyle = GetCellStyle(hssfworkbook, CellBorderType.THIN, CellBorderType.THIN, CellBorderType.THIN, CellBorderType.THIN, HSSFColor.LIGHT_GREEN.index,"#,##0");
- }
- return hssfworkbook;
- }
- /// <summary>
- /// 设置样式
- /// </summary>
- /// <param name="hssfworkbook"></param>
- /// <param name="borderLeft">左边框</param>
- /// <param name="borderBottom">下边框</param>
- /// <param name="borderRight">右边框</param>
- /// <param name="borderTop">上边框</param>
- /// <param name="fillforgeroundColor">背景填充色</param>
- /// <param name="dataFormat">数据格式</param>
- /// <returns></returns>
- private CellStyle GetCellStyle(HSSFWorkbook hssfworkbook
- , CellBorderType borderLeft, CellBorderType borderBottom, CellBorderType borderRight, CellBorderType borderTop
- , short fillforgeroundColor
- , string dataFormat)
- {
- CellStyle styleInfo = hssfworkbook.CreateCellStyle();
- styleInfo.BorderLeft = borderLeft;
- styleInfo.BorderBottom = borderBottom;
- styleInfo.BorderRight = borderRight;
- styleInfo.BorderTop = borderTop;
- styleInfo.Alignment = HorizontalAlignment.CENTER;
- styleInfo.VerticalAlignment = VerticalAlignment.CENTER;
- styleInfo.FillForegroundColor = fillforgeroundColor;//设置填充色
- styleInfo.FillPattern = FillPatternType.SOLID_FOREGROUND;//设置填充色的时候必须设置这个
- styleInfo.DataFormat = HSSFDataFormat.GetBuiltinFormat(dataFormat);
- // 当前日期格式的需要以下这样设置
- //HSSFDataFormat format = (HSSFDataFormat)hssfworkbook.CreateDataFormat();
- //styleInfo.DataFormat = format.GetFormat("yyyy年m月d日");
- return styleInfo;
- }
- /// <summary>
- /// 创建行对象
- /// </summary>
- /// <param name="rowID"></param>
- /// <param name="excelSheet"></param>
- /// <returns></returns>
- private Row CreateRow(int rowID, HSSFSheet excelSheet)
- {
- Row row = excelSheet.GetRow(rowID);
- if (row == null)
- {
- row = excelSheet.CreateRow(rowID);
- }
- return row;
- }
- /// <summary>
- /// 创建列对象
- /// </summary>
- /// <param name="rowID"></param>
- /// <param name="excelSheet"></param>
- /// <returns></returns>
- private Cell CreateCell(int cellID, Row row)
- {
- Cell cell = row.GetCell(cellID);
- if (cell == null)
- {
- cell = row.CreateCell(cellID);
- }
- return cell;
- }
- }
最近被Excel的給整疯了。
整理一下,希望可以给人帮助!
- NPOI 生成Excel 的——常用
- NPOI 生成Excel 的——常用
- NPOI 生成Excel 的——常用
- NPOI之Excel——简单的使用NPOI读取和生成Excel文件
- NPOI锦囊:如何用NPOI生成可下载的Excel
- NPOI生成Excel
- NPOI生成Excel
- NPOI根据Excel模板生成原生的Excel文件实例
- NPOI动态生成Excel下载
- NPOI简介(动态生成Excel)
- C#生成excel-NPOI篇
- C#使用NPOI生成Excel
- 简单的使用NPOI读取和生成Excel文件
- npoi常用方法,excel表的导入导出
- .NET——NPOI操作excel
- 使用NPOI生成Excel级联列表
- 使用NPOI生成Excel级联列表
- NPOI的EXCEL转DATATABLE
- SQL Server中的视图和查询语句的异同
- 有用的:nth-child秘方
- Oracle数据库监听配置
- poj1947(树形dp)
- Java 深拷贝
- NPOI 生成Excel 的——常用
- hdu_4342 History repeat itself (简单想法题)
- 最近半年计划
- 黑马程序员——内部类
- java正则表达式
- Android Intent Flag的介绍
- 图像平滑-邻域平均法
- 如何理解软件开发规范性与灵活性
- 使用Oracle SQLDeveloper连接数据库并创建用户