0

我想在 Excel 工作簿的工作表中插入图像。我插入图像的代码在这里:

                    ISheet sheet = templateWorkbook.GetSheet(sheetName);

                    HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
                    //IDrawing patriarch = (IDrawing)sheet.CreateDrawingPatriarch();
                    //HSSFPatriarch patriarch = sheet1.CreateDrawingPatriarch();
                    HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,
                            start.Col, start.Row, end.Col, end.Row);
                    anchor.AnchorType = 2;
                    int pictureIdx = 0;
                    using (FileStream fs = new FileStream(f.GetServerPathOfFile(imagePath), FileMode.Open))
                    {
                        byte[] bytes = new byte[fs.Length];
                        fs.Write(bytes, 0, (int)fs.Length);
                        pictureIdx = templateWorkbook.AddPicture(bytes, PictureType.JPEG);
                    }
                    IPicture picture = patriarch.CreatePicture(anchor, pictureIdx);

我不知道我在哪里弄错了。代码运行良好,没有任何错误/异常。

4

2 回答 2

0

If problem are still actual - picture.Resize(); could help

于 2013-03-11T08:04:22.463 回答
0

我想我知道这个错误。如果你懂中文,

你需要先把图片加进去在描点。</p>

就放

using (FileStream fs = new FileStream(f.GetServerPathOfFile(imagePath), FileMode.Open))
{
    byte[] bytes = new byte[fs.Length];
    fs.Write(bytes, 0, (int)fs.Length);
    pictureIdx = templateWorkbook.AddPicture(bytes, PictureType.JPEG);
}

HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, 
  start.Col, start.Row, end.Col, end.Row);
anchor.AnchorType = 2;
于 2014-09-17T14:43:43.577 回答