///
导出数据
///
public static MemoryStream ImportFileR(DataTable t)
MemoryStream stream = new MemoryStream();
if (t != null && t.Rows.Count > 0)
XSSFWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet();
IRow headerRow = sheet.CreateRow(0);
foreach (DataColumn column in t.Columns)
headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);
int rowIndex = 1;
foreach (DataRow row in t.Rows)
IRow dataRow = sheet.CreateRow(rowIndex);
foreach (DataColumn column in t.Columns)
if (row[column].ToString().IndexOf("Content/") > -1)
MsgInsetImg(sheet, dataRow, t.Rows.IndexOf(row) + 1, workbook, row[column].ToString().Replace("/Content/", "Content/"), t.Columns.IndexOf(column), System.Web.HttpContext.Current.Server.MapPath("/"));
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
rowIndex++;
workbook.Write(stream);
return stream;
public static void MsgInsetImg(ISheet sheet, IRow row1, int i, XSSFWorkbook xssfworkbook, string ImgUrl, int colnum, string MapPath)
//设置图片那的宽高
sheet.SetColumnWidth(colnum, 2900);
row1.Height = 1600;
var dPath = MapPath + ImgUrl;
System.Drawing.Image imgOutput = System.Drawing.Bitmap.FromFile(dPath);
System.Drawing.Image img = imgOutput.GetThumbnailImage(80, 100, null, IntPtr.Zero);
//图片转换为文件流
MemoryStream ms = new MemoryStream();
img.Save(ms, ImageFormat.Bmp);
BinaryReader br = new BinaryReader(ms);
var picBytes = ms.ToArray();
ms.Close();
//插入图片
if (picBytes != null && picBytes.Length > 0)
var rows = i;
var cols = colnum;
/* Add Picture to Workbook, Specify picture type as PNG and Get an Index */
int pictureIdx = xssfworkbook.AddPicture(picBytes, NPOI.SS.UserModel.PictureType.PNG); //添加图片
/* Create the drawing container */
XSSFDrawing drawing = (XSSFDrawing)sheet.CreateDrawingPatriarch();
/* Create an anchor point */
XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, cols, rows, 1, 3);
/* Invoke createPicture and pass the anchor point and ID */
XSSFPicture picture = (XSSFPicture)drawing.CreatePicture(anchor, pictureIdx);
/* Call resize method, which resizes the image */
picture.Resize();
picBytes = null;
catch (Exception ex)
row1.CreateCell(colnum).SetCellValue("图片导出失败");
查看更多关于NPOI组件导出EXCEL文档源码XSSFWorkbook实例带图片导出的详细内容...
阅读:15904次
CopyRight:2016-2023
语言吧
备案ICP:
湘ICP备09009000号-15
http://www.yuyanba.com
本站资讯不构成任何建议,仅限于个人分享,参考须谨慎!
本网站对有关资料所引致的错误、不确或遗漏,概不负任何法律责任。
本网站刊载的所有内容(包括但不仅限文字、图片、LOGO、音频、视频、软件、程序等)版权归原作者所有。任何单位或个人认为本网站中的内容可能涉嫌侵犯其知识产权或存在不实内容时,请及时通知本站,予以删除。