NPOI 导出excel带图片,可控大小
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.DDF;
using NPOI.SS.UserModel;
using System.IO;
using NPOI.SS;
#region 导出
protected void btnexcel_Click(object sender, EventArgs e)
{
QQT_BLL.gg_content bll = new QQT_BLL.gg_content();
DataSet ds = bll.getds("");
DataTable dt = ds.Tables[0];
string excelname = System.DateTime.Now.ToString().Replace(":", "").Replace("-", "").Replace(" ", "");
string filePath = System.Web.HttpContext.Current.Server.MapPath("ReadExcel") + "\\" + excelname + ".xls";
MemoryStream ms = RenderDataTableToExcel(dt) as MemoryStream;
FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
byte[] data = ms.ToArray();
fs.Write(data, 0, data.Length);
fs.Flush();
fs.Close();
data = null;
ms = null;
fs = null;
#region 导出到客户端
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(excelname, System.Text.Encoding.UTF8) + ".xls");
Response.ContentType = "Application/excel";
Response.WriteFile(filePath);
Response.End();
#endregion
}
public Stream RenderDataTableToExcel(DataTable SourceTable)
{
MemoryStream ms = new MemoryStream();
NPOI.HSSF.UserModel.HSSFWorkbook workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet();
NPOI.SS.UserModel.IRow headerRow = sheet.CreateRow(0);
//设置7列宽为100
sheet.SetColumnWidth(7, 100);
//加标题
headerRow.CreateCell(0).SetCellValue("广告编号");
headerRow.CreateCell(1).SetCellValue("广告标题");
headerRow.CreateCell(2).SetCellValue("广告内容");
headerRow.CreateCell(3).SetCellValue("所属广告商");
headerRow.CreateCell(4).SetCellValue("广告电话");
headerRow.CreateCell(5).SetCellValue("广告网址");
headerRow.CreateCell(6).SetCellValue("广告小图片");
headerRow.CreateCell(7).SetCellValue("图片显示");
headerRow.CreateCell(8).SetCellValue("广告大图片");
headerRow.CreateCell(9).SetCellValue("图片显示");
headerRow.CreateCell(10).SetCellValue("审核状态");
int rowIndex = 1;
foreach (DataRow row in SourceTable.Rows)
{
NPOI.SS.UserModel.IRow dataRow = sheet.CreateRow(rowIndex);
dataRow.Height = 50;
//列高50
dataRow.CreateCell(0).SetCellValue(row["gg_id"].ToString());
dataRow.CreateCell(1).SetCellValue(row["gg_title"].ToString());
dataRow.CreateCell(2).SetCellValue(row["gg_memo"].ToString());
dataRow.CreateCell(3).SetCellValue(row["ggs_name"].ToString());
dataRow.CreateCell(4).SetCellValue(row["gg_tel"].ToString());
dataRow.CreateCell(5).SetCellValue(row["gg_add"].ToString());
dataRow.CreateCell(6).SetCellValue(row["p_name"].ToString());
string picurl = row["p_url"].ToString(); //图片存储路径
dataRow.CreateCell(8).SetCellValue(row["gg_check"].ToString());
AddPieChart(sheet, workbook, picurl,rowIndex ,7);
rowIndex++;
}
workbook.Write(ms);
ms.Flush();
ms.Position = 0;
sheet = null;
headerRow = null;
workbook = null;
return ms;
}
///
/// 向sheet插入图片
///
///
///
private void AddPieChart(ISheet sheet, HSSFWorkbook workbook, string fileurl,int row,int col)
{
try
{
//add picture data to this workbook.
string path = Server.MapPath("~/html/");
if (fileurl.Contains("/"))
{
path += fileurl.Substring( fileurl.IndexOf ('/'));
}
string FileName = path;
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();这句话一定不要,这是用图片原始大小来显示
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
NPOI 导出excel带图片,可控大小的更多相关文章
- asp.net 导出excel带图片
protected void btgua_Click(object sender, EventArgs e) { DataTable dt = ds.Tables[0]; if (dt != null ...
- NPOI导出excel(带图片)
近期项目中用到Excel导出功能,之前都是用普通的office组件导出的方法,今天尝试用下NPOI,故作此文以备日后查阅. 1.NPOI官网http://npoi.codeplex.com/,下载最新 ...
- NPOI导出Excel及使用问题
NPOI导出Excel及使用问题 因为最近公司质管部门提出了一个统计报表的需求:要求导出一个2016及2017年度深圳区域的所有供应商的费用成本计算--一个22列的Excel表,其中还包括多列的合并单 ...
- NPOI导出Excel (C#) 踩坑 之--The maximum column width for an individual cell is 255 charaters
/******************************************************************* * 版权所有: * 类 名 称:ExcelHelper * 作 ...
- Asp.Net 使用Npoi导出Excel
引言 使用Npoi导出Excel 服务器可以不装任何office组件,昨天在做一个导出时用到Npoi导出Excel,而且所导Excel也符合规范,打开时不会有任何文件损坏之类的提示.但是在做导入时还是 ...
- NPOI导出EXCEL 打印设置分页及打印标题
在用NPOI导出EXCEL的时候设置分页,在网上有查到用sheet1.SetRowBreak(i)方法,但一直都没有起到作用.经过研究是要设置 sheet1.FitToPage = false; 而 ...
- .NET NPOI导出Excel详解
NPOI,顾名思义,就是POI的.NET版本.那POI又是什么呢?POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office的文件. 支持的文件格式包括xls, ...
- NPOI导出Excel(含有超过65335的处理情况)
NPOI导出Excel的网上有很多,正好自己遇到就学习并总结了一下: 首先说明几点: 1.Excel2003及一下:后缀xls,单个sheet最大行数为65335 Excel2007 单个sheet ...
- [转]NPOI导出EXCEL 打印设置分页及打印标题
本文转自:http://www.cnblogs.com/Gyoung/p/4483475.html 在用NPOI导出EXCEL的时候设置分页,在网上有查到用sheet1.SetRowBreak(i)方 ...
随机推荐
- IOS 瀑布流UICollectionView实现
IOS 瀑布流UICollectionView实现 在实现瀑布流之前先来看看瀑布流的雏形(此方法的雏形 UICollectionView) 对于UICollectionView我们有几点注意事项 它和 ...
- 明明已经执行Log.i,偏偏打不出日志
Android内打日志用的当然是Log.i(tag,string),调试时的日志输出可以很快的反映一些问题,方便我们跟进. 但是如果连日志都打不出来了怎么办呢,我今天就遇到了比较坑的问题.项目里别的日 ...
- C++语言-03-类与对象
类 类是面向对象编程中的核心概念,用于定义一个数据类型的蓝图,描述类的对象包括什么,以及可以在这些对象上执行那些操作. 类的成员 数据成员 描述数据的表示方法 class ClassName { ac ...
- RunTime(运行时机制)
1>runtime实现的机制是什么,怎么用,一般用于干嘛? 这个问题我就不跟大家绕弯子了,直接告诉大家, runtime是一套比较底层的纯C语言API, 属于1个C语言库, 包含了很多底层的C语 ...
- virtualbox 中ubantu虚拟机范文win7文件夹
宿主机操作系统: windows7_x64 旗舰版 Oracle VM virtualBox版本:5.0.12 虚拟机操作系统: Ubantu15.12_x64 Desktop版本 让虚拟机可以访 ...
- C++中const用法总结
1修饰变量/指针 注意以下几种修饰的区别: (1)const int * a; (2)int const *a; (3)int * const b; (4)int const* const c; 其中 ...
- JavaScript闭包的底层运行机制
转自:http://blog.leapoahead.com/2015/09/15/js-closure/ 我研究JavaScript闭包(closure)已经有一段时间了.我之前只是学会了如何使用它们 ...
- Eclipse中使用Git-让版本管理更简单
详细步骤: 第一部分 GIT介绍 (1)GIT往世今生 2005年开发Samba的Andrew试图破解BitKeeper(一款商业的版本控制系统)的协议(这么干的其实也不只他一个),被BitMove ...
- Swift学习笔记--变量与常量
1.Swift是一门强类型语言,不能为变量赋予其自身数据类型之外的值: 2.声明变量使用var关键字,声明常量使用let关键字: 3.声明变量或常量时没有对其指定类型且赋予了初值,则编译器会自动推断常 ...
- hadoop debug script
A Hadoop job may consist of many map tasks and reduce tasks. Therefore, debugging a Hadoop job is of ...