利用NPOI 导出文件到Excel (导出合同)

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;


using VO;
using DAL;
using BLL;
using System.IO;
using System.Text;
using NPOI;
using NPOI.HPSF;
using NPOI.HSSF;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.POIFS;
using NPOI.Util;
using NPOI.POIFS.FileSystem;
using System.Collections.Generic;

public partial class HT_HTSKMessage : System.Web.UI.Page
{
    public static IList<Tb_HTMXInfo> list = new List<Tb_HTMXInfo>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            datebinss();
        }
    }
//生成Excel
    protected void btntoExcel_Click(object sender, EventArgs e)
    {
        try
        {
            Tb_HTInfo info = new Tb_HTInfo();
            info.Ht_id = Convert.ToInt32(lblhtid.Text);
            info = new Tb_HTHelper().FindSelf(info);
            
            string fileName = HttpUtility.UrlDecode(info.Ht_HTName+".xls");
            Response.ContentType = "application/x-excel";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls"); 
            
            HSSFWorkbook hssfWorkBook = new HSSFWorkbook();//1、创建一个文件对象
            HSSFSheet sheet1 = hssfWorkBook.CreateSheet("合同正文");//2、创建一个工作薄
            HSSFSheet sheet2 = hssfWorkBook.CreateSheet("合同附件");
            HSSFSheet sheet3 = hssfWorkBook.CreateSheet("sheet1");
            sheet1.SetColumnWidth(2, 26 * 256);//设置指定列的宽度
            sheet1.SetColumnWidth(4, 24 * 256);
            sheet1.SetColumnWidth(5, 15 * 256);
            HSSFRow row = sheet1.CreateRow(0);//建行
            HSSFCell cell = row.CreateCell(0);//建单元格
            cell.SetCellValue(info.Ht_HTName);//合同名称
            //单元格对齐方式
            HSSFCellStyle style = hssfWorkBook.CreateCellStyle();
            style.Alignment = HSSFCellStyle.ALIGN_CENTER;
            //文本样式
            HSSFFont font = hssfWorkBook.CreateFont();
            font.FontHeight = 18 * 18;
            style.SetFont(font);
            cell.CellStyle = style;
            //合并单元格
            sheet1.AddMergedRegion(new Region(0, 0, 0, 5));
            HSSFRow row1 = sheet1.CreateRow(1);
            HSSFCell cell2 = row1.CreateCell(4);
            cell2.SetCellValue("合同号:" + info.Ht_HTnumber);
            HSSFCellStyle style1 = hssfWorkBook.CreateCellStyle();
            HSSFFont font1 = hssfWorkBook.CreateFont();
            font1.Color = HSSFColor.RED.index;//设置单元格字体颜色
            font1.FontHeightInPoints = 12;//设置单元格字体大小
            style1.SetFont(font1);
            cell2.CellStyle = style1;
            sheet1.AddMergedRegion(new Region(1, 4, 1, 5));
            sheet1.CreateRow(2).CreateCell(0).SetCellValue("甲方:");
            sheet1.CreateRow(2).CreateCell(1).SetCellValue("名称:");
            sheet1.CreateRow(2).CreateCell(2).SetCellValue(info.Ht_MaiF1);
            sheet1.CreateRow(3).CreateCell(1).SetCellValue("地址:");
            sheet1.CreateRow(3).CreateCell(2).SetCellValue("");
            sheet1.AddMergedRegion(new Region(3, 2, 3, 4));
            sheet1.CreateRow(4).CreateCell(1).SetCellValue("电话:");
            sheet1.CreateRow(4).CreateCell(2).SetCellValue(info.Ht_MF1Contact);
            sheet1.CreateRow(4).CreateCell(3).SetCellValue("传真:");
            sheet1.CreateRow(4).CreateCell(4).SetCellValue("");
            sheet1.CreateRow(5).CreateCell(0).SetCellValue("乙方:");
            sheet1.CreateRow(5).CreateCell(1).SetCellValue("名称:");
            sheet1.CreateRow(5).CreateCell(2).SetCellValue(info.Ht_MaiF2);
            sheet1.CreateRow(6).CreateCell(1).SetCellValue("地址:");
            sheet1.CreateRow(6).CreateCell(2).SetCellValue("");
            sheet1.AddMergedRegion(new Region(6, 2, 6, 3));
            sheet1.CreateRow(7).CreateCell(1).SetCellValue("电话:");
            sheet1.CreateRow(7).CreateCell(2).SetCellValue(info.Ht_MF2Contact);
            sheet1.CreateRow(7).CreateCell(3).SetCellValue("传真:");
            sheet1.CreateRow(7).CreateCell(4).SetCellValue("");
            sheet1.CreateRow(8).CreateCell(1).SetCellValue("开户行:");
            sheet1.CreateRow(8).CreateCell(3).SetCellValue("账号:");
            #region
            sheet1.CreateRow(9).CreateCell(1).SetCellValue("甲乙协商一致,就甲方向乙方购买网络产品事宜达成如下协议:");
            sheet1.CreateRow(10).CreateCell(1).SetCellValue("一、产品名称、型号、数量、价格见《合同附件——购货清单》。");
            sheet1.CreateRow(11).CreateCell(1).SetCellValue("二、本合同总金额(大写) 人民币:");
            sheet1.CreateRow(11).CreateCell(3).SetCellValue(MoneyToString.GetCnString(info.Ht_HTmoney.ToString()));
            sheet1.AddMergedRegion(new Region(11, 1, 11, 2));
            sheet1.AddMergedRegion(new Region(11, 3, 11, 4));
            sheet1.CreateRow(12).CreateCell(1).SetCellValue("三、交货时间、地点、方式:甲乙双方初定交货时间为合同签订后三个工作日内,乙方在此日");
            sheet1.CreateRow(13).CreateCell(1).SetCellValue("期前将货物运送至甲方指定地(详见附件)。乙方应当在2个工作日内将实际交货时间通知甲");
            sheet1.CreateRow(14).CreateCell(1).SetCellValue("方,如实际交货时间与初定交货时间有差异的,以实际交货时间为准。");
            sheet1.CreateRow(15).CreateCell(1).SetCellValue("四、运输方式及费用负担:由乙方代办托运并承担相关运输费用");
            sheet1.CreateRow(16).CreateCell(1).SetCellValue("五、产品包装:以厂家包装为准;如需特殊包装见附件。");
            sheet1.CreateRow(17).CreateCell(1).SetCellValue("六、验收标准、方法及提出异议期限:");
            sheet1.CreateRow(18).CreateCell(1).SetCellValue("七、 质量异议:甲方对产品的质量异议应自收到产品之日起3天内书面提出,逾期视为无质量");
            sheet1.CreateRow(19).CreateCell(1).SetCellValue("异议。乙方对产品提供的保修服务内容见本合同第一条。");
            sheet1.CreateRow(20).CreateCell(1).SetCellValue("八、结算方式及期限:");
            sheet1.CreateRow(21).CreateCell(1).SetCellValue("  货到后30天内将全额货款以电汇方式付清。甲方付款前乙方需提供相应金额增值税发票。");
            sheet1.CreateRow(22).CreateCell(1).SetCellValue("九、发票形式:17%增值税专用发票。");
            sheet1.CreateRow(23).CreateCell(1).SetCellValue("十、产品的保修期及售后服务:");
            sheet1.CreateRow(24).CreateCell(1).SetCellValue("    1、 产品的售后质保服务由厂家负责。");
            sheet1.CreateRow(25).CreateCell(1).SetCellValue("    2、 出现的质量问题,乙方应积极协助联系厂家解决,具体事宜双方协商。");
            sheet1.CreateRow(26).CreateCell(1).SetCellValue("十一、保密条款:甲乙双方对本合同的价格有保密义务,未经同意不得向第三方公开,");
            sheet1.CreateRow(27).CreateCell(1).SetCellValue("否则视为违约");
            sheet1.CreateRow(28).CreateCell(1).SetCellValue("十二、违约责任:");
            sheet1.CreateRow(29).CreateCell(1).SetCellValue("  1、所提交的产品与合同约定不符的,乙方应承担相应赔偿责任。");
            sheet1.CreateRow(30).CreateCell(1).SetCellValue("  2、乙方应按时交货,甲方应如期付款。如逾期付款或交货,应按合同违约金额的5%支付");
            sheet1.CreateRow(31).CreateCell(1).SetCellValue("违约金,并按逾期金额的每天万分之三支付逾期利息。双方对违约责任的承担仅限于本合同。");
            sheet1.CreateRow(32).CreateCell(1).SetCellValue("    3、甲方拒绝收货的,甲方应赔偿因此而给乙方造成的直接损失。");
            sheet1.CreateRow(33).CreateCell(1).SetCellValue("十三、不可抗力:一方因不可抗力不能履行或不能完全履行合同时,应当及时告知对方事");
            sheet1.CreateRow(34).CreateCell(1).SetCellValue("由,并在合理期限内由当地政府部门提供相关证明,双方据实重新协商合同履行事项。");
            sheet1.CreateRow(35).CreateCell(1).SetCellValue("十四、纠纷解决:本合同在履行过程中发生争议,由当事人双方协商解决。协商不成,任何");
            sheet1.CreateRow(36).CreateCell(1).SetCellValue("一方有权向其所在地有管辖权的人民法院提起诉讼。");
            sheet1.CreateRow(37).CreateCell(1).SetCellValue("十五、 本合同经双方签名盖章后生效,一式二份,双方各执一份,传真件具同等法律效力。");
            sheet1.CreateRow(38).CreateCell(1).SetCellValue("甲方:");
            sheet1.CreateRow(38).CreateCell(2).SetCellValue(info.Ht_MaiF1);
            sheet1.CreateRow(38).CreateCell(3).SetCellValue("乙方:");
            sheet1.CreateRow(38).CreateCell(4).SetCellValue(info.Ht_MaiF2);
            sheet1.CreateRow(39).CreateCell(1).SetCellValue("");
            sheet1.CreateRow(40).CreateCell(1).SetCellValue("代表:");
            sheet1.CreateRow(40).CreateCell(2).SetCellValue(info.Ht_MaiF1Man);
            sheet1.CreateRow(40).CreateCell(3).SetCellValue("代表:");
            sheet1.CreateRow(40).CreateCell(4).SetCellValue(info.Ht_MaiF2Man);
            sheet1.CreateRow(41).CreateCell(1).SetCellValue("");
            sheet1.CreateRow(42).CreateCell(1).SetCellValue("日期:");
            sheet1.CreateRow(42).CreateCell(2).SetCellValue(info.Ht_QDTime.Year + "年" + info.Ht_QDTime.Month + "月" + info.Ht_QDTime.Day + "日");
            sheet1.CreateRow(42).CreateCell(3).SetCellValue("日期:");
            sheet1.CreateRow(42).CreateCell(4).SetCellValue(info.Ht_QDTime.Year + "年" + info.Ht_QDTime.Month + "月" + info.Ht_QDTime.Day + "日");
            #endregion

            sheet2.SetColumnWidth(0, 5 * 256);//设置指定列的宽度
            sheet2.SetColumnWidth(1, 13 * 256);//设置指定列的宽度
            sheet2.SetColumnWidth(2, 19 * 256);//设置指定列的宽度
            sheet2.SetColumnWidth(3, 12 * 256);//设置指定列的宽度
            sheet2.SetColumnWidth(4, 7 * 256);//设置指定列的宽度
            sheet2.SetColumnWidth(5, 10 * 256);//设置指定列的宽度
            sheet2.SetColumnWidth(6, 10 * 256);//设置指定列的宽度
            sheet2.DefaultRowHeight = 30 * 20;//设置默认行高
            HSSFRow s2_row = sheet2.CreateRow(0);//建行
            HSSFCell s2_cell = s2_row.CreateCell(0);//建单元格
            s2_cell.SetCellValue("合同附件1");//标题
            //单元格对齐方式
            HSSFCellStyle s2_style = hssfWorkBook.CreateCellStyle();
            s2_style.Alignment = HSSFCellStyle.ALIGN_CENTER;
            //文本样式
            HSSFFont s2_font = hssfWorkBook.CreateFont();
            s2_font.FontHeight = 18 * 18;
            s2_style.SetFont(font);
            s2_cell.CellStyle = s2_style;
            //合并单元格
            sheet2.AddMergedRegion(new Region(0, 0, 0, 7));
            sheet2.CreateRow(1).CreateCell(0).SetCellValue("一、购货清单");
            sheet2.CreateRow(1).CreateCell(5).SetCellValue("合同编号:" + info.Ht_HTnumber);
            sheet2.AddMergedRegion(new Region(1, 0, 1, 1));//行、列、行、列
            sheet2.AddMergedRegion(new Region(1, 4, 1, 7));
            sheet2.AddMergedRegion(new Region(2, 0, 2, 7));
            sheet2.CreateRow(3).CreateCell(0).SetCellValue("序");
            sheet2.CreateRow(3).CreateCell(1).SetCellValue("产品名称");
            sheet2.CreateRow(3).CreateCell(2).SetCellValue("产品型号");
            sheet2.CreateRow(3).CreateCell(3).SetCellValue("产品编码");
            sheet2.CreateRow(3).CreateCell(4).SetCellValue("数量");
            sheet2.CreateRow(3).CreateCell(5).SetCellValue("产品单价");
            sheet2.CreateRow(3).CreateCell(6).SetCellValue("小计");
            sheet2.CreateRow(3).CreateCell(7).SetCellValue("备注");
            int h = 4;//行
            int number = 1;//序号
            for (int i = 0; i < list.Count; i++)
            {
                sheet2.CreateRow(h).CreateCell(0).SetCellValue(number.ToString());//序
                sheet2.CreateRow(h).CreateCell(1).SetCellValue(list[i].Hm_name);//产品名称
                sheet2.CreateRow(h).CreateCell(2).SetCellValue(list[i].Hm_CpXHNumber);//产品编号
                sheet2.CreateRow(h).CreateCell(3).SetCellValue(list[i].Hm_CpBhNumber);//产品编号
                sheet2.CreateRow(h).CreateCell(4).SetCellValue(list[i].Hm_counts.ToString());//数量
                sheet2.CreateRow(h).CreateCell(5).SetCellValue(list[i].Hm_price.ToString());//单价
                sheet2.CreateRow(h).CreateCell(6).SetCellValue(list[i].Hm_money.ToString());//小计
                sheet2.CreateRow(h).CreateCell(7).SetCellValue(list[i].Hm_beiz);//备注
                h++;
                number++;
            }
            sheet2.CreateRow(h + 1).CreateCell(0).SetCellValue("合计");
            sheet2.CreateRow(h + 1).CreateCell(7).SetCellValue(info.Ht_HTmoney.ToString());
            sheet2.AddMergedRegion(new Region(h + 1, 0, h + 1, 7));
            sheet2.CreateRow(h + 2).CreateCell(0).SetCellValue("二、交货地址");
            sheet2.AddMergedRegion(new Region(h + 2, 0, h + 2, 7));

            sheet2.CreateRow(h + 3).CreateCell(0).SetCellValue("地址:");
            sheet2.AddMergedRegion(new Region(h + 3, 0, h + 3, 7));
            sheet2.CreateRow(h + 4).CreateCell(0).SetCellValue("联系人:");
            sheet2.AddMergedRegion(new Region(h + 4, 0, h + 4, 7));
            //FileStream file = new FileStream(@"C:/" + info.Ht_HTName + ".xls", FileMode.Append);
            //hssfWorkBook.Write(file);
            //file.Close();
            hssfWorkBook.Write(Response.OutputStream);
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "key_2", "<script>alert('文件保存在c:/!');</script>");
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "key_1", "<script>alert('生成失败!');</script>");
        }
    }
}

留个笔记备忘~

找到更合理的方法再更新~~

哈哈,强大的NPOI

原文地址:https://www.cnblogs.com/huanhuan86/p/3007018.html