需要引入dll文件

也可以在NuGet里面管理(推荐) 比较方便 。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace CSR_Web.Common
{
public class NPOIExport
{
public static NPOI.HSSF.UserModel.HSSFWorkbook DoExport(System.Data.DataTable dt, string notile)
{ //创建工作簿
NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
//创建表
NPOI.SS.UserModel.ISheet sheet = book.CreateSheet(notile);
//自适应列宽
// sheet.AutoSizeColumn(1, true);
//标题行合并单元格
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(, , , dt.Columns.Count-)); NPOI.SS.UserModel.IRow firstrow = sheet.CreateRow();
NPOI.SS.UserModel.ICell firstcell = firstrow.CreateCell(); //表名样式
NPOI.SS.UserModel.ICellStyle styleHeader = book.CreateCellStyle();
NPOI.SS.UserModel.IFont fontHeader = book.CreateFont();
styleHeader.Alignment =NPOI.SS.UserModel.HorizontalAlignment.Center;
styleHeader.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
fontHeader.FontHeightInPoints =; styleHeader.SetFont(fontHeader);
firstcell.CellStyle = styleHeader;
firstcell.SetCellValue(notile); try
{
//列名样式
NPOI.SS.UserModel.ICellStyle styleColName = book.CreateCellStyle();
NPOI.SS.UserModel.IFont fontColName = book.CreateFont();
styleColName.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
styleColName.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
fontColName.FontHeightInPoints = ;
styleColName.SetFont(fontColName); //数据的样式、字体大小
NPOI.SS.UserModel.ICellStyle styleBody = book.CreateCellStyle();
NPOI.SS.UserModel.IFont fontBody = book.CreateFont(); styleBody.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
styleBody.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
fontBody.FontHeightInPoints = ;
styleBody.SetFont(fontBody); //创建具体单元格数据
int rowCount = dt.Rows.Count;
int colCount = dt.Columns.Count; NPOI.SS.UserModel.IRow colNameRow = sheet.CreateRow();
for (int x = ; x < colCount; x++) { //将列名写入单元格
NPOI.SS.UserModel.ICell colNameCell = colNameRow.CreateCell(x);
colNameCell.SetCellValue(dt.Columns[x].ColumnName);
colNameCell.CellStyle = styleColName;
} for (int i = ; i < rowCount; i++)
{
NPOI.SS.UserModel.IRow row = sheet.CreateRow(i + );//数据从第三行开始 第一行表名 第二行列名 for (int j = ; j < colCount; j++)
{
//填充数据
NPOI.SS.UserModel.ICell cell = row.CreateCell(j); if (dt.Rows[i][j] != null)
{ cell.SetCellValue(dt.Rows[i][j].ToString());
}
else
{
cell.SetCellValue("");
} cell.CellStyle = styleBody;
}
}
//自适应列宽
for (int x = ; x < colCount; x++)
{
sheet.AutoSizeColumn(x, true); }

            

//插入图片
byte[] bytes = System.IO.File.ReadAllBytes(FileName);
if (!string.IsNullOrEmpty(FileName))
{
int pictureIdx = workbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.JPEG);
HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 100, 50, col, row, col + 1, row + 1);
//##处理照片位置,【图片左上角为(col, row)第row+1行col+1列,右下角为( col +1, row +1)第 col +1+1行row +1+1列,宽为100,高为50

HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);

// pict.Resize();这句话一定不要,这是用图片原始大小来显示
}

//此处代码是将 xls文件发到页面通过浏览器直接下载到本地  可以放到 界面调用的地方
//System.IO.MemoryStream ms = new System.IO.MemoryStream();
//book.Write(ms);
//Response.AddHeader("Content-Disposition", string.Format("attachment; filename=绩效统计.xls"));
//Response.BinaryWrite(ms.ToArray());
//book = null;
//ms.Close();
//ms.Dispose(); return book;
}
catch {
throw new Exception();
}finally{
book=null;
} } }
}
  /// <summary>
///导出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnExport_Click(object sender, EventArgs e)
{ DataTable dt = cmbll.getdt(); NPOI.HSSF.UserModel.HSSFWorkbook book = NPOIExport.DoExport(dt, "xxx报表");
//写入客户端
try
{
WriteClient(book);
}
catch
{ }
finally
{ book = null;
} } public void WriteClient(NPOI.HSSF.UserModel.HSSFWorkbook book)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
book.Write(ms);
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=客户资料"+DateTime.Now.ToString("yyyyMMddHHmmss")+".xls"));
Response.BinaryWrite(ms.ToArray());
book = null;
ms.Close();
ms.Dispose();
}

c# NPOI 导出EXCEL (在下方显示图片)的更多相关文章

  1. NPOI导出excel(带图片)

    近期项目中用到Excel导出功能,之前都是用普通的office组件导出的方法,今天尝试用下NPOI,故作此文以备日后查阅. 1.NPOI官网http://npoi.codeplex.com/,下载最新 ...

  2. NPOI导出Excel,添加图片和设置格式,添加条形码

    先上代码 using grproLib; using System; using System.Collections.Generic; using System.Data; using System ...

  3. NPOI导出Excel (C#) 踩坑 之--The maximum column width for an individual cell is 255 charaters

    /******************************************************************* * 版权所有: * 类 名 称:ExcelHelper * 作 ...

  4. Asp.Net 使用Npoi导出Excel

    引言 使用Npoi导出Excel 服务器可以不装任何office组件,昨天在做一个导出时用到Npoi导出Excel,而且所导Excel也符合规范,打开时不会有任何文件损坏之类的提示.但是在做导入时还是 ...

  5. NPOI导出EXCEL 打印设置分页及打印标题

    在用NPOI导出EXCEL的时候设置分页,在网上有查到用sheet1.SetRowBreak(i)方法,但一直都没有起到作用.经过研究是要设置  sheet1.FitToPage = false; 而 ...

  6. .NET NPOI导出Excel详解

    NPOI,顾名思义,就是POI的.NET版本.那POI又是什么呢?POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office的文件. 支持的文件格式包括xls, ...

  7. NPOI导出Excel(含有超过65335的处理情况)

    NPOI导出Excel的网上有很多,正好自己遇到就学习并总结了一下: 首先说明几点: 1.Excel2003及一下:后缀xls,单个sheet最大行数为65335 Excel2007 单个sheet ...

  8. [转]NPOI导出EXCEL 打印设置分页及打印标题

    本文转自:http://www.cnblogs.com/Gyoung/p/4483475.html 在用NPOI导出EXCEL的时候设置分页,在网上有查到用sheet1.SetRowBreak(i)方 ...

  9. 分享使用NPOI导出Excel树状结构的数据,如部门用户菜单权限

    大家都知道使用NPOI导出Excel格式数据 很简单,网上一搜,到处都有示例代码. 因为工作的关系,经常会有处理各种数据库数据的场景,其中处理Excel 数据导出,以备客户人员确认数据,场景很常见. ...

  10. 用NPOI导出Excel

    用NPOI导出Excel public void ProcessRequest(HttpContext context) { context.Response.ContentType = " ...

随机推荐

  1. django jquery ajax 知识点

    示例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <div id='d'>1</div> <div> <div id='i1' nam ...

  2. 在centos7中安装python3并设置为默认版本

    1,查看Python默认版本 python -V (大写V) 2,看看执行Python在哪个位置 which python 3,安装gcc,用于编译Python源码 yum install gcc 4 ...

  3. windows 2008R2系统程序运行提示无法定位程序输入点ucrtbase.terminate

    1.用python写了个脚本,打成exe程序,在一些机器上正常运行,再另外一些机器上运行提示 无法定位程序输入点ucrtbase.terminate 应该是缺少库文件支持 2.网上搜了下.https: ...

  4. Linux内核分析第二次作业

    这周学习了<庖丁解牛Linux内核分析>并且学习了实验楼的相关知识. 在实验楼的虚拟环境下编写代码: 通过gcc编译后,使用查看文件命令:cat  -n 20189223.c 在vim中, ...

  5. java volatile

    volatile可以保证变量的可见性 当一个变量定义为volatile后,此变量对所有的线程具有可见性.这里的可见性是指当一个线程修改了这个变量的值,新值对于其他线程来说是可以立即得知的. 每次使用v ...

  6. NotePad++ 配置Python工作环境

    下载地址:https://notepad-plus-plus.org/ Current Version: 7.5.3 sss 显示空格和指标符 为什么建议这么作?因为判断Python语句是否在同一层次 ...

  7. nginx gzip配置

    参考: https://docs.nginx.com/nginx/admin-guide/web-server/compression/ server { gzip on;    gzip_types ...

  8. 【算法和数据结构】_14_小算法_Blank字符替换

    /* 本程序用来将输入的制表符替换为\t, 而将退格替换为\b, 将反斜杠替换为\\ */ #include <stdio.h> #include <stdlib.h> typ ...

  9. java代码------charAt()的用法

    总结:你看这个方法的用处真的蛮多比如这个计算器小项目,用这个charAt()方法来装运算符 package com.mmm; import java.util.Scanner; public clas ...

  10. 基础 - 获得CPU主频

    // 获得cpu主频.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <windows.h> #include ...