C# 實現Excel匯出圖片
1.匯出Excle表格,表格內巢狀圖片功能:
2.封裝匯出檔案方法
public bool ExportExcel(DataTable dt, string notile,string title)
{
CreatePic cp = new CreatePic();
//建立工作簿
NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
//建立表
NPOI.SS.UserModel.ISheet sheet = book.CreateSheet(notile);
//自適應列寬
// sheet.AutoSizeColumn(1, true);
//標題行合併單元格
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, dt.Columns.Count - 1));
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;
}
}
3.具體的引用
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
3.封裝成方法,使用NOPI外掛匯出Excel時候呼叫改方法即可
/// <summary>
/// 封裝方法,用於表格內渲染圖片
/// </summary>
/// <param name="sheet"></param>
/// <param name="workbook">工作簿</param>
/// <param name="fileurl">圖片地址</param>
/// <param name="row">行</param>
/// <param name="col">列</param>
public void AddPic(ISheet sheet, HSSFWorkbook workbook, string fileurl, int row, int col)
{
byte[] bytes = System.IO.File.ReadAllBytes(fileurl);
int picindex = workbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.JPEG);
HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,48,48,col,row,col+1,row+1);
HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor,picindex);
}
文章為原創作品