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

推荐订阅源

U
Unit 42
S
Security Affairs
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
Microsoft Security Blog
Microsoft Security Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
月光博客
月光博客
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
有赞技术团队
有赞技术团队
博客园 - 叶小钗
Hugging Face - Blog
Hugging Face - Blog
Microsoft Azure Blog
Microsoft Azure Blog
V
V2EX
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
腾讯CDC
C
Cisco Blogs
T
Tor Project blog
The Hacker News
The Hacker News
雷峰网
雷峰网
MyScale Blog
MyScale Blog
博客园 - 司徒正美
AWS News Blog
AWS News Blog
GbyAI
GbyAI
Y
Y Combinator Blog
D
DataBreaches.Net
Simon Willison's Weblog
Simon Willison's Weblog
S
Securelist
The GitHub Blog
The GitHub Blog
S
SegmentFault 最新的问题
T
Tenable Blog
L
LangChain Blog
M
MIT News - Artificial intelligence
N
Netflix TechBlog - Medium
The Cloudflare Blog
A
About on SuperTechFans
IT之家
IT之家
F
Fortinet All Blogs
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
NISL@THU
NISL@THU
爱范儿
爱范儿
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
W
WeLiveSecurity
A
Arctic Wolf
I
Intezer
Application and Cybersecurity Blog
Application and Cybersecurity Blog

博客园 - 独孤雁

在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;
                    }