惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
B
Blog
A
About on SuperTechFans
大猫的无限游戏
大猫的无限游戏
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
H
Help Net Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
IT之家
IT之家
D
Docker
Google DeepMind News
Google DeepMind News
罗磊的独立博客
T
The Blog of Author Tim Ferriss
aimingoo的专栏
aimingoo的专栏
博客园 - 叶小钗
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
D
DataBreaches.Net
博客园 - 司徒正美
Engineering at Meta
Engineering at Meta

博客园 - 独孤雁

在win2008中安装vs2005 【转】NPOI 单元格级别应用 [转]google hosts 2015 [转] C#反射设置属性值和获取属性值 [转]Visual Studio技巧之打造拥有自己标识的代码模板 【转】mysql如何跟踪执行的sql语句 [转]VS2005/2008过期之后简单实用的升级方法 【转】在web 项目使用了ReportViewer时出错 .NET中TextBox控件设置ReadOnly=true后台取不到值的解决方法 window.location 对象所包含的属性 【原】提交按钮被隐藏,回车一样提交表单 【转】Eclipse安装SVN插件 [转]listview学习 安卓App程序访问网络 需要配置权限(java.net.SocketException: Permission denied) 【转】Android模拟器怎么配置网络连通 【转】Adobe Dreamweaver CS5序列号 【转】HTML特殊符号对照表 【转】ScriptX打印问题 Android SDK Manager无法更新的解决
使用NPOI导出excel
独孤雁 · 2014-09-24 · via 博客园 - 独孤雁

NPOI下载地址http://npoi.codeplex.com/releases

从项目中引用NPOI.bll和NPOI.OOXML.bll

引用命名控件

using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;

using System.IO;

在页面(*.aspx)中使用

protected void Button1_Click(object sender, EventArgs e)
        {
            enumCj_State cjState = enumCj_State.待审核;

            tbCj_Sale_BaseInfo[] objList = cjManager.search_cj_sale_tongji(null, null, (int)cjState, -1, string.Empty);

            HSSFWorkbook workbook = new HSSFWorkbook();
            ISheet sheet = workbook.CreateSheet("销售当日台账");
            IRow headrow = sheet.CreateRow(0);//编写标题列
            headrow.CreateCell(0, CellType.String).SetCellValue("签约店");
            headrow.CreateCell(1, CellType.String).SetCellValue("业务编号");

            int intRolNum = 0;
            foreach (tbCj_Sale_BaseInfo objcj in objList)
            {
                IRow row = sheet.CreateRow(intRolNum + 1);
                //row.CreateCell(0, CellType.String).SetCellValue(((DateTime)objcj.CJ_DATE).ToShortDateString());
                row.CreateCell(1, CellType.String).SetCellValue(objcj.QY_DEPTNAME);
                row.CreateCell(2, CellType.String).SetCellValue(objcj.FK_FYCode);
                
                    intRolNum ++;
            }

            MemoryStream ms = new MemoryStream();
            workbook.Write(ms);

            // 設定強制下載標頭
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename=Download.xls"));
            // 輸出檔案
            Response.BinaryWrite(ms.ToArray());
            ms.Close();
            ms.Dispose();
            Response.End();
        }

在一般程序(*.ashx)中使用

case "excel":
                    {
                        context.Response.Clear();
                        context.Response.ClearContent();
                        context.Response.ClearHeaders();
                       

                        context.Response.ContentType = "application/x-excel";
                        string fileName = HttpUtility.UrlEncode("动态数据库.xls");
                        context.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
                        HSSFWorkbook workbook = new HSSFWorkbook();
                        HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet();
                        HSSFRow row = (HSSFRow)sheet.CreateRow(0);

                      

                        row.CreateCell(1, CellType.String).SetCellValue("Hello excel");

                  

                        MemoryStream ms = new MemoryStream();
                        workbook.Write(ms);

                        // 設定強制下載標頭

                        context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=Download.xls"));
                        // 輸出檔案
                        context.Response.BinaryWrite(ms.ToArray());
                        ms.Close();
                        ms.Dispose();
                        context.Response.End();

                        break;
                    }