首页 新闻 会员 周边 捐助

NPOI 导出EXCEL

0
悬赏园豆:40 [已关闭问题] 关闭于 2014-10-09 12:56

导出EXCEL其他都没问题  现在是添加图片出问题。 我是一个循环添加的图片  但是最后只有最后一张图片前面的都被覆盖掉了。

private static void AddPieChart(HSSFWorkbook workbook)
        {
            string filename;
            int pictureIdx;
            HSSFPatriarch patriarch;
            HSSFClientAnchor anchor;
            Sheet sheet = workbook.GetSheet("Sheet1");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                filename = dt.Rows[i][5].ToString();
                byte[] bytes = System.IO.File.ReadAllBytes(filename);
                if (!string.IsNullOrEmpty(filename))
                {
                    pictureIdx = workbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.JPEG);
                    patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
                    anchor = new HSSFClientAnchor(0, 0, 100, 50, 5, i, 5 + 1, i + 1);
                    anchor.AnchorType = 2;
                    patriarch.CreatePicture(anchor, pictureIdx);

                }
            }
        }

 求大神看看。 在线急等!!

战马的主页 战马 | 初学一级 | 园豆:27
提问于:2014-08-05 13:41
< >
分享
所有回答(2)
0

没看出你的代码有啥问题。

幻天芒 | 园豆:37207 (高人七级) | 2014-08-05 14:02
0

private static void AddPieChart(HSSFWorkbook workbook)
{
string filename;
int pictureIdx;
HSSFClientAnchor anchor;
Sheet sheet = workbook.GetSheet("Sheet1");
HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
for (int i = 0; i < dt.Rows.Count; i++)
{
filename = dt.Rows[i][5].ToString();
byte[] bytes = System.IO.File.ReadAllBytes(filename);
if (!string.IsNullOrEmpty(filename))
{
pictureIdx = workbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.JPEG);
anchor = new HSSFClientAnchor(0,0, 700, 255, 5, i + 1, 5, i + 1);
anchor.AnchorType = 2;
patriarch.CreatePicture(anchor, pictureIdx);
}
}
}

 

对象需在开始实例化 否则后面会一直覆盖之前的对象

战马 | 园豆:27 (初学一级) | 2014-08-05 14:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册