NPOI創建組合

組合是文字,圖片,文本框等的結合 

//創建一個工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//創建一個sheet
ISheet sheet1 = workbook.CreateSheet("sheet1");
HSSFPatriarch patriarch = (HSSFPatriarch)sheet1.CreateDrawingPatriarch();
HSSFClientAnchor a1 = new HSSFClientAnchor(255, 125, 1023, 150, 0, 0, 2, 2);

//創建組合
HSSFShapeGroup hSSFShapeGroup = patriarch.CreateGroup(a1);
HSSFChildAnchor childAnchor1 = new HSSFChildAnchor(255, 125, 1023, 150);
HSSFChildAnchor childAnchor2 = new HSSFChildAnchor(0, 0, 1023, 150);

//組合加直線
HSSFSimpleShape shape1 = hSSFShapeGroup.CreateShape(childAnchor1);
shape1.ShapeType = HSSFSimpleShape.OBJECT_TYPE_LINE;
shape1.LineStyle = HSSFShape.LINESTYLE_SOLID;

//組合加圖片
string picPath = @"C:\Users\537\Desktop\Test\圖片1.png";
FileStream picFs = File.OpenRead(picPath); //OpenRead
int filelength = 0;
filelength = (int)picFs.Length; //獲得文件長度 
Byte[] image = new Byte[filelength]; //建立一個字節數組 
picFs.Read(image, 0, filelength); //按字節流讀
int pictureIdx = workbook.AddPicture(image, PictureType.PNG);
hSSFShapeGroup.CreatePicture(childAnchor2, pictureIdx);

//組合加文本框
HSSFTextbox hSSFTextbox = hSSFShapeGroup.CreateTextbox(childAnchor2);
hSSFTextbox.WrapText = 1;
IRichTextString richTextString = new HSSFRichTextString("123");
IFont font = workbook.CreateFont();
font.FontName = "C39HrP36DmTt";// 字體
font.FontHeightInPoints = 36;// 字號大小
richTextString.ApplyFont(font);
hSSFTextbox.String = richTextString;

using (Stream stream = File.OpenWrite("d:\\ccc.xls"))
{
    workbook.Write(stream);
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章