Excle的导出

public void excleExport()
{
//动态创建一个表
HSSFWorkbook workbook = new HSSFWorkbook();
//创建一个表
HSSFSheet sheet = workbook.CreateSheet(“富婆通讯有限公司”) as HSSFSheet;

    #region   公司说明
    HSSFRow head = sheet.CreateRow(0) as HSSFRow;
    HSSFCell headceel = head.CreateCell(0) as HSSFCell;
    //设置行高
    head.Height = 255 * 3;
    //设置列宽
    for (int i = 0; i < 7; i++)
    {
        sheet.SetColumnWidth(i, 255 * 3 * 4);
    }
    //合并单元格

    sheet.AddMergedRegion(new NPOI.SS.Util.Region(0, 0, 0, 6));
    //设置样式
    HSSFCellStyle cellstyle = workbook.CreateCellStyle() as HSSFCellStyle;
    cellstyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER;
    cellstyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;
    //得到字体样式
    HSSFFont font = workbook.CreateFont() as HSSFFont;
    font.Boldweight = 600;
    font.FontHeight = 30 * 14;
    headceel.CellStyle = cellstyle;
    cellstyle.SetFont(font);


    headceel.SetCellValue("富婆通讯有限公司");
    #endregion
    //创建行
    HSSFRow row = sheet.CreateRow(1) as HSSFRow;

    //创建列

    row.CreateCell(0).SetCellValue("姓名");
    row.CreateCell(1).SetCellValue("年龄");
    row.CreateCell(2).SetCellValue("QQ");
    row.CreateCell(3).SetCellValue("微信");
    row.CreateCell(4).SetCellValue("电话");
    row.CreateCell(5).SetCellValue("地址");
    row.CreateCell(6).SetCellValue("资产");

    Random rand = new Random();
    string[] city = { "贵州", "天津", "北京", "上海", "深圳", "重庆", "广东" };
    string[] username = { "花花", "翠翠", "兰兰", "云云", "香香", "梦梦", "玲玲" };
    string[] usernameEnglish = { "huahua", "cuicui", "lanlan", "yunyun", "xiangxiang", "mengmeng", "linlin" };
    int j = 0;
    for (int i = 2; i < 9; i++)
    {

        HSSFRow row2 = sheet.CreateRow(i) as HSSFRow;
        row2.CreateCell(0).SetCellValue(username[j]);
        row2.CreateCell(1).SetCellValue(rand.Next(20, 50));
        row2.CreateCell(2).SetCellValue(rand.Next(100000, 99999999));
        row2.CreateCell(3).SetCellValue(usernameEnglish[j] + rand.Next(100000, 99999999));
        row2.CreateCell(4).SetCellValue(13 + rand.Next(100000, 99999999));
        row2.CreateCell(5).SetCellValue(city[j]);
        row2.CreateCell(6).SetCellValue(rand.Next(100, 999) + "万");
        j++;
        if (j > 6)
        {
            j = 0;

        }
    }
    HSSFRow row1 = sheet.CreateRow(15) as HSSFRow;
    for (int i = 16; i < 17; i++)
    {
        HSSFRow row2 = sheet.CreateRow(i) as HSSFRow;
        row2.CreateCell(0).SetCellValue("编码");
        row2.CreateCell(1).SetCellValue("月度");
        row2.CreateCell(2).SetCellValue("编码");
        row2.CreateCell(3).SetCellValue("月度");
        row2.CreateCell(4).SetCellValue("编码");
        row2.CreateCell(5).SetCellValue("月度");
        row2.CreateCell(6).SetCellValue("编码");
        row2.CreateCell(7).SetCellValue("月度");
    }

    MemoryStream memoryStream = new MemoryStream();
    workbook.Write(memoryStream);
   // Response.AddHeader("Content-Disposition", "attachment;filename=富婆通讯有限公司.xlsx");
   // Response.AddHeader("Content-Disposition", "attachment;filename=富婆通讯有限公司.xls");
    Response.AddHeader("Content-Disposition", "attachment;filename=富婆通讯有限公司.csv");

    Response.BinaryWrite(memoryStream.ToArray());

}

导出结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你的美,让我痴迷

你的好,我会永远记住你的。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值