打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C#实现Excel导出图片,NPOI组件进行实现,源码可用
NPOI.SS.UserModel.IRow firstrow = sheet.CreateRow(0); NPOI.SS.UserModel.ICell firstcell = firstrow.CreateCell(0); //表名样式 NPOI.SS.UserModel.ICellStyle styleHeader = book.CreateCellStyle(); NPOI.SS.UserModel.IFont fontHeader = book.CreateFont(); styleHeader.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left; styleHeader.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; fontHeader.FontHeightInPoints = 15; styleHeader.SetFont(fontHeader); firstcell.CellStyle = styleHeader; firstcell.SetCellValue(title); 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 = 14; 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 = 12; styleBody.SetFont(fontBody); //创建具体单元格数据 int rowCount = dt.Rows.Count; int colCount = dt.Columns.Count; NPOI.SS.UserModel.IRow colNameRow = sheet.CreateRow(1); for (int x = 0; x < colCount; x++) { //将列名写入单元格 NPOI.SS.UserModel.ICell colNameCell = colNameRow.CreateCell(x); colNameCell.SetCellValue(dt.Columns[x].ColumnName); colNameCell.CellStyle = styleColName; } for (int i = 0; i < rowCount; i++) { NPOI.SS.UserModel.IRow row = sheet.CreateRow(i + 2);//数据从第三行开始 第一行表名 第二行列名 for (int j = 0; j < colCount; j++) { //填充数据 NPOI.SS.UserModel.ICell cell = row.CreateCell(j); if (dt.Rows[i][j] != null) { cell.SetCellValue(dt.Rows[i][j].ToString() + ' '); if (dt.Rows[i][j].ToString().Contains('www')) { cell.Row.Height = 4000; cp.AddPic(sheet, book, dt.Rows[i][j].ToString(), i+2, j); } } else { cell.SetCellValue(''); } cell.CellStyle = styleBody; } } //自适应列宽 for (int x = 0; x < colCount; x++) { sheet.AutoSizeColumn(x, true); } //最后插入图片 //设置宽高 // sheet.SetColumnWidth(4,2000); //此处代码是将 xls文件发到页面通过浏览器直接下载到本地  可以放到 界面调用的地方 System.IO.MemoryStream ms = new System.IO.MemoryStream(); book.Write(ms); //Response.AddHeader('Content-Disposition', string.Format('attachment; filename=ccc.xls')); Response.AddHeader('Content-Disposition', 'attachment;filename=' + Server.UrlEncode(notile + '.xls'));//注意 Response.BinaryWrite(ms.ToArray()); book = null; ms.Close(); ms.Dispose(); return true; } catch { throw new Exception(); } finally { book = null; } }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C# 使用 NPOI 库读写 Excel 文件
(1.2.4)无法将类型“NPOI.SS.UserModel.Sheet”隐式转换为“NPOI.HSSF.UserModel.HSSFSheet”
C# 操作Excel文件之NPOI (二)
NET NPOI操作Excel常用函数
NPOI对excel操作总计
如何将asp.net DataTable导出Excel自定义列名?
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服