NPOI 1.2.3教程 -15 插入图片InsertPicture
2011-01-23 15:58
477 查看
using System; using System.Text; using System.IO; using NPOI.HSSF.UserModel; using NPOI.HPSF; using NPOI.POIFS.FileSystem; using System.Drawing; using NPOI.SS.UserModel; namespace InsertPicturesInXls { class Program { static void Main(string[] args) { InitializeWorkbook(); Sheet sheet1 = hssfworkbook.CreateSheet("PictureSheet"); HSSFPatriarch patriarch = (HSSFPatriarch)sheet1.CreateDrawingPatriarch(); //create the anchor HSSFClientAnchor anchor; anchor = new HSSFClientAnchor(0, 0, 0, 255, 2, 2, 4, 7); anchor.AnchorType = 2; //load the picture and get the picture index in the workbook HSSFPicture picture = (HSSFPicture)patriarch.CreatePicture(anchor, LoadImage("../../image/HumpbackWhale.jpg", hssfworkbook)); //Reset the image to the original size. picture.Resize(); picture.LineStyle = HSSFPicture.LINESTYLE_DASHDOTGEL; WriteToFile(); } public static int LoadImage(string path, HSSFWorkbook wb) { FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read); byte[] buffer = new byte[file.Length]; file.Read(buffer, 0, (int)file.Length); return wb.AddPicture(buffer, PictureType.JPEG); } static HSSFWorkbook hssfworkbook; static void WriteToFile() { //Write the stream data of workbook to the root directory FileStream file = new FileStream(@"test.xls", FileMode.Create); hssfworkbook.Write(file); file.Close(); } static void InitializeWorkbook() { hssfworkbook = new HSSFWorkbook(); //create a entry of DocumentSummaryInformation DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation(); dsi.Company = "NPOI Team"; hssfworkbook.DocumentSummaryInformation = dsi; //create a entry of SummaryInformation SummaryInformation si = PropertySetFactory.CreateSummaryInformation(); si.Subject = "NPOI SDK Example"; hssfworkbook.SummaryInformation = si; } } }
相关文章推荐
- NPOI2.2.0.0实例详解(十一)—向EXCEL插入图片
- 【ppt入门教程】如何在ppt幻灯片中插入GIF格式动态图片
- NPOI导出Word插入图片问题解决方案
- NPOI 1.2.3教程 -7 单元格高度和宽度HeightAndWidth
- NPOI 1.2.3教程 -23 页眉页脚HeaderFooter
- NPOI 批量插入图片进Excel
- excel 插入图片 NPOI
- NPOI 1.2.3教程 - 2 使用NPOI生成xls文件
- NPOI 1.2.3教程 -8 打印Print
- NPOI 1.2.3教程 -24 下拉框DropDownList
- 2.4.5 用NPOI操作EXCEL--插入图片
- NPOI 1.2.3教程 - 3 縮放Zoom
- NPOI 1.2.3教程 -9 格式化Format
- NPOI 1.2.3教程 -25 条件样式ConditionalFormat
- node-webkit教程(15)当图片加载失败的时候
- 使用Npoi向Excel中插入图片
- 2.4.5 用NPOI操作EXCEL--插入图片
- NPOI 1.2.3教程 - 4 基本公式 BasicFormula
- NPOI 1.2.3教程 -10批注Comment
- NPOI 1.2.3教程 -26 字体Font