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)方 ...
随机推荐
- 【原】画流程图工具visio使用技巧汇总
最近写论文需要画不少流程图,有两种选择,一是word, 二是visio.原先一直用word画,效果也还可以,但是每个部件画完后为了便于适应排版变动,需要将需要的模块按下ctrl逐个点击选中后进行组合. ...
- rails关于utf8问题-------------------utf8申明必须置顶
utf-8必须置顶,如果放在其他位置,会导致后面如果遇到中文无法解析,然后报其他乱七八糟的错误,比如不能连接数据库,比如语法错误......这种错误不好找,切记!!! 出错代码: #!/bin/env ...
- cocos2d-x之Vector与map
bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getIn ...
- uname
uname uname用于打印操作系统和硬件架构相关的信息,对于可能在多个系统或架构上运行的Shell脚本程序很有用, 缺省选项相当于 -s 或--system $uname [-amnrsvpio] ...
- linux—【用户和组的管理操作】(5)
用户:user 组:group 增加:add 修改:modify mod 删除:delete del useradd 增加用户 usermod 修改用户 userdel ...
- matlab中subplot函数的功能
转载自http://wenku.baidu.com/link?url=UkbSbQd3cxpT7sFrDw7_BO8zJDCUvPKrmsrbITk-7n7fP8g0Vhvq3QTC0DrwwrXfa ...
- NOIP2014提高组 DAY1 -SilverN
T1 生活大爆炸版石头剪刀布 题目描述 石头剪刀布是常见的猜拳游戏:石头胜剪刀,剪刀胜布,布胜石头.如果两个人出拳一样,则不分胜负.在<生活大爆炸>第二季第8 集中出现了一种石头剪刀布的 ...
- 边工作边刷题:70天一遍leetcode: day 84-3
Meeting Rooms I/II 要点:这题和skyline类似,利用了interval start有序的特点,从左向右处理,用一个heap来动态表示当前占用rooms的时间段,所以heap的si ...
- poj1459 Power Network (多源多汇最大流)
Description A power network consists of nodes (power stations, consumers and dispatchers) connected ...
- UVA 12532 Interval Product
线段树水题,忽略每个数的大小,只记住他们的正负即可,规规矩矩的代码.不过这是我第一次完全自己写的一次A的代码了.纪念一下... #include <iostream> #include & ...