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)方 ...
随机推荐
- Oracle行列转换
一.建表与插入数据 1.1.建表 create table kecheng ( id NUMBER, name ), course ), score NUMBER ); insert into kec ...
- IOS 登陆界面的简单编写(通过NSNotificationCenter)
在介绍内容的之前,先看一下实现结果. 通过细节可以发现,只有当手机号与密码都输入的情况登录按钮才会变亮.那么这是怎么实现的呢? 首先我们要知道,这种情况的发生的首要条件便是每时每刻都知道两个TextF ...
- 不小心改了Xcode系统的头文件,运行报错,解决办法
- iOS开发笔记11:表单键盘遮挡、浮点数价格格式化显示、省市区选择器、View Debugging
1.表单键盘遮挡 应用场景为一个collectionView上有多个textfield.textView供用户填写信息. 之前输入项较少时,采取的方法比较粗暴,didSelectItemAtIndex ...
- Linux写时拷贝技术(copy-on-write)
COW技术初窥: 在Linux程序中,fork()会产生一个和父进程完全相同的子进程,但子进程在此后多会exec系统调用,出于效率考虑,linux中引入了“写时复制“技术,也就是只有进程空间的各段的内 ...
- 【linux】关于TCP三次握手和四次挥手
1.TCP是什么 关于OSI的七层模型 TCP在第四层——Transport层,第四层的数据叫Segment->报文 IP在第三层——Network层,在第三层上的数据叫Packet->数 ...
- Android中解析XML
XML在各种开发中都广泛应用,Android也不例外.作为承载数据的一个重要角色,如何读写XML成为Android开发中一项重要的技能.今天就由我向大家介绍一下在Android平台下几种常见的XML解 ...
- chrome45以后的版本安装lodop后,仍提示未安装解决
请先查看你chrome浏览器的版本,如果是45版本以前的版本,安装后仍提示 "未安装" 或 "请升级" 请参照本链接解决:http://blog.sina.co ...
- 从mysql数据表中随机取出一条记录
核心查找数据表代码: ; //此处的1就是取出数据的条数 但这样取数据网上有人说效率非常差的,那么要如何改进呢 搜索Google,网上基本上都是查询max(id) * rand()来随机获取数据. S ...
- Helloworld -SilverN
/*Hello World*/ #include<iostream> #include<cstdio> #include<cstring> using namesp ...